-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate HBM File - Map one field to multiple data types
PostPosted: Wed Jun 22, 2005 6:10 am 
Newbie

Joined: Mon May 30, 2005 5:52 am
Posts: 15
Hi,
We need to support DB2 and Postgres databases. Having investigated this there is one fundamental difference between the two. In Postgres a Boolean type is allowed and in DB2 it is not.

What we have to do (more then likly) is to convert Boolean fields to SmallInt in DB2.

However, this poses a problem for our Hibernate API when we access the DB2 database in that we cannot convert from Smallint to Boolean.

This is an exaple of a boolean value specification in our HBM file.

<property
name="disabledInd"
type="java.lang.Boolean"
column="disabled_ind"
length="1"
>

So, what I am woundering is has anyone ever had this problem in Hibernate where we want to map one field to different types depending on the database we are connecting to and how the problem was solved.

thanks,
Richie.


Top
 Profile  
 
 Post subject: Hibernate HBM File - Map one field to multiple data types
PostPosted: Wed Jun 22, 2005 11:41 am 
Newbie

Joined: Mon May 30, 2005 5:52 am
Posts: 15
I have looked into UserTypes for this and this will not work. This is becuase at compile time the hbm2java process calles the returnedClass method of all classes that Implement UserType and in our case we don't know what the return type is going to be until run-time.

So, what I really want to know is how can I keep the hbm files and effectivly the database API the same (say always to think that the field will be boolean). But underneath hibernate change the database mapping to map from SmallInt to Boolean when the database is DB2. So, it would basically involve modifiying the Hibernate version of the standardjbosscmp-jdbc.xml CMP file to tell it to map all Smallint values to Boolean when the database is DB2.

thanks,
Richie.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 22, 2005 11:50 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Quote:
So, what I am woundering is has anyone ever had this problem in Hibernate where we want to map one field to different types depending on the database we are connecting to and how the problem was solved.


This is what Hibernate is all about. You define the mapping file and say you want a Boolean, and Hibernate knows how to work with Booleans in the different dbs. I've not used DB2, but what I would do is use SchemaExport to generate DDL in DB2 and see what the Boolean property gets created as in the db and use that.


Top
 Profile  
 
 Post subject: Hibernate HBM File - Map one field to multiple data types
PostPosted: Wed Jun 22, 2005 12:25 pm 
Newbie

Joined: Mon May 30, 2005 5:52 am
Posts: 15
Thanks for the reply.

The problem lies in the fact that we have a Boolean data type in Posgress and no boolean data type in DB2. So, how to handle this is a problem.

I have gone down the route of trying to find out what data type can be converted into Boolean and using one of those data types that is available in DB2 and Postgress as the boolean database type. In this case char(1) seems to come up trumps. Hibernate converts it to boolean coming from the database and back to char 1 going to the database (accoring to section 6.1.1 in Hibernate in Action). I hope this one works.

I will post back with progress.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 11:16 am 
Newbie

Joined: Mon May 30, 2005 5:52 am
Posts: 15
This worked for me.

Basically we now use char(1) 't', 'f' in the database instead of Boolean. In the HBM file it means that the data type becomes true_false. This will work for all databases.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.