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: Problem with persist a object
PostPosted: Tue Aug 21, 2007 11:22 am 
Newbie

Joined: Wed Jan 17, 2007 9:39 pm
Posts: 5
Hi, I'm developing a Web Project with Hibernate and I have some problems.
I had this exception:
Code:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:

I know what happening, but I don't know how to solve...
I load an object tha manager the application from database and put it in the session of my web application. This object have a lot of lists and when I add a nem item to a list and call saveOrUpdate a have this exception. What should I do?
I'm using Reflection to add an object to a especified List.
This is the code where the problem occurs:
Code:
List < Object > objList = ( List < Object > ) getMethod.invoke ( curriculo );
         if ( ( objList == null ) || ( objList.size ( ) == 0 ) )
            objList.add ( obj );
         else
         {
            boolean found = false;
            for ( Object object : objList )
            {
               if ( ( ( Integer ) objGetIdMethod.invoke ( object ) ).equals ( ( Integer ) objGetIdMethod.invoke ( obj ) ) )
               {
                  int i = objList.indexOf ( object );
                  for ( Method method : object.getClass ( )
                                 .getMethods ( ) )
                  {
                     if ( method.getName ( )
                                    .startsWith ( "set" ) )
                     {
                        Method method2 = object.getClass ( )
                                       .getDeclaredMethod ( "g" + method.getName ( )
                                                      .substring ( 1 ) );
                        method.invoke ( objList.get ( i ) , method2.invoke ( obj ) );
                     }
                  }
                  found = true;
                  break;
               }
            }
            if ( !found )
               objList.add ( obj );
         }
         setMethod.invoke ( curriculo , objList );
         DAO.saveOrUpdate ( curriculo );

This is the saveOrUpdate code
Code:
public static void saveOrUpdate ( Object obj )
   {
         HibernateUtils.commitTransaction ( );
   }

My HibernateUtils class
Code:
public class HibernateUtils
{

   private static final SessionFactory sessionFactory;

   private static final ThreadLocal < Session > sessionThread = new ThreadLocal < Session > ( );

   private static final ThreadLocal < Transaction > transactionThread = new ThreadLocal < Transaction > ( );
   
   static
   {
      try
      {
         sessionFactory = new AnnotationConfiguration ( ).
                                             addAnnotatedClasses().
                        .setProperty ( "hibernate.connection.driver_class" , "org.postgresql.Driver" )
                        .setProperty ( "hibernate.connection.url" , "jdbc:postgresql:lattes" )
                        .setProperty ( "hibernate.connection.username" , "postgres" )
                        .setProperty ( "hibernate.connection.password" , "postgres" )
                        //.setProperty ( "hibernate.dialect" , "org.hibernate.dialect.PostgreSQLDialect" )
                        .setProperty ( "hibernate.hbm2ddl.auto" , "update" )
                        .buildSessionFactory ( );
      }
      catch ( Throwable ex )
      {
         ex.printStackTrace ( );
         throw new ExceptionInInitializerError ( ex );
      }
   }


   public static Session getSession ( )
   {
      Session session = ( Session ) sessionThread.get ( );
      if ( session == null || !session.isOpen ( ) )
      {
         session = sessionFactory.openSession ( );
         sessionThread.set ( session );
      }
      return ( Session ) sessionThread.get ( );
   }


   public static void closeSession ( )
   {
      Session session = ( Session ) sessionThread.get ( );
      if ( session != null && session.isOpen ( ) )
      {
         sessionThread.set ( null );
         session.close ( );
      }
   }


   public static void beginTransaction ( )
   {
      Transaction transaction = getSession ( ).beginTransaction ( );
      transactionThread.set ( transaction );
   }


   public static void commitTransaction ( )
   {
      Transaction transaction = ( Transaction ) transactionThread.get ( );
      if ( transaction != null && !transaction.wasCommitted ( )
                     && !transaction.wasRolledBack ( ) )
      {
         transaction.commit ( );
         transactionThread.set ( null );
      }
   }


   public static void rollbackTransaction ( )
   {
      Transaction transaction = ( Transaction ) transactionThread.get ( );
      if ( transaction != null && !transaction.wasCommitted ( )
                     && !transaction.wasRolledBack ( ) )
      {
         transaction.rollback ( );
         transactionThread.set ( null );
      }
   }
}

And my filter class
Code:
HibernateUtils.beginTransaction ( );
      try
      {
         chain.doFilter ( request , response );
         HibernateUtils.commitTransaction ( );
      }
      catch ( HibernateException exception )
      {
         HibernateUtils.rollbackTransaction ( );
         exception.printStackTrace ( );
      }
      finally
      {
         HibernateUtils.closeSession ( );
      }

Hibernate version: 3.2

Name and version of the database you are using:PostgreSQL 8.2

Thanks for any help


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 4:38 pm 
Beginner
Beginner

Joined: Fri May 18, 2007 10:28 am
Posts: 48
Location: Madison, WI
Can you try flushing the session after committing transaction.

_________________
Please rate if it helped


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 10:43 pm 
Newbie

Joined: Wed Jan 17, 2007 9:39 pm
Posts: 5
Do not work!
The app throws the exception whe commit is called.


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.