-->
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: Null Object mgmt with Lifecycle Callbacks
PostPosted: Fri Feb 27, 2004 10:58 am 
Newbie

Joined: Fri Feb 20, 2004 12:15 pm
Posts: 8
Hello,

I am using the NullObject Pattern which I find very convenient for not testing for nulls everywhere in my programs. I am using the Lifecycle callbacks to replace my NullObject by null when Hibernate saves / updates my classes and I instantiate a new NullObject when I load from Hibernate.

Here is the code for that.
Code:
// For Operation Null
public boolean onSave(Session s) throws CallbackException {
  if (this.getOperation() instanceof OperationNullImpl)
     this.setOperation(null);
  return false;
}

public void onLoad(Session s, Serializable id) {
  if (this.getOperation() == null)
     this.setOperation(new OperationNullImpl());
}


The problem I have is that when I try to load, in this case, an operation which is null, Hibernate complains that I don't have a Persister for class 'OperationNullImpl'.

Do I really need a mapping for the OperationNullImpl class ? And if yes, how do I map it since I will never have a table in my database for null objects.

lacou


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 12:06 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't think there is a orkaround for that except a private unmapped setter which do the job.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Good idea
PostPosted: Fri Mar 05, 2004 6:29 pm 
Newbie

Joined: Fri Feb 20, 2004 12:15 pm
Posts: 8
The simple things are sometimes not so obvious.

Thanks alot.

So now my instance variables always represents what's on the database. Its on my public getter and setter that I deal with my NullObject and a new private getter/setter has been defined and mapped in the hbm.

Here's the code for those interested

Code:
   public void setOperation(Operation operation) {
      if (operation instanceof OperationNullImpl)
         setPrivateOperation(null);
      else
         setPrivateOperation(operation);
   }

   public Operation getOperation() {
      if (this.operation == null){
         return new OperationNullImpl();
      }
      return operation;
   }
   
   /**
    * for Hibernate.
    * @param operation
    */
   private void setPrivateOperation(Operation operation){
      this.operation = operation;
   }
   
   private Operation getPrivateOperation(){
      return operation;
   }


And now the hbm needs to point to set/getPrivateOperation.

Code:
<many-to-one class="...bo.OperationImpl" name="privateOperation" column="operation" />


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.