-->
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.  [ 8 posts ] 
Author Message
 Post subject: Mapping of boolean to 'Y' and 'N'
PostPosted: Tue Apr 13, 2004 9:40 am 
Newbie

Joined: Fri Jan 30, 2004 1:59 am
Posts: 10
I want to map a database field VARCHAR(1) with values 'Y', 'N' (and in some cases null) to a boolean (and java.lang.Boolean). When hibernates reads these fields it works fine: 'Y' is tranformed to true and 'N' to false. But when inserting a new record: hibernates uses 1 and 0 in stead of 'Y' and 'N'.

Is there a way to set this up the way i want?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 12:59 pm 
Newbie

Joined: Thu Nov 13, 2003 12:03 pm
Posts: 18
Location: France
Maybe more details would help...
have you set type to "yes_no" ?
http://www.hibernate.org/hib_docs/refer ... pping-s2-2


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 6:30 pm 
Newbie

Joined: Fri Jan 30, 2004 1:59 am
Posts: 10
Thank you for your reaction. I am testing it now but a quick reaction i can give already. yes_no will not work for java.lang.Boolean.

For example this mapping:
Code:
<property name="garden" column="garden_ind" type="yes_no" not-null="false">
  <meta attribute="field-description">Does the Listing have a garden? If you don't know, set this (or leave it) null.</meta>
</property>


The code generator will generate a boolean member, and not java.lang.Boolean. So i cannot use yes_no there.

I am trying yes_no for boolean right now (so where not-null="true"...) - i will keep you informed if this works, but i am still looking for a mapping solution for java.lang.Boolean!

You asked for some more details. What i want is the database field garden_ind to have the values: 'Y', 'N' and null. I want Hibernate to translate it to their appropriate Boolean value. Retrieving is not a problem, but when I insert a new record, garden_ind will be set to 1, 0, or null and thereby introduce a problem with the legacy software.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 6:43 pm 
Newbie

Joined: Fri Jan 30, 2004 1:59 am
Posts: 10
yes_no works for boolean. Thank you for the tip!

For others who might try to use it: i use MySQL. MySQL has a nice feature that when you insert 'No' into a VARCHAR(1) it will be stored as 'N'! So - i am not sure if this solution will work for other databases (i guess that most databases will produce an error when you try to insert 'No' in a VARCHAR(1) field).

Anyway - i still need a solution for java.lang.Boolean. Is there a yes_no_null kind of solution?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 14, 2004 6:55 am 
Newbie

Joined: Fri Jan 30, 2004 1:59 am
Posts: 10
I have been looking/googling again - but i don't think the java.lang.Boolean works properly in Hibernate. I mean this setting:
Code:
<attribute name="QuerySubstitutions">true 'Y', false 'N'</attribute>

only works when retrieving by Hibernate.

I have solved the problem by writing specific setters and getters for java.lang.Boolean. These setters/getters replace the hybernate setters and getters - which are implemented as String now.

For example:
Code:
public Boolean getGarage() {
if (getGarageInd() == null)
   return null;
else
   return Boolean.valueOf(getGarageInd().equals("Y"));
}


In this example getGarageInd() is generated by Hibernate - but not used in the application.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 14, 2004 8:36 am 
Newbie

Joined: Sun Apr 04, 2004 6:19 am
Posts: 15
Quote:
MySQL has a nice feature that when you insert 'No' into a VARCHAR(1) it will be stored as 'N'!


People must be shot for such "nice features".


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 14, 2004 10:54 am 
Regular
Regular

Joined: Fri Jan 16, 2004 4:48 am
Posts: 56
Or you can use the hibernate properties for global substitutions:

hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N'


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 15, 2004 10:20 am 
Newbie

Joined: Fri Jan 30, 2004 1:59 am
Posts: 10
I agree, I put an ! after my nice feature sentence, trying to put it as a cynical remark...

hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N' as i posted before - will NOT work.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.