aspose file tools*
The moose likes Object Relational Mapping and the fly likes Problem in updating the values with Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem in updating the values with Hibernate" Watch "Problem in updating the values with Hibernate" New topic
Author

Problem in updating the values with Hibernate

Tejo Kumar
Greenhorn

Joined: Aug 09, 2004
Posts: 21
Hi,
I am having a problem in updating the values of one form which is realated to 2 tables. When i am trying to update the values I am getting the following error message.

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Exception on Hibernate data access; nested exception is:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 8, of class: com.b2b.datas.dto.Postaladdress
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:526)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:467)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1422)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:523)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
com.b2b.datas.filters.SessionChecking.doFilter(SessionChecking.java:50)
org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:86)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)


root cause

org.springframework.orm.hibernate.HibernateSystemException: Exception on Hibernate data access; nested exception is:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 8, of class: com.b2b.datas.dto.Postaladdress
org.springframework.orm.hibernate.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:171)
org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:151)
org.springframework.orm.hibernate.HibernateTemplate.update(HibernateTemplate.java:236)
com.b2b.datas.dao.PostalAddressDAO.update(PostalAddressDAO.java:140)
com.b2b.datas.web.action.PostalAddressAction.update(PostalAddressAction.java:272)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:324)
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:278)
org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:218)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:465)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1422)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:523)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
com.b2b.datas.filters.SessionChecking.doFilter(SessionChecking.java:50)
org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:86)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)


root cause

net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 8, of class: com.b2b.datas.dto.Postaladdress
net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1673)
net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1442)
net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1469)
net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1392)
net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1474)
net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1392)
net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
net.sf.hibernate.engine.Cascades.cascade(Cascades.java:482)
net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1474)
net.sf.hibernate.impl.SessionImpl.update(SessionImpl.java:1354)
org.springframework.orm.hibernate.HibernateTemplate$4.doInHibernate(HibernateTemplate.java:238)
org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:144)
org.springframework.orm.hibernate.HibernateTemplate.update(HibernateTemplate.java:236)
com.b2b.datas.dao.PostalAddressDAO.update(PostalAddressDAO.java:140)
com.b2b.datas.web.action.PostalAddressAction.update(PostalAddressAction.java:272)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:324)
org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:278)
org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:218)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:465)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1422)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:523)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
com.b2b.datas.filters.SessionChecking.doFilter(SessionChecking.java:50)
org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:86)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)


note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.

Can anyone please solve my problem, and help me with the correct approach.

Thanx,
Regards
Tejo Kumar.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


a different object with the same identifier value was already associated with the session: 8, of class: com.b2b.datas.dto.Postaladdress

What you are possibly doing is modifying an object so the PK matches that of one which you have already loaded into the Session. You say you are updating a value via a form? Are you recovering the object from the database and calling update in the same session, but creating a new object to do the update possibly?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Tejo Kumar
Greenhorn

Joined: Aug 09, 2004
Posts: 21
Hi Paul,

Thanks for ur reply.
I have solved the problem. The problem was keeping the attribute "cascade=all" of collection, set in hbm file. I have removed it and its working fine.

Regards,
Tejo Kumar.
Francois Roland
Ranch Hand

Joined: Jul 24, 2003
Posts: 34

I had a similar problem. But in my case, no attribute was involved.

What I had was a bidirectional many-to-one association. When I wanted to edit the parent object, what I was doing was storing only an array of ids in my Struts form.

When I received the request, I was loading each child from Hibernate, creating a new parent object and populating its collection with the children objects before calling a saveOrUpdate method on the parent.

The problem cause, as far as I understood, is that my children objects were backreferencing the parent. So, when I loaded them, I loaded the parent too into my Hibernate session. But when I was updating, I was using a detached copy of the parent. There were actually two version of the parent object, one attached to the session and one not. This is probably why Hibernate complained.

There are two solutions:
- either you load the parent object before updating it
- or you make your association unidirectional.


--
Brown belt on KnowledgeBlackBelt.com.
Aaron Bartell
Greenhorn

Joined: Dec 28, 2004
Posts: 2
I had the same problem and fixed it, but have no idea why the change I made fixes it.

Works:
try {
User usr = LoginController.user();
DBConn.sess().save(usr);
} catch (HibernateException e) {
e.printStackTrace();
return "fail";
}

Doesn't Work:
try {
DBConn.sess().save(LoginController.user());
} catch (HibernateException e) {
e.printStackTrace();
return "fail";
}

Odd eh?

Aaron Bartell


Aaron Bartell<br /><a href="http://mowyourlawn.com" target="_blank" rel="nofollow">http://mowyourlawn.com</a>
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Aaron, are you sure that was the only difference? I would expect that most compilers would generate the exact same code in both cases since the usr variable is local to the try block.
sajjad ahmad
Ranch Hand

Joined: Jan 23, 2003
Posts: 75
I am trying to update a child records i have following scenario
master record (having one to many assciation with child table cascade=all in .hbm.xml configuration file) ===> customerInfo
detail record set (having many to one assciation with master table cascade=all in .hbm.xml configuration file) ===> customerAccountsSet

i do following steps to update a record
1) get the customerInfo objetc from session successfully
2) get the customerAccountsSet = customerInfo.getCustomerAccounts();
3) traying to add a new customerAccount say customerAccount1 to be saved for the first time without primery key in it
4) traying to update a customerAccount say customerAccount2 to be updated with primery key in it
5) traying to add a delete customerAccount say customerAccount3 with primery key in it

only step 5 is not executed properly as i can still see the deleted record in db , although save and update steps have been successfully completed in db.
can anybody tell me whats going wrong here.code for steps 2,3,4,5 is as follows in the same order
2)Set customerAccountSet = (Set) customerInfo.getCustomerAccounts();
3)customerAccountSet.add(customerAccount);
4)customerAccountSet.add(customerAccount);
5)customerAccountSet.remove(customerAccount);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem in updating the values with Hibernate
 
Similar Threads
jboss + mysql error.
Nullpointer exception while accessing datasource in struts
unexpected Exception : by bean
FileUploading in struts
SQL server and hibernate