-->
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: Newbie join Question
PostPosted: Sun Oct 02, 2005 3:13 am 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
Simplified version of the object structure at hand:

Code:
class A
{
   public int id;
   public B bReference;
};

class B
{
   public int id;
   public String someData;
};


My simplified code:

Code:
   Session session = HibernateUtil.getCurrentThreadSession();   
   try {   
      Query query = session.createQuery("from A as a");
      searchResults = query.list();
   } finally {
      HibernateUtil.closeSession();
   }


"searchResults" contains a collection of "A" instances (or proxy classes derived from "A"). However, when code tries to use the accessor to the bReference property, I get an exception:

javax.servlet.ServletException: javax.servlet.jsp.JspException: could not initialize proxy - the owning Session was closed

I assume this happens because the B instances weren't loaded and Hibernate is trying to load them when the accessor method gets called which is after the session is closed. Is this right?

I want Hibernate to preload the B instances and mappings as well. Does that make sense? How do I do that?

Or am I not supposed to close the session within a web app (JSF) and simply let each thread maintain an open session to Hibernate?

Thanks for any help!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 6:58 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This is very well explained in the documentation chapter "Working with objects".


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 7:01 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Actually, its really an FAQ, it has several Wiki pages dealing with it, and the documentation chapter is

http://www.hibernate.org/hib_docs/v3/re ... ons-basics


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 10:38 am 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
christian wrote:
Actually, its really an FAQ, it has several Wiki pages dealing with it, and the documentation chapter is

http://www.hibernate.org/hib_docs/v3/re ... ons-basics


I just reread that chapter; I see what it says regarding session use. It suggests one session per request and discusses using the HibernateUtil class and ThreadLocal which is what I am doing.

This doesn't really doesn't solve my problem. It doesn't say anything about how Hibernate loads many-to-one references and how I can force that loading during a query.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 02, 2005 11:05 am 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
OK, I resolved that first issue. Had to set lazy="false" in the mapping. So, I can control the auto-fetch at the mapping level but not at the query level. Now on to the next problem; this is a little trickier:

Simplified classes:

Code:
class A
{
   private int id;
   private B b;   // Ref to class b

   // basic getters/settters
};

class B
{
   private int id;
   private String someData;

   // basic getters/settters
};

class C extends B
{
   private String derivedClassData;

   // basic getters/settters
};


C.hbm.xml:

Code:
<joined-subclass
      name="package.C"
      table="C"
      extends="package.B">
   <key column="ID"/>

   <property name="derivedClassData" column="DerivedClassData" type="string" not-null="true"/>
</joined-subclass>


My simplified code:

Code:
   Session session = HibernateUtil.getCurrentThreadSession();   
   try {   
      Query query = session.createQuery("from A as a");
      searchResults = query.list();
   } finally {
      HibernateUtil.closeSession();
   }

   // Later in GUI framework, something like the following runs:
   for (Object o : searchResults) {
      A a = (A) a;
      B b = a.getB();   // Need to set lazy="false" in <key-many-to-one> in A.hbm.xml or else this will exception.

      // This will throw ClassCastException. Hibernate always returns direct instances of B
      // and never detects and creates the derived instances of C. Is there any way to get it
      // to do that?
      C c = (C) b;
   }


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.