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