-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Prevent Hibernate initialising Boolean to false in jdk1.5
PostPosted: Thu Jul 14, 2005 8:13 am 
Beginner
Beginner

Joined: Wed May 04, 2005 5:17 am
Posts: 40
Hi all,

I am using jdk1.5 and Hibernate3. I have an object with a java.lang.Boolean value which is mapped as follows:

Code:
<property name="hasPages" column="HAS_PAGES" type="java.lang.Boolean"/>


The value in the database is null, but the value on the object is false, I presume because of autoboxing.

How can I prevent this initialisation. I need to see if the value has been set in the DB, and therefore I need the field to be null.

Any ideas?

Thanks,

Colin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 10:27 am 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
What is your datatype on your Java object, boolean or Boolean?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 10:46 am 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Ok, it's early, lol.

So Boolean really get initialized to false under 1.5? I find that very strange, which is why I initially assumed your java object was using a boolean.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 7:51 am 
Beginner
Beginner

Joined: Wed May 04, 2005 5:17 am
Posts: 40
Yeah, it is kinding of driving me nuts :)

Any idea how to prevent it?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 1:47 pm 
Newbie

Joined: Thu Jun 03, 2004 10:29 am
Posts: 13
It is because of autoboxing. I am assuming that if the column returns a null that you still want the coresponding object to also be null. In that case you would have to create a custom user type.

It's quite simple. You don't have to update the POJO. But you will have to update the mapping file in this case. Inside the user type class that you create, you will define the behavior that you want.

I had to create these types for Nullable int primative and the like.

Here's an example for boolean:

package com.sobetech.hibernate.type.primative;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

/**
* com.sobetech.hibernate.type.NullableBoolean
*
* @author John Murray - Sobetech
*
* Creation Date: Aug 24, 2004
*/
public class NullableBoolean implements UserType
{
/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
*/
public Object assemble(Serializable cached, Object owner) throws HibernateException
{
return cached;
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
*/
public Serializable disassemble(Object value) throws HibernateException
{
return (Serializable)value;
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
*/
public int hashCode(Object object) throws HibernateException
{
return object.hashCode();
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
*/
public Object replace(Object original, Object target, Object owner) throws HibernateException
{
return original;
}

/* (non-Javadoc)
* @see org.hibernate.UserType#sqlTypes()
*/
public int[] sqlTypes()
{
int[] typeList =
{Types.BIT};
return typeList;
}

/* (non-Javadoc)
* @see org.hibernate.UserType#returnedClass()
*/
public Class returnedClass()
{
return Boolean.class;
}

/* (non-Javadoc)
* @see org.hibernate.UserType#equals(java.lang.Object, java.lang.Object)
*/
public boolean equals(Object x, Object y) throws HibernateException
{
return Hibernate.BOOLEAN.isEqual(x, y);
}

/* (non-Javadoc)
* @see org.hibernate.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
*/
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException
{
Boolean thisBoolean = (Boolean)Hibernate.BOOLEAN.nullSafeGet(rs, names[0]);
if(thisBoolean == null) return new Boolean(false);
return thisBoolean;
}

/* (non-Javadoc)
* @see org.hibernate.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
*/
public void nullSafeSet(PreparedStatement ps, Object value, int index)
throws HibernateException, SQLException
{
Hibernate.BOOLEAN.nullSafeSet(ps, value, index);
}

/* (non-Javadoc)
* @see org.hibernate.UserType#deepCopy(java.lang.Object)
*/
public Object deepCopy(Object value) throws HibernateException
{
return value;
}

/* (non-Javadoc)
* @see org.hibernate.UserType#isMutable()
*/
public boolean isMutable()
{
return Hibernate.BOOLEAN.isMutable();
}
}


Top
 Profile  
 
 Post subject: Re: Prevent Hibernate initialising Boolean to false in jdk1.
PostPosted: Fri Jul 15, 2005 3:06 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
yatesco wrote:
Hi all,

I am using jdk1.5 and Hibernate3. I have an object with a java.lang.Boolean value which is mapped as follows:

Code:
<property name="hasPages" column="HAS_PAGES" type="java.lang.Boolean"/>


The value in the database is null, but the value on the object is false, I presume because of autoboxing.
Colin


Are you *sure* of that. I've just tested that on the Annotations project and I don't have any problem retrieving a null object.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 3:20 pm 
Beginner
Beginner

Joined: Wed May 04, 2005 5:17 am
Posts: 40
Excellent!

I will try it when I get back to work.

Many thanks.

Col

P.S. Seems a bit of an oversight on hibernates part. I would have though a flag on the mapping to turn it off or on would be best :(


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 16, 2005 10:59 am 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
I don't experience this behaviour. I have java.lang.Booleans in my classes which can store and retrieve nulls just fine.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 6:30 am 
Beginner
Beginner

Joined: Wed May 04, 2005 5:17 am
Posts: 40
Which version are you guys using?

I am using 3.0.5, am trying 3.1 alpha 1.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 12:22 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Tested on Hibernate 3.1 HEAD, but I don't see any reason for a change in this area since a while.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 1:44 pm 
Beginner
Beginner

Joined: Wed May 04, 2005 5:17 am
Posts: 40
Well I will try with 3.1 HEAD.

Is anybody getting nulls on 3.0.5 (apart from me ;))


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 6:13 am 
Beginner
Beginner

Joined: Wed May 04, 2005 5:17 am
Posts: 40
Any progress on this guys? It is still happening with jdk 1.5, hibernate 3.05.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 1:07 pm 
Newbie

Joined: Thu Jun 03, 2004 10:29 am
Posts: 13
yatesco,

I wouldn't wait too long for an update. I would just use the custom type. If and when the change gets made, all you'd have to do is update the hbm.xml of the classes that you used the custom type.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 1:10 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i'm quite sure this is simply not possible - but if you still claim it to be so then produce a SIMPLE & SMALL runnable test case (it should be as simple as having a single boolean mapped)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 1:33 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I tested it, it is not true.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.