-->
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.  [ 3 posts ] 
Author Message
 Post subject: Primitive field for nullable column
PostPosted: Wed Jun 14, 2006 12:46 pm 
Beginner
Beginner

Joined: Tue May 02, 2006 10:04 am
Posts: 29
Hi,

I have a boolean field in the Java class that I want to map to a nullable
numeric column (whose value would be 0 or 1). Hibernate does not allow
primitive types to be mapped to nullable columns. I use the default
setting in Hibernate where the fields are accessed as properties, i.e. by settters and getters. So, I want to create two sets of setters and getters - one
for Hibernate and the second one for the application's internal use, like this.

Code:

private boolean foo;

....

public boolean isFoo() {
    return this.foo;
}

public void setFoo(boolean foo) {
   this.foo = foo;
}

public Boolean isFooObj() {
  return new Boolean(this.foo);
}

public void setFooObj(Boolean fooObj) {
  this.foo = fooObj.booleanValue();
}



My question is: is this workable? Are there other ways to accomplish
the same goal?
Thanks.


Top
 Profile  
 
 Post subject: Use the acces tag property
PostPosted: Wed Jun 14, 2006 3:31 pm 
Newbie

Joined: Tue Jun 06, 2006 7:33 pm
Posts: 11
Another solution is that you preserve your getter and setter and in the maapinf file you declare the acces property at field...

Hibernate Doc:

Quote:
The access attribute lets you control how Hibernate will access the property at runtime. By default, Hibernate
will call the property get/set pair. If you specify access="field", Hibernate will bypass the get/set pair and access
the field directly, using reflection. You may specify your own strategy for property access by naming a
class that implements the interface org.hibernate.property.PropertyAccessor.



If I understand your data base column must accept the null value:
Code:
private Boolean foo;

...
   
public boolean isFoo() {
  if (null!=foo) {
     return foo.booleanValue();
  }
else return false; //arbitrary choice
    
}

public void setFoo(boolean f) {
  this.foo = new Boolean(f);
}


Quote:
In the mapping file you declare the property:


Code:
<property name="foo" column="foo_column" type="java.lang.Boolean"  not-null="false" access="field"/>


Top
 Profile  
 
 Post subject: Re: Use the acces tag property
PostPosted: Wed Jun 14, 2006 3:53 pm 
Beginner
Beginner

Joined: Tue May 02, 2006 10:04 am
Posts: 29
Thanks, this is what I was looking for.


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