-->
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.  [ 2 posts ] 
Author Message
 Post subject: Nested Transactions in hibernate
PostPosted: Sun May 12, 2013 5:19 am 
Newbie

Joined: Sun May 12, 2013 4:12 am
Posts: 1
Hi,
I am a beginner in hibernate. I am facing a problem when I am trying to create nested transactions.

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: lib.LibStudent.transactions, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:119)
at org.hibernate.collection.PersistentSet.isEmpty(PersistentSet.java:169)
Code:
public String onSubmit() {
      Session sessionUtil = HibernateUtil.getSessionFactory().openSession();
                            sessionUtil.setFlushMode(FlushMode.MANUAL);
      sessionUtil.beginTransaction();
      try {
// nested transaction call   
                               LibLogin result = LibLoginDAO.getUser(loginObj.getUserID());
         if (null != result) {
   
               Set<LibTransaction> tranSet = result.getLibStudent()
                        .getTransactions();
// this piece of code is giving error
                  if (null != tranSet && !tranSet.isEmpty()) {
                     session.put("issuedBookList", tranSet);
                  }
                  return "student";
               }

            } else {
               return "failure";
            }

         }
      } catch (HibernateException e) {
         e.printStackTrace();
         sessionUtil.getTransaction().rollback();
      } finally {
         sessionUtil.getTransaction().commit();
                                           sessionUtil.flush();
         sessionUtil.close();
      }

      return "failure";

   }

Nested Transaction method:
public static LibLogin getUser(String userId) {
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
      LibLogin login = null;
      
         
         List<LibLogin> loginList= session.createQuery("from lib.LibLogin where userid ='" + userId +"'").list();
         if(loginList.size()==1){
            login=(LibLogin)loginList.get(0);
            
         }
         
      session.getTransaction().commit();
      return  login;      
   }

IF I do the following changes in getUser() method :
Session session = HibernateUtil.getSessionFactory().openSession();
and block the
//session.close();

no exception in this case, but I want make the whole transaction Atomic.

kindly advice what is wrong with the code.


Top
 Profile  
 
 Post subject: Re: Nested Transactions in hibernate
PostPosted: Thu May 16, 2013 8:26 am 
Newbie

Joined: Sun Oct 02, 2005 12:49 am
Posts: 15
Two key points:

> AFAIK, nested transactions are not supported.

> See section 2.3 of the hibernate documentation titled "Contextual Sessions" to understand the scope of sessions and transactions and relevant configuration parameters.


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