-->
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.  [ 13 posts ] 
Author Message
 Post subject: is @OneToOne Always Eager fetch
PostPosted: Mon Nov 23, 2009 9:14 am 
Beginner
Beginner

Joined: Sat Nov 07, 2009 10:31 am
Posts: 22
The entity mapped as @OneToOne is loading always eagerly even though we did fetchType=Lazy

if i do optional =true then its not loading when i load parent entity but problem is that i can load it lazily whenever i need in transaction .

i can do join fetch of that entity but what if such entity is really huge bacame performance issue ..

is there any solution for this in jpa ...?
or it is disadvantage in JPA...
(in this forum someone said we cant achive this becoz some proxy reason)
but i heard in hibernate with constrinat element we can acheive this ..
then why can't we do the same here.. i m confused....!!!
its very urgent ..
some expert in jpa.. please help.. me..
i stucked here.. with performance issue..


Thanks in Advance

_________________
Jana


Top
 Profile  
 
 Post subject: Re: is @OneToOne Always Eager fetch
PostPosted: Mon Nov 23, 2009 9:25 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Quote:
(in this forum someone said we cant achive this becoz some proxy reason)


Right, here is a good thread about that topic.
And here is a full explanation.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: is @OneToOne Always Eager fetch
PostPosted: Mon Nov 23, 2009 9:27 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
To clarify, it is possible to have one-to-one-assiocations loaded lazily, just look at the link I posted above.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: is @OneToOne Always Eager fetch
PostPosted: Mon Nov 23, 2009 10:52 am 
Beginner
Beginner

Joined: Sat Nov 07, 2009 10:31 am
Posts: 22
Thanks..
i have gone through the links what u given..
do u mean that .. we cannt lazy load oneToone association.
Then its something bad right..
how can i overcome this problem..please
clarify the same..

Thanks

_________________
Jana


Top
 Profile  
 
 Post subject: Re: is @OneToOne Always Eager fetch
PostPosted: Mon Nov 23, 2009 11:00 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
No, I do not mean, that you cannot lazy load a oneToOne (as clarified).

You can for example use a JoinColumn (not the PK) or set optional to false, so that hibernate does not need to check if the proxy could be null.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: is @OneToOne Always Eager fetch
PostPosted: Tue Nov 24, 2009 12:18 am 
Beginner
Beginner

Joined: Sat Nov 07, 2009 10:31 am
Posts: 22
joinColumn ...?

u mean to say i should an extra coulumn in this table
but its not possible . i just cannot alter database.
that is my problem. as of now i m doing left join fetch.
but the performance got down..

so please suggest me..

and how can they are doing in hibernate...?

_________________
Jana


Top
 Profile  
 
 Post subject: Re: is @OneToOne Always Eager fetch
PostPosted: Tue Nov 24, 2009 4:20 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
mmerder wrote:
... set optional to false, so that hibernate does not need to check if the proxy could be null.

Thats the other solution, if the association is mandatory. Otherwise it's not possible to prevent the extra select (or join), because of above reasons. I don't know why this had a huge impact on your performance, are there no indices on the keys in the db?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: is @OneToOne Always Eager fetch
PostPosted: Tue Nov 24, 2009 6:18 am 
Beginner
Beginner

Joined: Sat Nov 07, 2009 10:31 am
Posts: 22
Thanks for suggestion i will try with that.

i thought by default optional=false

i have indices in db .but 1 entity has 6 ontToone entities so if i load 1000 main
entitis 1000*6 times database hit is happening.. so only performance problem.

and if do left join fetch.. the no of elements selected will be come more..so ..

any how thanks i will try optina=false .. hope it should avoid loading..
and also i expect to get load whenever i call getter of that entity.

Thanks

_________________
Jana


Top
 Profile  
 
 Post subject: Re: is @OneToOne Always Eager fetch
PostPosted: Tue Nov 24, 2009 6:47 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Don't forget to set it to lazy explicitly. Default is eager.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: is @OneToOne Always Eager fetch
PostPosted: Wed Nov 25, 2009 1:13 am 
Beginner
Beginner

Joined: Sat Nov 07, 2009 10:31 am
Posts: 22
Thanks!
It is working when i do fetchType=LAZY and optional =false only when that oneToone element
of other side has @PrimaryKeyJoinColumn (primary key as this primary key)

but it is not working when the is @JoinColumn at other side ..

for ex:
Code:
Class A{
B b ;
C c;
@OneToOne(mappedBy = "a",optional=false,fetch=FetchType.LAZY)
public B getB(){ return b;}

@OneToOne(mappedBy = "a",optional=false,fetch=FetchType.LAZY)
public C getC(){ return c;}
}

Class B{
A a;
@OneToOne(fetch = FetchType.LAZY,optional=false)
@PrimaryKeyJoinColumn
public A getA(){ return a;}
}
Class C{
A a;
@OneToOne(fetch = FetchType.LAZY,optional=false)
@JoinColumn(name = "A_ID")
public A getA(){ return a;}
}

Quote:
When i load A then B is becoming Lazy as you said.. but C is still eagerly loading . how to avoid it.please suggest me



Thanks

_________________
Jana


Top
 Profile  
 
 Post subject: Re: is @OneToOne Always Eager fetch
PostPosted: Wed Nov 25, 2009 5:18 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
You are right, this is probably due to proxy implementation limitation. In order to create a (to-one-)proxy, hibernate needs to know the id of the referenced object. So it makes a select on the other table (the one with the join-column) to retrieve the id.

So you have to use primary key join column in order to do lazy loading.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: is @OneToOne Always Eager fetch
PostPosted: Wed Nov 25, 2009 5:42 am 
Beginner
Beginner

Joined: Sat Nov 07, 2009 10:31 am
Posts: 22
okay .. thanks alot.. no other go then... really bad thing here in jpa i feel...

any how thanks alot..

_________________
Jana


Last edited by janu019 on Wed Nov 25, 2009 5:43 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: is @OneToOne Always Eager fetch
PostPosted: Wed Nov 25, 2009 5:42 am 
Beginner
Beginner

Joined: Sat Nov 07, 2009 10:31 am
Posts: 22
thanks...

_________________
Jana


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