• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in updating the values with Hibernate

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
Tejo Kumar
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic