-->
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.  [ 7 posts ] 
Author Message
 Post subject: org.hibernate.NonUniqueObjectException
PostPosted: Wed Aug 29, 2007 4:08 am 
Newbie

Joined: Wed Aug 08, 2007 3:51 am
Posts: 6
Code:
public ICurrency getSystemCurrency(Session session) {
      ICurrency currency = null;

      Query query = null;

      query= session.createQuery("from Currency where systemCurrency = true");

      if (!Utils.isEmtpyCollection(query.list())) {

         currency = (ICurrency) ((query.list().get(0)));
         
         return currency;
      } else {
         currency = null;
         return currency;
      }
   }


I got a large method in my application:
testing stuf
process shopping cart
process pending orders
redirect to another page

in process shopping cart i do al sorts of stuf incl:
generate proforma documents --> here i call through a service this dao method


in pending orders i do al sorts of stuf incl:
generate invoice documents --> here i call through a service this dao method


the second time i call this method, to get the systemCurrency. I get :
Code:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session


I looked in the session and idd that object is still in the session. But how do i get it out and can test on it ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 29, 2007 10:10 am 
Newbie

Joined: Wed Aug 08, 2007 3:51 am
Posts: 6
Anyone?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 12:04 am 
Newbie

Joined: Thu Dec 21, 2006 5:18 am
Posts: 3
Location: Bangalore
Clear the session before calling the the method.
Code:
session.clear();

or
After fetching the object remove it from the session using
Code:
session.evict(currency );


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 3:32 am 
Newbie

Joined: Wed Aug 08, 2007 3:51 am
Posts: 6
Thx for replying, but already tried it.

I get :

Code:
org.hibernate.LazyInitializationException



I got the same problem at :

Quote:
public IContentContainer getContentContainerByName(String contentContainerName,int languageId, Session session) {
IContentContainer contentContainer = null;
Query query = null;

query = session
.createQuery("from ContentContainer where contentContainerName = :contentContainerName and languageId = :languageId");
query.setString("contentContainerName", contentContainerName);
query.setInteger("languageId", languageId);

try {
query.list();
} catch (Exception e) {
e.printStackTrace();
}
if (!Utils.isEmtpyCollection(query.list())) {

contentContainer = ((IContentContainer) (query.list().get(0)));

return contentContainer;
}
contentContainer = null;
return contentContainer;
}


query.list(); --> non unique error. And i don't even see an object in the session. Which totally strange. That's something i didn't had at currency.

I tried :
session.evict(currency);
session.clear();

or

Code:
contentContainer = ((IContentContainer) (query.list().get(0)));
session.evict(contentContainer);
return contentContainer;


or

Code:
contentContainer = ((IContentContainer) (query.list().get(0)));
session.clear();
return contentContainer;


They all gave :

Code:
org.hibernate.LazyInitializationException


at contentcontainer :

Code:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: be.in.inforbusiness.model.implementations.contentcontainer.ContentContainer.containerElements, no session or session was closed


Anyone got more ideas plz ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 4:05 am 
Newbie

Joined: Wed Aug 08, 2007 3:51 am
Posts: 6
Just want to say.

If i put the mappings on lazy="false", the NonUnique error returns.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 4:40 am 
Newbie

Joined: Thu Dec 21, 2006 5:18 am
Posts: 3
Location: Bangalore
Can you post the code which shows how you are passing Session object to getSystemCurrency method.


Last edited by vishwas.tp on Thu Aug 30, 2007 5:38 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 4:49 am 
Newbie

Joined: Wed Aug 08, 2007 3:51 am
Posts: 6
Code:
   public IContentContainer getContentContainerByName(
         String contentContainerName, int languageid) {

      IContentContainer contentContainer = null;

      contentContainer = this.dao.getContentContainerByName(
            contentContainerName, languageid, HibernateUtil
                  .currentSession());

      return contentContainer;
   }




Code:
public static Session currentSession() {
      Session s = (Session) threadLocalSession.get();
      // Open a new Session, if this Thread has none yet
      if (s == null) {
         s = sessionFactory.openSession();
         threadLocalSession.set(s);
      }
      return s;
   }


In an aspect :


Code:
public Object wrapTransaction(ProceedingJoinPoint pjp) throws Throwable {
      Object retVal = null;
      Transaction trans = null;
       try{
          Session sess = HibernateUtil.currentSession();
          trans = HibernateUtil.currentTransaction(sess);
          // System.out.println("Transaction starts");

          retVal = pjp.proceed();

          trans = HibernateUtil.currentTransaction(sess);
          trans.commit();

          // System.out.println("Transaction commit");
       }catch(Exception e){

          Session sess = HibernateUtil.currentSession();
          trans = sess.beginTransaction();
          trans.rollback();

       }finally{

       }
       return retVal;
   }


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