Hibernate version:
Hibernate 2.1.6
Mapping documents:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="" auto-import="true">
<class name="items.Box" table="Box">
<id name="boxId" column="box_id" unsaved-value="0" >
<generator class="native"/>
</id>
<bag name="balls" lazy="true" cascade="save-update" table="Box_For_Ball">
<key column="box_id"/>
<many-to-many class="items.Ball"
column="ball_id"
/>
</bag>
<bag name="papers" lazy="true" cascade="save-update" table="Box_For_Paper">
<key column="box_id"/>
<many-to-many class="items.Paper"
column="paper_id"
/>
</bag>
</class>
<class name="items.Ball" table="Ball" lazy="true">
<id name="ballId" column="ball_id" unsaved-value="0">
<generator class="native"/>
</id>
<property name="size" type="string" column="size"/>
</class>
<class name="items.Paper" table="Paper" lazy="true">
<id name="paperId" column="paper_id" unsaved-value="0">
<generator class="native"/>
</id>
<property name="weight" type="int" column="weight"/>
</class>
</hibernate-mapping>
CodeCode:
Session sess = getSession();
List returnList = new ArrayList();
Box bx = null;
try {
beginTransaction();
bx = (Box)sess.createQuery("select bx from Box bx left join fetch" +
" bx.balls ball where bx.boxId = 1")
.uniqueResult();
Hibernate.initialize(bx.getPapers());
commitTransaction();
}
catch (HibernateException e) {
e.printStackTrace();
}
finally {
closeSession();
}
Iterator itr = bx.getPapers().iterator();
while (itr.hasNext()) {
Paper p = (Paper)itr.next();
System.out.println(p.getWeight());
}
return returnList;
Full stack trace of any exception that occurs:net.sf.hibernate.LazyInitializationException: Exception initializing proxy: [items.Paper#1]
at net.sf.hibernate.proxy.LazyInitializer.initializeWrapExceptions(LazyInitializer.java:64)
at net.sf.hibernate.proxy.LazyInitializer.getImplementation(LazyInitializer.java:164)
at net.sf.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:108)
at items.Paper$$EnhancerByCGLIB$$a15452b7.getWeight(<generated>)
at BoxAndBallDao.getBoxes(BoxAndBallDao.java:45)
at BoxAndBall.main(BoxAndBall.java:51)
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.intellij.rt.execution.application.AppMain.main(Unknown Source)
Caused by: net.sf.hibernate.HibernateException: Could not initialize proxy - the owning Session was closed
at net.sf.hibernate.proxy.LazyInitializer.initialize(LazyInitializer.java:47)
at net.sf.hibernate.proxy.LazyInitializer.initializeWrapExceptions(LazyInitializer.java:60)
... 10 more
Exception in thread "main" Process terminated with exit code 1
Name and version of the database you are using:MySQL 4.1
The generated SQL (show_sql=true):Hibernate: select box0_.box_id as box_id0_, ball2_.ball_id as ball_id1_, ball2_.size as size1_, balls1_.box_id as box_id__, balls1_.ball_id as ball_id__ from Box box0_ left outer join Box_For_Ball balls1_ on box0_.box_id=balls1_.box_id left outer join Ball ball2_ on balls1_.ball_id=ball2_.ball_id where (box0_.box_id=1 )
Hibernate: select papers0_.box_id as box_id__, papers0_.paper_id as paper_id__ from Box_For_Paper papers0_ where papers0_.box_id=?
Here we see that the code attempting to load the papers in a box loads the proxies of these values instead of the actual objects. I should note if the collection is a one-to-many, it actually loads the entire object, however if it is a many-to-many, it only loads the joining table.
My question is if it is possible to keep the class lazy but be able to load (in one query) the papers inside of the box. It would be asking if it is possible to disable proxies just for one initialize of a collection.[/code]