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.  [ 4 posts ] 
Author Message
 Post subject: one-to-one proxies
PostPosted: Tue Nov 01, 2005 4:32 am 
Newbie

Joined: Fri Oct 28, 2005 7:35 am
Posts: 12
Hibernate version: 3

I posted a similar message yesterday titled "Hibernate and Proxies" which has not received any comment, so I will try and refrase it here.

I am trying to set up two classes with a one-to-one relationship between them:

Code:
ClassA
ClassB


ClassA should have a one-to-one two ClassB. Where the relationship should be proxied. However everytime I read an instance of ClassA from Hibernate, it also reads the relationship to ClassB.

In my mapping file for ClassA, I have the following:

<one-to-one name="classB" class="ClassB"
fetch="select" lazy="proxy"/>

Now: when I execute:

Code:
ClassA classA = session.load(ClassA.class, id);
classA.getName();


When #getName() is executed, Hibernate executes the SQL to get the ClassA instance "classA", but also executes a second SQL to get a ClassB instance !
Why is there not a proxy generated and the SQL only executed when I call something like:

Code:
classA.classB;


?

Many thanks
Ian


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 15, 2005 3:54 am 
Newbie

Joined: Wed Oct 26, 2005 7:35 am
Posts: 10
Hibernate does not know the id of class B in the scope of class A. The foreign key is stored in class B.
So it can not make a proxy for it, because hibernate don't know its id
If you want to know the id of class B, you need to do a query on class A. And if you do a query on class A you have class A and there is no need for a proxy anymore.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 15, 2005 10:46 am 
Newbie

Joined: Fri Oct 28, 2005 7:35 am
Posts: 12
Thanks, I figured this out, what I really wanted was a many-one mapping. Everything then worked :o)


Top
 Profile  
 
 Post subject: Re: one-to-one proxies
PostPosted: Fri Dec 09, 2005 6:08 am 
Regular
Regular

Joined: Tue Nov 16, 2004 6:36 pm
Posts: 62
Location: Zürich
Hell Ian,

I had the same problem, then read this thread but was still puzzled.
Then I re-read the ref. manual, and it became clear then:

In the description of the one-to-one tag, it says:

Quote:
constrained (optional) specifies that a foreign key constraint on the primary key of the mapped table references the table of the associated class. This option affects the order in which save() and delete() are cascaded, and determines whether the association may be proxied (it is also used by the schema export tool).


So if you don't set it, the relation may not be proxied.

Set constrained="true" and lazy loading works.
Quite obscure, I would have expected the default to be true instead of false.


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