-->
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.  [ 12 posts ] 
Author Message
 Post subject: Help: can't get lazy @ManyToOne and @OneToOne
PostPosted: Fri Apr 21, 2006 2:42 pm 
Newbie

Joined: Fri Apr 22, 2005 3:39 pm
Posts: 18
Hello!

I've got trouble with lazy @ManyToOne and @OneToOne: I can't get it lazy!
The childre are lazy indeed, but parent and data are loaded eagerly.
(the commented code has no effect)


@Entity
@Table(name = "NODE_TABLE")
public final class Node {

...
@ManyToOne(/* fetch = FetchType.LAZY */)
@Column(name = "PARENT_ID")
@Index(name = "NODE_PARENT_INDEX", columnNames = { "PARENT_ID" })
public Node getParent() {
return parent;
}

@OneToMany(mappedBy = "parent",/* fetch = FetchType.LAZY*/, cascade = CascadeType.ALL)
@OrderBy("name")
protected Set<Node> getChildrenInternal() {
return childrenInternal;
}
...
@OneToOne(fetch = FetchType.LAZY)
public Data getData() {
return data;
}
}


Please, any help! I need the data lazy because it's it's very heavy!

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 3:01 pm 
Newbie

Joined: Wed Feb 15, 2006 10:17 am
Posts: 13
Location: Rome, Italy
@ManyToOne(/* fetch = FetchType.LAZY */)
@Column(name = "PARENT_ID")
@Index(name = "NODE_PARENT_INDEX", columnNames = { "PARENT_ID" })
public Node getParent() {
return parent;
}

What if you decomment it or set it to fetch=FetchType.EAGER? on the JSR-220 spec it says FetchType.EAGER is the default (anybody correct me if I am wrong).

Alessio Pace.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 3:06 pm 
Newbie

Joined: Fri Apr 22, 2005 3:39 pm
Posts: 18
Hi!
You seem to get me wrong: commenting or uncommenting the code gives the SAME result: no lazy (eager). So again: the problem is that I can't get it lazy even when lazy strategy IS specified...

Michael.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 5:04 pm 
Newbie

Joined: Fri Apr 22, 2005 3:39 pm
Posts: 18
I was able to make the @OneToOne lazy:
the trick is to declare the dependant entity as proxyable like this:

@Entity
@Proxy(lazy=true)
class Data {...}

And, ofcource, set @OneToOne(fetch = FetchType.LAZY)
Looks like just setting fetch = FetchType.LAZY works only for collections. In this case it just creates a collection proxy. But in case of @xxxToOne an entity must be proxied. Looks like we just must allow it being proxied explicitly...


But I can't still make the @ManyToOne sideof the relation be lazy with this approach. May be the problem is that I have bidi one-to-many with self (the same entity) and the same entity is at the same time the "one" and "many" side of relation ... Currently I'm trying:

@Entity
@Proxy(lazy=true)
@Table(name = "NODE_TABLE")
public final class Node {

...
// lazy: PROBLEM, always eager
@ManyToOne( fetch = FetchType.LAZY )
@Column(name = "PARENT_ID")
@Index(name = "NODE_PARENT_INDEX", columnNames = { "PARENT_ID" })
public Node getParent() {
return parent;
}

// lazy collection: ok
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OrderBy("name")
protected Set<Node> getChildrenInternal() {
return childrenInternal;
}
...

// lazy: ok
@OneToOne(fetch = FetchType.LAZY)
public Data getData() {
return data;
}
}


@Entity
@Proxy(lazy=true)
class Data {...}

Still hoping... :-)
Thanks in advance,
Michael.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 10:22 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
proxy = true is the default, so @ManyToOne(fetch=LAZY) and @ManyToOne(fetch=LAZY, optional=false)
will work out of the box, note the optional=false. There is an explaination in the wiki cummunity area on why an one-to-one cannot be lazy unless constrained

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 2:22 am 
Contributor
Contributor

Joined: Wed Jun 28, 2006 2:00 pm
Posts: 3
Location: São Leopoldo, RS
emmanuel wrote:
There is an explaination in the wiki cummunity area on why an one-to-one cannot be lazy unless constrained


Emmanuel, how can I do a "constrained" relation with annotations?

_________________
Diego Pires Plentz


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 19, 2007 9:36 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
@OneToOne(optional=false)

I guess it would be cool to have that in the annotation reference doc as well

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Lazy @ManyToOne not working when specifying referenceCo...
PostPosted: Thu Oct 25, 2007 12:31 pm 
Newbie

Joined: Thu Mar 29, 2007 12:35 pm
Posts: 2
We have found similar behavior when specifying a @ManyToOne with a @JoinColumn on a non-primary key (alternate key) referencedColumnName.

Code:
    @ManyToOne(cascade = {}, fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "FMR_ISR_CUSIP", referencedColumnName = "FMR_ISR_CUSIP")


Our other uses of the @ManyToOne with LAZY fetch type work fine. Is there a workaround for this?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 4:02 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
To work around that, you need to mark the association as lazy no proxy and use bytecode enhanced classes

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 4:09 pm 
Newbie

Joined: Thu Mar 29, 2007 12:35 pm
Posts: 2
I have not been able to find the annotation to mark the association as no-proxy. The fetch variable allows only LAZY or EAGER.

Would I have to use an hbm config to set this? If so, is it possible to only have that in my hbm configuration and not the rest of the metadata?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 11:45 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://www.hibernate.org/hib_docs/annotations/reference/en/html/entity.html#entity-hibspec-singleassoc-fetching

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: Help: can't get lazy @ManyToOne and @OneToOne
PostPosted: Wed Feb 23, 2011 12:18 pm 
Newbie

Joined: Wed Feb 23, 2011 12:13 pm
Posts: 1
Make sure your class is not final.

Ramon Havermans


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