-->
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.  [ 6 posts ] 
Author Message
 Post subject: Return "empty" value instead of null
PostPosted: Thu Jul 01, 2004 6:55 pm 
Beginner
Beginner

Joined: Tue Mar 23, 2004 8:40 pm
Posts: 22
Folks,

Is it possible to configure hibernate to return "empty" value instead of null for retrieval of component even if it is null in the persistence ?

<!--
example hbm file
-->
<class name="test" ...>
...
<component name="address" class="com.example.Address">
<property name = "line" type = "string"/>
<property name = "city" type = "string"/>
</component>
...
</class>

<!--
Expected value from retrieval for both line and city are null in the persistence
-->
empty com.example.Address (e.g. new Address())

Thanks in advance!

Chris


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 02, 2004 5:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
No, this is not possible.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 25, 2005 10:42 am 
Regular
Regular

Joined: Mon Sep 20, 2004 8:42 am
Posts: 58
Location: Boston, US
It would be useful to have a way to express that we want the component object to be an empty object (new Address()) instead of null when all the component fields are null.

In my specific use case, I am using Spring MVC to bind form data values to the domain classes and I have something like

Code:
<spring:bind path="companyForm.company.address.city">
  <input name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}"/>" />
</spring:bind>


In my Company class, is specifically declare the Address member as

Code:
class company {
  private Address address = new Address();


However since objects loaded from Hibernate set the address composite member to null, I end up getting NullPointerExceptions.

With hibernate setting the component member to null despite me having declared the address member as new Address() I now have an inconsistency introduced.

Using a user type for this is going to be complex as my current component has 'many-to-one' declarations which is nicely handled by hibernate when using a 'component'. Using a User type for handling a many-to-one mapping would require the UserType implrmentation to query the database and replicate logic that hibernate already handles.

Thanks,
Sanjiv


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 25, 2005 11:18 am 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
Perhaps you could handle this in your JavaBean instead. You could implement some logic in the set method that replaces the property with an "empty" object if Hibernate passes in null. Something along those lines.

Also, have you looked at CompositeUserType?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 11:31 am 
Newbie

Joined: Fri Mar 11, 2005 10:57 am
Posts: 2
Location: Netherlands
I had the same problem and I handled it in the JavaBean as rhasselbaum suggested: the set method replaces the null passed by Hibernate by an empty object.

One problem with this approach though: now Hibernate thinks the entity object is dirty, because null is not equal to the empty object I replaced it with in the setter. So the entity gets updated even if no changes are made. For this particular project this is a big problem, because the database tables are audited. Every update generates a new row in the corresponding history table.

This problem I solved by implementing a new CompositeUserType that considers null equal to an empty object. A much nicer approach would be to subclass ComponentType itself and overrule the isDirty method, but it does not seem possible to do that - how to make Hibernate use the new class?

Another feasable approach is to write an Interceptor that implements findDirty.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 22, 2005 11:39 am 
Regular
Regular

Joined: Mon Sep 20, 2004 8:42 am
Posts: 58
Location: Boston, US
Quote:
One problem with this approach though: now Hibernate thinks the entity object is dirty, because null is not equal to the empty object I replaced it with in the setter. So the entity gets updated even if no changes are made


Excellent point. I had used rhasselbaum's suggestion too of setter replacing null with empty object. Will switch to using a custom CompositeUserType.

Thanks,
Sanjiv


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