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.
|