-->
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.  [ 4 posts ] 
Author Message
 Post subject: Representing a boolean using a char(1)
PostPosted: Tue Feb 10, 2004 10:00 am 
Newbie

Joined: Mon Feb 09, 2004 10:00 pm
Posts: 15
What is the best suggestion for a mapping file when the domain model contains a boolean value, but it is stored in the database as a single char. This is one of those cases when 'Y' = true and 'N' = false.

So far I have created two methods in my domain model:

Code:
public boolean getValueBoolean() {
   return "Y".equals(_value);
}

public void setValueBoolean(boolean value) {
    _value = value ? "Y" : "N";
}

private String getValue() {
   return _value;
}

private void setValue(String value) {
   _value = value;
}


And I map the _value instance variable in my HBM file. Seems like I may be missing something built-in to Hibernate to enable these types of conversions that I haven't come across in the documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 10:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Just map the boolean property using type=yes_no and set

<property name="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>

in your hibernate configuration file.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 10:05 am 
Senior
Senior

Joined: Sun Jan 04, 2004 2:46 pm
Posts: 147
Use a yes_no type in the relevant properties.

You can also add this to hibernate.properties

Code:
hibernate.query.substitutions false='N', true='Y'


which allows you to use true and false in your HQL queries.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 10:07 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Yes, good point, Myk, if you want to use true/false in your HQL queries, you should rather do this:

<property name="hibernate.query.substitutions">true 'Y', false 'N', yes 'Y', no 'N'</property>


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