-->
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: Lazy One-to-one not working with Hibernate + JPA
PostPosted: Tue Feb 19, 2008 1:12 pm 
Newbie

Joined: Tue Feb 19, 2008 10:47 am
Posts: 4
(versions:
Hibernate 3.2
annotations 3.3.0
entityManager 3.3.1
)

Hi,

I have a class just like that:

public class TbPessoa{
@LazyToOne(LazyToOneOption.NO_PROXY)
@Fetch(FetchMode.SELECT)
@OneToOne(fetch=FetchType.LAZY, optional=true,mappedBy="pessoa")
private TbUsuario tbUsuario;
}

and a
public class TbUsuario{
@JoinColumn(name = "ID_PESSOA", referencedColumnName = "ID_PESSOA")
@OneToOne(fetch=FetchType.LAZY)
private TbPessoa pessoa;
}

But there is nothing that I do to make the relation LAZY.
Every time I find a TbPessoa, the TbUsuario is selected too.

How can I do a lazy one-to-one then?

The 2 classes have default public constructors.
I already tried LazyToOneOption.NO_PROXY and LazyToOneOption.PROXY

My persistence.xml:
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.default_schema" value="dbcorporativo"/>
<property name="hibernate.generate_statistics" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 1:15 pm 
Newbie

Joined: Tue Feb 19, 2008 10:47 am
Posts: 4
I forgot to mention:

TbPessoa{
@Id
@Column(name = "ID_PESSOA", nullable = false)
private String idPessoa;
}

and the ID_PESSOA on TbUsuario is a foreign key.
The key of TbUsuario is
@Id
@Column(name = "ID_USUARIO", nullable = false)
private String idUsuario;

and the gets/sets is there too.

thanks


Top
 Profile  
 
 Post subject: You might want to read this
PostPosted: Tue Feb 19, 2008 1:42 pm 
Newbie

Joined: Thu Feb 14, 2008 1:41 pm
Posts: 14
http://www.hibernate.org/162.html#A3


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 5:38 pm 
Newbie

Joined: Tue Feb 19, 2008 10:47 am
Posts: 4
thank you for your answer.

"if your B->C mapping is mandatory (constrainted=true), Hibernate will use proxy for C resulting in lazy initialization."
I think it means that, when I put the optional=false, Hibernate will use lazy.

But, even when I put optional=false
@OneToOne(fetch=FetchType.LAZY, optional=false,mappedBy="pessoa")
the TbUsuario IS queried...

what to do then?

But as I mentioned, it is not working...
:(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 5:57 pm 
Newbie

Joined: Thu Feb 14, 2008 1:41 pm
Posts: 14
andrevcf wrote:
I think it means that, when I put the optional=false, Hibernate will use lazy.

But, even when I put optional=false
@OneToOne(fetch=FetchType.LAZY, optional=false,mappedBy="pessoa")
the TbUsuario IS queried...

what to do then?


http://java.sun.com/javaee/5/docs/api/j ... #optional()
Whether the association is optional. If set to false then a non-null relationship must always exist.

When you use the option (optional = false), means the items is required mandatory by the object, you have to use (optional = true).

The document link mentioned previously says, you cannot have LazyLoading for a one-to-one, as the object WILL BE required by Hibernate,

Quote:
But now imagine your B object may or may not have associated C (constrained="false"). What should getCee() return when specific B does not have C? Null. But remember, Hibernate must set correct value of "cee" at the moment it set B (because it does no know when someone will call getCee()). Proxy does not help here because proxy itself in already non-null object.


Also why do u want to lazy load it? Is it a blob that u cannot afford to eager load?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 21, 2008 10:14 am 
Newbie

Joined: Tue Feb 19, 2008 10:47 am
Posts: 4
I can do a EAGER fetch but I don't want to cause of performance problems (it didnt have a LOB, but is from a table very large and I dont need it)

What I mean is that, when I put optional=false (mandatory), "Hibernate will use a proxy" and then I get lazy loading.

But the lazy is not working even when I put optional=false (mandatory).

Thanks.


Top
 Profile  
 
 Post subject: POJO's please
PostPosted: Thu Feb 21, 2008 11:31 am 
Newbie

Joined: Thu Feb 14, 2008 1:41 pm
Posts: 14
Can you paste the POJOs with hbms


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 21, 2008 12:23 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't think it can conceptually work when used on the mappedBy side

_________________
Emmanuel


Top
 Profile  
 
 Post subject: The hibernate doc
PostPosted: Thu Feb 21, 2008 12:34 pm 
Newbie

Joined: Thu Feb 14, 2008 1:41 pm
Posts: 14
The doc says even if u try to lazy load it'll still load (one-to-one) cos if a field and get/set exist for it, hibernate will try to put an object value for it, and if not the real object the proxy will be loaded. Which it either ways will query and fetch.

So technically we don't/cannot have lazy loading in a one-to-one case.


Top
 Profile  
 
 Post subject: Re: The hibernate doc
PostPosted: Thu Feb 21, 2008 2:14 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
varunmehta wrote:
The doc says even if u try to lazy load it'll still load (one-to-one) cos if a field and get/set exist for it, hibernate will try to put an object value for it, and if not the real object the proxy will be loaded. Which it either ways will query and fetch.

So technically we don't/cannot have lazy loading in a one-to-one case.


I don't quite understand the reasoning, but clearly the conclusion is wrong :) Check the wiki page talking about one to one and lazy loading.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 5:55 am 
Newbie

Joined: Wed May 09, 2007 10:36 am
Posts: 4
emmanuel wrote:
I don't think it can conceptually work when used on the mappedBy side


What do you mean by that?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 08, 2008 11:49 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Jux wrote:
emmanuel wrote:
I don't think it can conceptually work when used on the mappedBy side


What do you mean by that?


because a OneToOne(mappedBy) need to read the value of the PK from the mappedBy property, hence the other side has to be loaded.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 11, 2008 4:40 am 
Newbie

Joined: Tue Mar 11, 2008 4:11 am
Posts: 1
this explains how to lazy load a one to one association using a purely hibernate implementation. How do you do this in JPA with Hibernate? How do you define a mandatory one to one mapping in jpa?

TIA


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.