-->
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.  [ 5 posts ] 
Author Message
 Post subject: @manytoone not lazy loaded - always additional select(s)
PostPosted: Tue Jun 12, 2012 10:58 am 
Newbie

Joined: Tue Jun 21, 2011 5:33 am
Posts: 7
Hi!
I am working on a problem for a while, and wonder if anyone can provide some hint.
I have an entity class (ChildObject), with a many-to-one relation to another entity (ParentObject) in the following way

Code:
   @ManyToOne(fetch=FetchType.LAZY)
   @JoinColumn(name="m_rl_GO_id_sp_id", referencedColumnName="GO_id_sp_id")
   private ParentObject parentObject;

I have the problem that hibernate ignores lazy loading and always creates additional select statements, although I am not accessing the object.
Moreover, I have a methods fetching all child objects and there parents:

Code:
SELECT c FROM child JOIN FETCH c.parentObject p

and even in this case the join is not working, but instead the parentObject are read with selects.
There are some similar topics in the forum already but none that seems to provide a solution?

Thanks,
Daniel


Top
 Profile  
 
 Post subject: Re: @manytoone not lazy loaded - always additional select(s)
PostPosted: Tue Jun 12, 2012 8:10 pm 
Newbie

Joined: Tue Jun 01, 2010 4:18 am
Posts: 11
daniel.aschauer wrote:
I have the problem that hibernate ignores lazy loading and always creates additional select statements, although I am not accessing the object.

How you querying the child object? Which hibernate version are you using? Normally, if you write (for example):
Code:
List<ChildObject> list = entityManager.createQuery("from ChildObject").getResultList();
for (ChildObject co : list) {
  System.out.println(co.childObjectId);
}

should not invoke additional select for parent object. Could you give more detail for your implementation?

daniel.aschauer wrote:
Moreover, I have a methods fetching all child objects and there parents:
Code:
SELECT c FROM child JOIN FETCH c.parentObject p

and even in this case the join is not working, but instead the parentObject are read with selects. There are some similar topics in the forum already but none that seems to provide a solution?

In general, if you defining "JOIN FETCH" explicitly in select statement it would querying the associated objects, no matter you accessing the associated object or not. Furthermore, hibernate offers fetching strategies to used in your application. Here is good examples described here.

Thanks,
xsalefter


Top
 Profile  
 
 Post subject: Re: @manytoone not lazy loaded - always additional select(s)
PostPosted: Wed Jun 13, 2012 4:37 am 
Newbie

Joined: Tue Jun 21, 2011 5:33 am
Posts: 7
Hi! Thanks for your reply.

Regarding versions, I currently use 4.2.0.Final, but also switched back trying to (3.5.6?), with no effect.

Quote:
How you querying the child object? Which hibernate version are you using?

I do exactly what you wrote:
Code:
Query q = entitymanager.createQuery("SELECT c FROM ChildObject c ")...

but as well read it in a reference from other entity (e.g. a):
Code:
a.getChildObject()
but I always get the select statements.

Quote:
In general, if you defining "JOIN FETCH" explicitly in select statement it would querying the associated objects

Yes, of course I use the fetch as I want to get the objects, I am just suprised that i get these select statemnents instead of a join (as I use a join)
I even tried an additional annotation to influence the fetching:
Code:
@Fetch(FetchMode.JOIN)

but with no effect, I get those additional select statements for both cases.
I found another topic descibing the same problem, whereas the poster points out that it might be related to using a referencedColumnName, and the column is not the primary key of the entity.
@ManyToOne + lazy + referencedColumnName doesn't lazy

Daniel


Top
 Profile  
 
 Post subject: Re: @manytoone not lazy loaded - always additional select(s)
PostPosted: Wed Jun 13, 2012 4:57 am 
Newbie

Joined: Sun Jan 24, 2010 4:20 pm
Posts: 13
Quote:
I found another topic descibing the same problem, whereas the poster points out that it might be related to using a referencedColumnName, and the column is not the primary key of the entity.
@ManyToOne + lazy + referencedColumnName doesn't lazy


My problem (the link you pointing to) is strictly related to using referencedColumnName to point to an alternate-key (or natural key if you like) not being the PK(@id).


Top
 Profile  
 
 Post subject: Re: @manytoone not lazy loaded - always additional select(s)
PostPosted: Wed Jun 13, 2012 6:44 am 
Newbie

Joined: Tue Jun 21, 2011 5:33 am
Posts: 7
Hi Andreas!
It is indeed the same problem, as well I referenced a column that is not @id.
After I changed the model now using a referenced column that is used as id, i could get rid of the selects.
I guess the only solution in your case is a workaround, something like: do not annotate Person, but just created_by, and
Code:
@Column(name = "created_by")
private String created_by;

private Person getPerson() {
//Load person here
}

This would work for lazy loading. Sure, if you rather what to fetch them somewhere, this is not working.
Daniel


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