-->
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: why Lifecycle interface's onLoad has the id as a parameter?
PostPosted: Thu Feb 22, 2007 7:30 am 
Newbie

Joined: Thu Feb 22, 2007 7:14 am
Posts: 4
we use the Lifecycle interface's onLoad method
i need to ask a question about this method
why this method has 2 param's passed to it.

in onSave, onUpdate and onDelete, they has just 1 parameter passed,
what is the benefit of the second parameter passed for onLoad method,

to explain this, i have written the code of my pojo:

Code:
public class Course implements Lifecycle
{
  private Long id;
  private String name;
  private Long duration;


  public void setId(Long id)
  {
    this.id = id;
  }


  public Long getId()
  {
    return id;
  }

  public void setName(String name)
  {
    this.name = name;
  }


  public String getName()
  {
    return name;
  }


  public void setDuration(Long duration)
  {
    this.duration = duration;
  }


  public Long getDuration()
  {
    return duration;
  }
 
 
  public void onLoad(Session s, Serializable id)
  {
    System.out.println("onLoad.");
    System.out.println("id = " + id);
    System.out.println("this.id = " + this.id);
  }
 
  public boolean onSave(Session s){ }
 
  public boolean onUpdate(Session s){ }

  public boolean onDelete(Session s){  }
}


as you can see the onLoad method has the id of the current pojo as a parameter and also has the same id as field variable in it.
why Lifecycle interface's onLoad has the identifier as a parameter?


Top
 Profile  
 
 Post subject: Please need help
PostPosted: Tue Feb 27, 2007 8:47 am 
Newbie

Joined: Thu Feb 22, 2007 7:14 am
Posts: 4
5 days ago since i have make this topic.
I am really want an answer for this question as soon as possible if any one know the answer or know where can i find the answer I'll appreciate him.

Thanks any way
;)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 9:03 am 
Newbie

Joined: Tue Feb 27, 2007 7:24 am
Posts: 10
At the point where onLoad is called, the entity object is empty. If you didn't have the primary key passed to the onLoad handler, how would you know which object it is requesting to load? I think this means that you can load the details for the object from some sort of cache if you like and skip the database access.

Does that help?

David


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 9:17 am 
Newbie

Joined: Thu Feb 22, 2007 7:14 am
Posts: 4
at the hibernate java docs
http://www.hibernate.org/hib_docs/v3/api/org/hibernate/classic/Lifecycle.html
you will see that :
onLoad: called just after an object is loaded

that mean the onLoad method is called after retrieving the data if you modify the code in onLoad as following:
Code:
  public void onLoad(Session s, Serializable id)
  {
    System.out.println("onLoad.");
    System.out.println("id = " + id);
    System.out.println("this.id = " + this.id);
    System.out.println("this.name = " + this.name);
    System.out.println("this.duration = " + this.duration);
  }


the previouse code will executed successfully and print the name and duration specified. That means this POJO is not empty.

Am I right?
any way thanks for your help :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 9:44 am 
Newbie

Joined: Tue Feb 27, 2007 7:24 am
Posts: 10
Ah, yes, I was looking at the documetnation in "Pro Hibernate 3", but I think I got confused with the other onLoad method on interceptors.

I'm afraid I don't know why it passes the Id in there.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 9:48 am 
Newbie

Joined: Thu Feb 22, 2007 7:14 am
Posts: 4
Thanks davidsheldon
Any way your reply made me happy
Thanks again :)


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.