-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: "a different object with the same identifier value was
PostPosted: Tue Sep 09, 2008 2:42 am 
Newbie

Joined: Tue Sep 09, 2008 2:30 am
Posts: 10
Hi!
I have a problem with hibernate. When i want to delete or edit an object, i have this exception:

"a different object with the same identifier value was already associated with
the session"

Yesterday, i modified the .hbm.xml because i had this exception:

"deleted object would be re-saved by cascade (remove deleted object from associations)"

I erased the property lazy="false" of my .hbm.xml, maybe it is the reason that now is failing...?

I am a spanish girl, sorry for my english, i hope you understand me.


Top
 Profile  
 
 Post subject: Cascade
PostPosted: Tue Sep 09, 2008 2:57 am 
Newbie

Joined: Fri Aug 29, 2008 6:57 am
Posts: 6
Hi,

Plase check the cascade value.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 3:00 am 
Newbie

Joined: Tue Sep 09, 2008 2:30 am
Posts: 10
cascade="all-delete-orphan"


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 3:19 am 
Newbie

Joined: Tue Sep 09, 2008 2:30 am
Posts: 10
i am using spring+hibernate, and my configuration file has this code:


<bean id="controladorAlumno" class="org.cisgalicia.control.controladores.ControladorAlumno">
<property name="alumnoServicio" ref="alumnoServicio" />
<property name="empresaServicio" ref="empresaServicio" />
<property name="cursoServicio" ref="cursoServicio" />
</bean>


Maybe, if i use three "Servicios" inside the same "controladorAlumno", they will share the same Hibernate Session, what do you think? it is posible?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 3:54 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Sounds like you are explicitly deleting an object that is part of a collection marked as cascade="all-delete-orphan" but not removing it from the collection. And perhaps doing an update on a detatched instance instead of a merge.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 4:10 am 
Newbie

Joined: Tue Sep 09, 2008 2:30 am
Posts: 10
now i am doing a merge (not a update), and the application is running "better",not as well that i want, but better than few minutes ago, ;).
But i have another cuestion for this problem. I put this code in my web.xml:

<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>
org.cisgalicia.sesion.MySessionFilter
</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

It was because i needed to change de flush mode, and this is the class MySessionFilter. it is all ok in this class?

public class MySessionFilter extends OpenSessionInViewFilter {
protected Session getSession(SessionFactory sessionFactory)
throws DataAccessResourceFailureException {
Session session = super.getSession((org.hibernate.SessionFactory) sessionFactory);
session.setFlushMode(FlushMode.COMMIT);
return session;
}
protected void closeSession(Session session, SessionFactory factory) {
session.flush();
super.closeSession(session, (org.hibernate.SessionFactory) factory);
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 4:24 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I've never used OpenSessionInViewFilter but having read the spring docs your code looks fine. Did you add this filter recently? I ask because there is a warning in the spring docs saying:

"Applying this filter to existing logic can cause issues that have not appeared before, through the use of a single Hibernate Session for the processing of an entire request. In particular, the reassociation of persistent objects with a Hibernate Session has to occur at the very beginning of request processing, to avoid clashes with already loaded instances of the same objects."

Adding this filter to existing code could lead to the exception you were seeing: "a different object with the same identifier value was already associated with the session"


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 4:44 am 
Newbie

Joined: Tue Sep 09, 2008 2:30 am
Posts: 10
Yes! I added this filter recently because i had an exception saying that the flushmode was only "to read". I could not save or update any object and i saw this solution searching in some forums.
There is another solution to change the flushmode?

This is the exception that caused that I changed flushmode:

Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 5:08 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
This probably means you were trying to delete/update objects inside a read-only transaction. I imagine the correct fix would be to change the transaction definition for the service operation you are performing, not to add a filter that overrides flush mode. How are you managing transactions? spring or ejb?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 5:18 am 
Newbie

Joined: Tue Sep 09, 2008 2:30 am
Posts: 10
I am managing transactions with spring:

<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 5:32 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
That's the transaction manager but how are transactions actually started? Are you using spring's declarative transactions?
e.g.
Code:
  <!-- the transactional advice (what 'happens'; see the <aop:advisor/> bean below) -->
  <tx:advice id="txAdvice" transaction-manager="txManager">
    <!-- the transactional semantics... -->
    <tx:attributes>
      <!-- all methods starting with 'get' are read-only -->
      <tx:method name="get*" read-only="true"/>
      <!-- other methods use the default transaction settings (see below) -->
      <tx:method name="*"/>
    </tx:attributes>
  </tx:advice>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 6:24 am 
Newbie

Joined: Tue Sep 09, 2008 2:30 am
Posts: 10
I don't understand what are you asking to me. Do you want to say properties of TransactionManager?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 6:27 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
How do you begin a transaction in your system?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 6:39 am 
Newbie

Joined: Tue Sep 09, 2008 2:30 am
Posts: 10
with getHibernateTemplate

For example:

public List<Alumno> getAlumnos() {
return getHibernateTemplate().find("from Alumno");
}


You talk about that?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 7:19 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Ok, because you don't know what I'm talking about I assume you're not using middle tier transactions at all. In which case, you should _keep_ your filter the way it is and fix any problems you have in the service layer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.