0

assume i got

 class Father{

private Integer fatherId;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="FATHERID", nullable=false)
public Integer getFatherId() {
    return fatherId;
}    


Set<Son> sons;

@OneToMany(fetch=FetchType.LAZY)
@JoinColumn(name="FATHERID")
public Set<Son> getSons() {
    return airlineFlights;
}
}

and Son

   class Son{


Integer sonId
@Id
@GeneratedValue(generator="identity")
@GenericGenerator(name="identity", strategy="identity")
@Column(name="SONID", nullable=false)
public Integer getSonId() {
    return sonId;
}

 Father father;

     @ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="FATHERID", nullable=false)
public Father getFather() {
    return father;
}

}

now lets assume i got this code :

 Father f = session.load(Father.class, 1);
 f.getSons();

f is a simple POJO and the collection of sons is lazy , so what will happen here ? how is gonig to the database and query for those sons , we are in simple JVM where simple get is called , how does hibernate work there?

EDIT: what happens when getSons(), is called can you give the full cycale ?

yoav.str
  • 1,505
  • 6
  • 31
  • 68
  • possible duplicate of [What is lazy loading in Hibernate?](http://stackoverflow.com/questions/2192242/what-is-lazy-loading-in-hibernate) – BalusC Feb 21 '13 at 19:46

2 Answers2

2

It works the same was as it would in any environment. Hibernate will make a call to the database to retrieve the lazy-loaded collection, assuming an active session exists. Note that if you have a different session active from the one used to load the parent object, that you will need to merge the parent first.

As to how it works - byte code instrumentation. Behind the scenes Hibernate creates a 'proxy' object to wrap your entity, and makes method calls from this proxy as needed to fulfill the caller requests.

Very roughly, something like this happens:

Father (FatherProxy) -> Lazy Collection Reference -> Session -> Load Entities
Perception
  • 75,573
  • 19
  • 170
  • 185
  • what happens when getSons(), is called can you give the full cycale ? – yoav.str Feb 21 '13 at 19:27
  • Can't give the full cycle, its pretty complicated. But I gave a rough sketch of what happens. – Perception Feb 21 '13 at 19:39
  • if you have an articale or some kind of exeplenation pleasee share it eith me , i want to know this by detail – yoav.str Feb 21 '13 at 19:40
  • I linked the Hibernate manual in my answer. You can use that as a starting point. – Perception Feb 21 '13 at 19:44
  • Note that calling just `getSons()` won't fire a query. It will only be fired when you actually need to access an individual item by e.g. `get()`, `iterator()`, etc. Also note that byte code instrumentation doesn't apply on collection properties. – BalusC Feb 21 '13 at 19:45
  • @BalusC - *Also note that byte code instrumentation doesn't apply on collection properties* - care to elaborate? – Perception Feb 21 '13 at 19:55
  • Read the documentation which you linked: *"Hibernate3 supports the lazy fetching of **individual properties**"* Thus, like `father.getId()`, `father.getFirstname()`, etc. For collection properties, Hibernate just sets a custom lazy loading implementation. – BalusC Feb 21 '13 at 19:56
  • @BalusC - ah, so you were pointing out that the link is pointing to the wrong part of the documentation. Thanks, and fixed. – Perception Feb 21 '13 at 20:00
0

You will see 2 queries in the logs. The first one loads up the father, and then because you are calling getSons(), it will go and fetch the sons so you'll see a second query for that.

Uncle Iroh
  • 4,961
  • 4
  • 43
  • 55
  • The `getSons()` call alone won't fire a query. – BalusC Feb 21 '13 at 19:48
  • @BalusC - Bauke -- enlighten me please, or are you saying that it just loads up the wrapper and they would actually have to do something with it, like getSons().get(0) before the query is actually fired? – Uncle Iroh Feb 22 '13 at 21:38
  • @BalusC Oh, lol, just read your comment above on the accepted answer. – Uncle Iroh Feb 22 '13 at 21:46