Workaround for storing boolean variable in JPA entity using Hibernate and MySQL


When we instruct Hibernate to update [assigning the value of hibernate.hbm2ddl.auto as update] the tables based on the JPA entity classes, Hibernate converts a boolean (or Boolean class) to the Type Bit in MySQL.

For example, the following in an JPA entity class 

   @Column(name = "answer")
    private Boolean answer;

 is converted to a column as below in MySQL.

answer    bit(1)

We may populate the value in Seam using the below code in the UI (JSF)

 <h:selectBooleanCheckbox id="qanswer"  value="#{qopt.answer}"/>

Now when we persist (save) the entity we may get the below exception

java.sql.SQLException: Data too long for column

Reason: Though Hibernate generated the table structure and designated the column type as bit for a boolean variable, there are some issues in storing the value in Type bit for MySQL.

Solution: Declare the column type as SMALLINT

For more informtion please refer http://opensource.atlassian.com/projects/hibernate/browse/HHH-468

 
 
 
 
Comments:

Post a Comment:
  • HTML Syntax: Allowed
 

« January 2009
SunMonTueWedThuFriSat
    
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
       
Today

Valid XHTML or CSS?

[This is a Roller site]
 
© Karthik