-->
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.  [ 7 posts ] 
Author Message
 Post subject: Why ManyToOne optional association can LAZY-load?
PostPosted: Tue Jan 08, 2008 2:29 pm 
Beginner
Beginner

Joined: Tue Jan 08, 2008 2:15 pm
Posts: 22
Hi guys.

I have been browsing this forum, the HB documentation and the HB in action book for a while now and I cannot figure out why ManyToOne optional associations can LAZY-load and OneToOne optional associations cannot.

Yes, I know, I have been visiting the HB wiki as well: Proxying an optional OneToOne association does not make sense since we need to know if it is null or not first and while determining that, why not load the row at the same time.

Yes, same answer in HB in Action on page 566 (FAQ: Does lazy loading of one-to-one associations work?).

But, why an optional ManyToOne association can perform lazy loading then? To me, the same logic than optional OneToOne associations should have been applied to optional ManyToOne associations. What am I missing? Where am I wrong in my assumption?

Nowhere I have been able to find an answer, nowhere the book or HB documentation says the same thing should or should not apply to ManyToOne - while they have the same fundamental problem of a single association being optional.

So I tried and tried things. This thing below will lazy load (i.e. no select is made agains the DB as long as I stick to Ref.id):

@ManyToOne(fetch=FetchType.LAZY, optional=true)
@JoinColumns({
@JoinColumn(name="REF_ID", referencedColumnName="REF_ID")
})
public Ref getRef() {
return this.ref;
}

While this property, on the same parent class: will never lazy load:

@OneToOne(fetch=FetchType.LAZY)
@PrimaryKeyJoinColumns({
@PrimaryKeyJoinColumn(name="PRT_NRI", referencedColumnName="PRT_NRI")
})
public Agent getAgent() {
return this.agent;
}

I am looking for a quick definitive answer to that! Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 2:17 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
I think the main difference between the optional many-one and one-one is this:

In a many-one, what you are dealing with is a Set or a List, you know, Collection Types. So Hibernate can provide wrappers to these known Collection Types. Instead of using the java types directly, Hibernate uses their own Types that implement the same interface. The first time we iterate thru the collection Hibernate knows to load the objects since they are using their version of the Collection types.

On a one-one, what we are dealing with is our own objects.

For example we have Class B inside of Class A. Now what should A.getB() return? Since class A is our own class Hibernate cannot provide a wrapper to it and intercept getB() the first time it's called. It won't know when it's called. So when A was instantiated, it needs to know whether to set B to null or not.

Hope that helps.

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject: Not OneToMany, ManyToOne
PostPosted: Wed Jan 09, 2008 9:31 am 
Beginner
Beginner

Joined: Tue Jan 08, 2008 2:15 pm
Posts: 22
Hi. I am talking about @ManyToOne, not @OneToMany which yes, involves collections, but not the @ManyToOne.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 11:49 am 
Beginner
Beginner

Joined: Tue Jan 08, 2008 2:15 pm
Posts: 22
I think I now understand why:

An optional OneToOne association in class A to class B means that there is a FK in table B to table A (since it is B that is optional).

However, an optional ManyToOne association in class A to class B means that there is a FK in table A to table B (since many A's can point to the same B). HB can tell from the FK columns in table A that there is or not a linkage to table B, so it can lazy load that association from class A.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 12:41 pm 
Beginner
Beginner

Joined: Tue Jan 08, 2008 2:15 pm
Posts: 22
In fact, a FK column is generated in table A on OneToOne association too and it CAN be lazy loaded.

However, when PrimaryKeyJoinColumn is used, no additional FK column is generated in table A of course, and there is no way to lazy load.


Top
 Profile  
 
 Post subject: Re: Why ManyToOne optional association can LAZY-load?
PostPosted: Mon Jul 18, 2011 4:05 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You got it right.
There is a nice explanation here http://community.jboss.org/wiki/SomeExp ... one-to-one

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: Why ManyToOne optional association can LAZY-load?
PostPosted: Sun Sep 11, 2011 8:54 pm 
Newbie

Joined: Sun Sep 11, 2011 5:14 pm
Posts: 3
can somebody explain to me how they make it optional? I don't need it to lazy load, I just want a optional OneToOne

I have tried using @OneToOne(optional=true) but it doesn't work, I still get a foreign key from A to B.

What I'm doing is in class A, I have a @OneToOne mapping to class B. I solved the problem in another way, but I would really love to find out how to make a simple, optional one to one mapping.


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