-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to use 'Hibernate.initialize()'
PostPosted: Sun Apr 03, 2011 5:36 am 
Newbie

Joined: Sun Jan 23, 2011 4:14 am
Posts: 7
Hi,

I used it as below, but getting an exception when the 'initialize()' method is called (pasted below). Where should I do this 'initialize()'?

Code:
public class TestProductHibernateImpl extends org.springframework.orm.hibernate3.support.HibernateDaoSupport{
.
.
.

   public TestInstance getTestInstance(String instanceId,
         boolean includeTestInstanceQuestions) throws DaoException {
      TestInstance testInstance = (TestInstance) this.getHibernateTemplate()
            .get(TestInstance.class, instanceId);
      if (includeTestInstanceQuestions) {
         this.getHibernateTemplate().initialize(
               testInstance.getInstanceQuestions());
      }
      return testInstance;
   }



Code:
org.springframework.orm.hibernate3.HibernateSystemException: collection is not associated with any session; nested exception is org.hibernate.HibernateException: collection is not associated with any session
   at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:661)
   at org.springframework.orm.hibernate3.HibernateTemplate.initialize(HibernateTemplate.java:651)
   at com.test.product.dataAccess.impl.TestProductHibernateImpl.getTestInstance(TestProductHibernateImpl.java:62)


Top
 Profile  
 
 Post subject: Re: How to use 'Hibernate.initialize()'
PostPosted: Mon Apr 04, 2011 12:57 am 
Regular
Regular

Joined: Wed Apr 25, 2007 11:44 pm
Posts: 59
i haven't used spring with hibernate but the exception that you mentioned is thrown when there is no active connection available in hibernate;

are you closing connection in you classes ?


Top
 Profile  
 
 Post subject: Re: How to use 'Hibernate.initialize()'
PostPosted: Mon Apr 04, 2011 3:25 am 
Newbie

Joined: Sun Jan 23, 2011 4:14 am
Posts: 7
What I am thinking is, I should use HibernateTemplate instead of HibernateDaoSupport, because I need to do the initialize().

Am I correct?


Top
 Profile  
 
 Post subject: Re: How to use 'Hibernate.initialize()'
PostPosted: Mon Apr 04, 2011 4:46 am 
Beginner
Beginner

Joined: Mon Apr 04, 2011 3:31 am
Posts: 41
ya correct you can do that for example

public interface xyzDao {
Person getxyz(String firstName, String... paths);
}

Here is what an implementation of this DAO may look like:

public class HibernateXyzDao implements xyzDao {

public Person getxyz(String firstName, String... paths) {
Session session = getSession();
Criteria criteriaQuery = session.createCriteria(resultClass);
criteriaQuery.add(Expression.eq("firstName", firstName));
setFetchModes(criteriaQuery, loadedPaths);
for(String eagerPath : paths) {
criteriaQuery.setFetchMode(eagerPath, FetchMode.JOIN);
}
return criteriaQuery.uniqueResult();
}
}

You would use this in your application like this:

XyzDao dao = // get person dao.
dao.getxyz("Jane", "mother", "children", "children.mother");

hope its works

_________________
Thanks
Niki


Top
 Profile  
 
 Post subject: Re: How to use 'Hibernate.initialize()'
PostPosted: Mon Apr 04, 2011 5:12 am 
Newbie

Joined: Sun Jan 23, 2011 4:14 am
Posts: 7
niks23 wrote:
ya correct you can do that for example

public interface xyzDao {
Person getxyz(String firstName, String... paths);
}

Here is what an implementation of this DAO may look like:

public class HibernateXyzDao implements xyzDao {

public Person getxyz(String firstName, String... paths) {
Session session = getSession();
Criteria criteriaQuery = session.createCriteria(resultClass);
criteriaQuery.add(Expression.eq("firstName", firstName));
setFetchModes(criteriaQuery, loadedPaths);
for(String eagerPath : paths) {
criteriaQuery.setFetchMode(eagerPath, FetchMode.JOIN);
}
return criteriaQuery.uniqueResult();
}
}

You would use this in your application like this:

XyzDao dao = // get person dao.
dao.getxyz("Jane", "mother", "children", "children.mother");

hope its works


Im afraid you have masked your code too much that it is difficult to understand.

It will be nice of you to repost it, with more complete and clear detail.

For example, what class is xyzDao? Is it a HibernateTemplate or a HibernateDaoSupport?

Please repost with more complete details.


Top
 Profile  
 
 Post subject: Re: How to use 'Hibernate.initialize()'
PostPosted: Mon Apr 04, 2011 5:26 am 
Beginner
Beginner

Joined: Mon Apr 04, 2011 3:31 am
Posts: 41
Check this ..

http://www.javalobby.org/java/forums/t62077.html

_________________
Thanks
Niki


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