-->
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.  [ 4 posts ] 
Author Message
 Post subject: Empty component... a good practice ?
PostPosted: Wed Jun 27, 2007 2:45 pm 
Newbie

Joined: Fri Feb 10, 2006 2:20 pm
Posts: 9
Is it a good practice to return an empty component from the getter instead of null when all the members are null ?

Hibernate will return a null object but it's easier for me to work with non-null component if empty.

Is there a limitation if I do like this ?

Code:
@Entity
public class ComponentTest {

private Component component;

public Component getComponent() {
   if(component == null) component = new Component();
   return component;
}

}


Thanks...

_________________
Cédric
http://surunairdejava.blogspot.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 4:32 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Well, I guess you're doing that because of some use of your POJO in a JSP or something like that... I find it so despairingthat one has to modify/pollute the domain class so as to deal with a presentational problem...

For your problem, I guess Hibernate won't have any problem to understand your component is still a null one of every properties on it stay null.

So, if I were right above, apart from a big anti-pattern in my point of view (modify the domain class for presentational constraints...), this should not be a problem.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 4:49 pm 
Newbie

Joined: Fri Feb 10, 2006 2:20 pm
Posts: 9
Yes, you're right it's for my jsps :(

But even in my business layer, it's simplier to work with empty component than check each time I access them if they're not null.

From an Hibernate point of view, when I store an object that is identical to the one in DB but that has empty components instead of null components, maybe Hibernate will execute an update query (with no values) because empty != null.
I have to run test cases to validate this...

_________________
Cédric
http://surunairdejava.blogspot.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 5:18 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
thiebal wrote:
But even in my business layer, it's simplier to work with empty component than check each time I access them if they're not null.


IMO, it shouldn't. For example, Address is often mapped as a component. I find it a lot more easy to understand that, say, customer.getAddress() returns null because this customer has no address submitted yet instead of an anemic instance of Address.

Still imo, moreover, the thing is I think that the code that uses your API shouldn't have to know if some property is referencing a component or a real entity.

With this special component handling, customer.getAddress() would for example never be null, but customer.getBoughtItems() could be although this customer has neither Address nor bought items... I find it not satisfying 'cause there's no consistency between both usages.

Matter of taster, I suppose :-).

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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