-->
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 a SMALLINT to a boolean
PostPosted: Thu Sep 25, 2003 11:16 am 
Newbie

Joined: Thu Sep 25, 2003 11:09 am
Posts: 6
Location: Auburn Hills, MI, USA
Hello all,

I have a field in an already existing table which is of type SMALLINT (a DB2 database).
I would like to map this to a boolean property in my Java class. Is this possible? If yes, how do I go about it?

Thanks,
Balaji


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 11:20 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
I have a method I use like this:

Code:
private boolean convertFlag (int value) {

    if (value==0) {
        return true;
    }
    else return false;
}

I use this with any database system for binary storage.

-G


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 11:23 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Sorry, forgot to mention, that you can make this a little more robust like this:

Code:
private boolean convertFlag (int value) throws MyException {

    if (value==0) {
        return true;
    }
    else if (value==1) {
        return false;
    }
    else {
        throw new MyException("Tried to convert a non-binary type.");
    }
}


:)

-G


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 11:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No! You don't pollute your domain model with this kind of code. Use a UserType.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 11:42 am 
Newbie

Joined: Thu Sep 25, 2003 11:09 am
Posts: 6
Location: Auburn Hills, MI, USA
Thanks Brannor and gavin,

I saw some code in the forum using the style Brannor suggested.. but still had some concerns (polluting the domain..). I will go ahead with gavin's idea. My domain model will now be littered with this boolean (user type) now.

Thanks to both of you.

-Balaji


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2003 1:10 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
gavin wrote:
No! You don't pollute your domain model with this kind of code. Use a UserType.

<cowers in the corner.>
Ok, I hadn't read the short section on UserType's in the reference material. Would you mind giving a short example of type conversion using net.sf.hibernate.UserType, or directing us to one? :)

Quote:
4.2.4. Custom value types

It is relatively easy for developers to create their own value types. For example, you might want to persist properties of type java.lang.BigInteger to VARCHAR columns. Hibernate does not provide a built-in type for this....


Is this the section?

-G


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2003 1:24 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Here's the example mentioned in that section: http://cvs.sourceforge.net/viewcvs.py/hibernate/Hibernate2/src/net/sf/hibernate/test/DoubleStringType.java?rev=1.6&only_with_tag=v21branch&view=auto


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2003 1:26 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Thanks Steve.

-G


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.