-->
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.  [ 10 posts ] 
Author Message
 Post subject: get methods to return an object instead of null
PostPosted: Wed Oct 29, 2003 11:33 am 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
Hi,

Is there a way to tell Hibernate to return an (empty) object instead of null if all the object's properties are null?

Thanks,

--steve


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 11:34 am 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
Forgot to mention that the getter retrieves an associated object belonging to a different class.

Thank you again,
--steve


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 11:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Huh? Maybe post some code so we can see what you are asking.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 11:54 am 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
public class MgmtInfo implements Serializable {

/** nullable persistent field */
private com.device.NeUsersAndPasswords neUsersAndPasswords;
.........................................

public com.device.NeUsersAndPasswords getNeUsersAndPasswords() {
return this.neUsersAndPasswords;
}

} // end class

The question, basically is:

How can I make getNeUsersAndPasswords() to return an object instead of null if the object is not found in the database ?

Thx,

--steve p.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 11:55 am 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
public class MgmtInfo implements Serializable {

/** nullable persistent field */
private com.device.NeUsersAndPasswords neUsersAndPasswords;
.........................................

public com.device.NeUsersAndPasswords getNeUsersAndPasswords() {
return this.neUsersAndPasswords;
}

} // end class

The question, basically is:

How can I make getNeUsersAndPasswords() to return an object instead of null if the object is not found in the database ?

Thx,

--steve p.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 12:05 pm 
Newbie

Joined: Wed Sep 10, 2003 3:21 pm
Posts: 18
Location: USA
Make seprate geter and setter for application and separate getter and setter for hibernate. So hibernate will use it's separate setter to set state from database and in application level getter you can put additional business logic.

In apllication level getter:

Code:

public SomeObject getSomeObjectAppLevel()
{
     return (someObject == null)? someThing: someObject;
}


Thanks,

Giedrius


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 12:09 pm 
Newbie

Joined: Wed Sep 10, 2003 3:21 pm
Posts: 18
Location: USA
I will sugest to use in this case NullObject pattern. So your getter always returns something. Because checks for null's can be anoying.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 12:12 pm 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
trumpc wrote:
I will sugest to use in this case NullObject pattern. So your getter always returns something. Because checks for null's can be anoying.


I agree. I hoped that Hibernate could be instructed to automatically return an (empty) instance of the return type class of the getter, instead of returning null and then me having to check for nulls.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 12:16 pm 
Newbie

Joined: Wed Sep 10, 2003 3:21 pm
Posts: 18
Location: USA
Hibernate can do it as well but then you need CustomeType for that.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 29, 2003 12:19 pm 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
I see. Thanks!

--steve


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