-->
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.  [ 3 posts ] 
Author Message
 Post subject: Collection proxy initialization
PostPosted: Sat Dec 18, 2004 1:24 am 
Newbie

Joined: Sun Apr 11, 2004 2:25 am
Posts: 9
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>



Code
Code:
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]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 19, 2004 7:12 pm 
Newbie

Joined: Sun Apr 11, 2004 2:25 am
Posts: 9
Sadly this feature is critical to my employer's ORM solution, because we need to make classes lazy for auto-generation, re-usability and efficiency reasons. If there is no built-in in way to initialize the proxies within many-to-many collections in one query I will be forced to modify the hibernate source code to make it work. I am open to ANY suggestions (I really really don't want to have to modify/re-compile hibernate to get this to work!).

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 19, 2004 7:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No need to get all dramatic. This is of course trivial:

Code:
<many-to-many class="items.Paper"
            column="paper_id"
            outer-join="true"
         />


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.