-->
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.  [ 7 posts ] 
Author Message
 Post subject: why proxy in many-to-one when lazy=false
PostPosted: Thu Jul 14, 2005 8:37 pm 
Newbie

Joined: Thu Jul 14, 2005 8:21 pm
Posts: 9
In this example, SubComponent points to a Component. When I load a SubComponent, I get a proxy for the Component and it's not even initialized. I need the real class.


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
<class
name="com.joe.SubComponent"
table="SUB_COMPONENT"
>

<id
name="id"
column="ID"
type="java.lang.Integer"
>
<generator class="sequence">
<param name="sequence">INTERNAL_ID_SEQ</param>
</generator>
</id>

<property
name="lastModifiedBy"
type="java.lang.String"
update="true"
insert="true"
column="LAST_MODIFIED_BY"
length="100"
not-null="true"
/>


<many-to-one
name="component"
class="com.joe.Component"
cascade="none"
outer-join="auto"
update="true"
insert="true"
lazy="false"
>
<column
name="COMPONENT_ID"
/>
</many-to-one>



</class>

</hibernate-mapping>

Using Hibernate 3.0.2


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 8:41 pm 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
fetch="join"


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 9:55 pm 
Newbie

Joined: Thu Jul 14, 2005 8:21 pm
Posts: 9
fetch="join" didn't work.

I wasn't specific in how I load the object thinking it didn't (shouldn't) matter. In this case the object is being loaded using a org.hibernate.Query object. Using hsql:

select c from Component c where ...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 2:05 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Either put a
Code:
lazy="false"

in the class tag of your Component if you never want to get a proxy.

Or call
Code:
Hibernate.initialize(aComponent)

in order to initialize a proxy in a particular situation.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 4:04 am 
Newbie

Joined: Wed Jul 13, 2005 8:03 am
Posts: 5
Try
Code:
outer-join="true"

to override
Code:
<class name="Component", lazy="true">

declared in Component.hbm.xml


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 15, 2005 12:23 pm 
Newbie

Joined: Thu Jul 14, 2005 8:21 pm
Posts: 9
Adding a class level tag - lazy="false" to Component works as expected. In my case, this is the perfect solution as it will generally pull the Component from second level cache any way, so there is no performance issue.

The join solutions (which I didn't fully try out) would seem to hinder performance because I don't need to read the Component from the db every time and I'll have hundreds of SubComponents attached to the same Component. ...just a thought.

Thanks all for your help!


Top
 Profile  
 
 Post subject: Is there any way to set a global setting for this?
PostPosted: Sat Aug 20, 2005 2:15 am 
Beginner
Beginner

Joined: Sat Aug 30, 2003 1:36 am
Posts: 47
Location: Calgary, AB
It seems that it's trying to proxy everything including simple fields!? after upgrading to hibernate 3 and I would like to it behave like hibernate 2 did. Setting lazy="false" on the class fixes it, but that's a pain to go into all my entities and set that. Why does it proxy basic class fields?

ex:

23:10:02,351 ERROR LazyInitializationException:19 - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:80)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
at com.x.y.MyObject$$EnhancerByCGLIB$$5e1dd2b1.getAmountOnAccount(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.