-->
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: Why the lazy="true" in <class ....> is unava
PostPosted: Fri Jul 07, 2006 12:42 am 
Regular
Regular

Joined: Fri Nov 12, 2004 12:07 am
Posts: 57
Location: Beijing,China
Hi there,

Tow classes C and P and the relation between C and P is many-to-one,the source code as follows:

public class P{
private Long pid;
private String name;
private Set cset=new HashSet();

public Long getPid(){
return pid;
}

public void setPid(Long pid){
this.pid=pid;
}

public String getName(){
return name;
}

public void setName(String name){
this.name=name;
}

public Set getCset(){
return cset;
}

public void setCset(Set cset){
this.cset=cset;
}

}


public class C{
private Long cid;
private String name;
private P p;

.....
.....
.....

public P getP(){
return p;
}

public void setP(P p){
this.p=p;
}


}

p.hbm.xml as follows:
<class name="example.model.P" table="p_tab" lazy="true"> //---L1
<id name="pid" column="p_id" type="long" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="name" column="name" type="string"/>
<set name="cset" inverse="true" lazy="true" cascade="all">
<key column="p_id">
<one-to-many class="example.model.C"/>
</set>
</class>

c.hbm.xml as follows:

<class name="example.model.C" table="c_tab" >
<id name="cid" column="c_id" type="long" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="name" column="name" type="string"/>
<many-to-one name="p" column="p_id" class="example.model.P">
</class>

In Hibernate2.x situation,if set lazy="true" at line L1,When excute

String hql="from C";
Session =HibernateUtil.openSession();
Query query=session.createQuery(hql);
query.list();

The sql statements shown in console like follows:

select c.c_id c_id,c.name name from c_tab;

If don't set lazy="true" at line L1, the sql statements shown like follows:

select c.c_id c_id,c.name name from c_tab;
select p.p_id p_id,p.name name from p_tab where p_id=?

The result is just what i expected.

But in Hibernate3.x,excute the same code, whether lazy="true" exists or not,the result is:

Caused by: java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at java.lang.Class.newInstance0(Class.java:308)
at java.lang.Class.newInstance(Class.java:261)
at org.hibernate.proxy.CGLIBLazyInitializer.getProxy(CGLIBLazyInitializer.java:99)
at org.hibernate.proxy.CGLIBProxyFactory.getProxy(CGLIBProxyFactory.java:47)
at org.hibernate.tuple.AbstractEntityTuplizer.createProxy(AbstractEntityTuplizer.java:372)
at org.hibernate.persister.entity.AbstractEntityPersister.createProxy(AbstractEntityPersister.java:3120)
at org.hibernate.event.def.DefaultLoadEventListener.createProxyIfNecessary(DefaultLoadEventListener.java:232)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:173)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:812)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:782)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:266)
at org.hibernate.type.EntityType.resolve(EntityType.java:303)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:113)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223)
at org.hibernate.loader.Loader.doList(Loader.java:2147)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2026)
at org.hibernate.loader.Loader.list(Loader.java:2021)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:369)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:298)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:137)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1014)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at com.amjn.web.dao.hibernate.user.admin.AdminRoleResourceDAO.remove(AdminRoleResourceDAO.java:30)
at com.amjn.web.bo.user.admin.AdminRoleResourceBO.remove(AdminRoleResourceBO.java:16)
at com.amjn.web.bp.user.admin.AdminRoleResourceBP.clearAndCreate(AdminRoleResourceBP.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.amjn.web.util.sql.DBInvocationHandler.invoke(DBInvocationHandler.java:20)
at $Proxy3.clearAndCreate(Unknown Source)
at com.amjn.web.struts.action.admin.user.admin.ARoleResourceAdminAction.reCreate(ARoleResourceAdminAction.java:63)
... 41 more
Caused by: net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:236)
at net.sf.cglib.reflect.FastClass$Generator.create(FastClass.java:64)
at net.sf.cglib.proxy.MethodProxy.helper(MethodProxy.java:81)
at net.sf.cglib.proxy.MethodProxy.create(MethodProxy.java:46)
at com.amjn.web.model.user.admin.AdminRoleModel$$EnhancerByCGLIB$$7033e790.CGLIB$STATICHOOK2(<generated>)
at com.amjn.web.model.user.admin.AdminRoleModel$$EnhancerByCGLIB$$7033e790.<clinit>(<generated>)
... 80 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor84.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at net.sf.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:384)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:218)
... 85 more
Caused by: java.lang.LinkageError: duplicate class definition: $java/lang/Object$$FastClassByCGLIB$$3f697993
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)


if changed
<many-to-one name="p" column="p_id" class="example.model.P">
to
<many-to-one name="p" column="p_id" class="example.model.P" lazy="false">

The code can be correctly executed,but sql statements presented in console are:

select c.c_id c_id,c.name name from c_tab;
select p.p_id p_id,p.name name from p_tab where p_id=?

It is looks like that the lazy="true" at line L1 was not be used by Hibernate3.x!!

Who encounterd this proplem and what can i do?
Hope your helpful ,thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 2:02 am 
Regular
Regular

Joined: Wed Apr 12, 2006 12:49 am
Posts: 105
Location: Malaysia
Hi,

I'm not sure why are you getting the error.

But I read from somewhere (probably 3.x documentation) that the default for class in Hibernate 3.x is lazy=true.

So, specifying lazy=true or not in class does not really make the different in line 1.

Quote:
Rate me if you find this useful.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 07, 2006 2:08 am 
Regular
Regular

Joined: Wed Apr 12, 2006 12:49 am
Posts: 105
Location: Malaysia
By the way, can you try to change to following?

<many-to-one name="p" column="p_id" class="example.model.P" lazy="true">

Quote:
Rate me if you find this userful.


Top
 Profile  
 
 Post subject: Hi
PostPosted: Fri Jul 07, 2006 3:05 am 
Regular
Regular

Joined: Fri Nov 12, 2004 12:07 am
Posts: 57
Location: Beijing,China
jbchurn wrote:
By the way, can you try to change to following?

<many-to-one name="p" column="p_id" class="example.model.P" lazy="true">

Quote:
Rate me if you find this userful.


Thanks for your answer.
The value of lazy can only be one of "false,proxy,no-proxy" when it is used in <many-to-one>

When execute

"from C"

What i want is only the instance list of C not the P instance that the each C instance related.

in Hibernate2.x

What i have to do is just add lazy="true" in P.hbm.xml

<class ......... lazy="true">
.......
</class>

when excute

"from C"

the sql statements shown in console is:

select c.id.......from c_tab

but in Hibernate3.x, as you said lazy="true" is the default behaviour in
<class....... >

</class>

when i excute

"from C"

the exception that i have posted will be presented.

why?

i really really don't understand.


Top
 Profile  
 
 Post subject: Hi
PostPosted: Thu Jul 13, 2006 11:18 pm 
Regular
Regular

Joined: Fri Nov 12, 2004 12:07 am
Posts: 57
Location: Beijing,China
When you developing with Hibernate2.x,you must add lazy=true in hbm.xml,for example:

<class ....... lazy="true">
</class>

class C extends P.

hql is "from C".

when lazy="false",the sqls presented in console are:
select c.id,c.name from c
select p.id,p.name from p where p.id=?

when lazy="true" sql is:
select c.id,c.name from c

But in Hibernate3.x,you don't need add lazy=true in hbm.xml,because it is the default behaviour,but it seems not useful.

whether lazy is true or false,the sqls always are:
select c.id,c.name from c
select p.id,p.name from p where p.id=?

why?Can anybody tell me the cause?


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.