• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Data conversion in Hibernate

 
Somak Dalui
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a column in database of type smallint.It is guranteed that the column will have values 0 and 1.In Hibernate I have mapped this as
.

I want to convert this column to a boolean value in my POJO.0 = false, 1 = true. Can I do that ?

Right now, this mapping is giving error because there is a NULL value in one of the records , inspite of having .How can I avoid this ?
 
Lakshmi Dasari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try type="yes_no" in your property mapping in hbm.xml and use a boolean datatype for the property in your pojo instead of Boolean. "yes-no" type converts 0 to false and 1 to true boolean datatype. And not-null="true" only tells that at the time of insertion/updation of a record through hibernate, null value is not acceptable. In your particular case, you could think of updating your database if its not a prod database and/or giving a default value for this column in the database table so that you would not encounter a null value.
 
Somak Dalui
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Lakshmi, it works great.
 
Somak Dalui
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help !! This is not working while updating. I am setting a boolean value to the field and it's giving "Implicit conversion from datatype 'CHAR' to 'INT' is not allowed.Use the CONVERT function to run this query.". I am using Sybase DB.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, before I was going to suggest creating a UserType like the on eposted here at Hibernate's website

http://www.hibernate.org/189.html

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic