-->
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: Initializing child Objects to non null values?
PostPosted: Wed Jan 07, 2004 2:33 pm 
Regular
Regular

Joined: Wed Dec 17, 2003 1:58 pm
Posts: 102
Hi I was wondering if it is possible to initialize child objects to non null values if they don't exist or their records are null?

Example:
Credit Union Object
{
Location location;
}

The location may actually not exist in the database, however for the sake of my webapp it calls cuobject.location.name, and if the location object gets loaded as null I'll get a null pointer exception, or I'll have to check to make sure location isn't null everywhere I want to check inside it. So is it possible to load the location object like Location location = new Location(); and its inner values just be null or whats the best way to do this?

Thanks,
David


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 07, 2004 8:34 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I'm not sure but I think it is not doable.

If you will be returning new Location from getter (in the case actual location is null), or you will be substitute Location in setter (in the case parameter is null), sooner or later Hibernate will attempt to save your object to the database because from Hibernate's point of view there was a change - Location was null and it is not null anymore. This is because Hibernate uses the same getter as your code to obtain location.

Theoretically, if you tell Hibernate to use direct access to the location field (do not use getter/setter) and provide getter like

Code:
public Location getLocation()
{
    return (location != null) ? location : new Location;
}


it will solve your particular problem and will not cause Hibernate to save transient locations. But this approach really sucks because each time you call getLocation you will be getting different objects. And you may forget about something like

Code:
obj.getLocation().setState("xxx");


To me it is better to check if returned location is null or not at every place neede.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 7:11 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Quote:
But this approach really sucks because each time you call getLocation you will be getting different objects.


You can add an unmapped getter checking the null condition and returning a "cached' new object.

_________________
Emmanuel


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.