-->
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: Hibernate is saving unwanted empty records to the database
PostPosted: Wed Sep 05, 2007 5:02 pm 
Newbie

Joined: Wed May 09, 2007 2:25 pm
Posts: 4
Hi everyone,

I have a problem where hibernate is saving empty records to the database. The scenario that I have is - I have an Person object which has a many-to-one relationship with an Address. The Person's constructor creates a new Address object each time a Person is instantiated. The reason I am doing this is because I am using on the UI and the Person's Address cannot be null. From the screen, the user has the option to enter the Address information or not. What I've noticed is that when the Address information is not entered, Hibernate still saves and empty Address record to the database. I am suspected that this is because the Address is not null (even though other attributes within Address are). Is there a way to specify to to Hibernate that if an object is "empty" that it should not get saved to the database?

Thanks in advance!


Top
 Profile  
 
 Post subject: Re: Hibernate is saving unwanted empty records to the databa
PostPosted: Wed Sep 05, 2007 10:42 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I can think of a couple of ways but the best is to handle this in your UI code. Have a variable in your form that is initialized from Person address and is set back to person before saving. Another way would be to write an interceptor that intercepts your Person objects before saving and sets addresses to null when they are empty. This is ugly though. There might be a better way.


Top
 Profile  
 
 Post subject: Null Object pattern?
PostPosted: Thu Sep 06, 2007 1:14 am 
Newbie

Joined: Wed Sep 05, 2007 6:34 pm
Posts: 13
Would the Null Object pattern be of help here?

http://www.developer.com/design/article.php/3697611
http://en.wikipedia.org/wiki/Null_Object_pattern

Instead of inserting new Addresses, Hibernate could associate the Person with a single "null" address of a designated id.

e.g.:
Code:
public class NullAddress extends Address {

    // make this a singleton
    private static NullAddress singleNullAddress = new NullAddress(true);

    private NullAddress(boolean uglyHack) {}  // <-- there's probably a better way
    public NullAddress () { return singleNullAddress; }

    public Long    getId()     { return 0; } // or another special id
    public String  getStreet() { return ""; }
    ...etc...
    public boolean isNull()    { return true; }
}


I haven't actually implemented something like this yet. I assume there would be many more kinks to work out.


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.