-->
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.  [ 5 posts ] 
Author Message
 Post subject: NonUniqueObjectException
PostPosted: Thu Oct 21, 2004 5:28 am 
Newbie

Joined: Wed Oct 06, 2004 3:20 am
Posts: 12
Description

The problem is, that we have a user registration-flow:
1. personal data,store in http session
2. interests of user, store in http session
3. insert everthing in my sql db

if i register a user1, everthing works fine, and all data's are inserted in my db.
but if i would like to register a user2 while inserting the data, hibernate throws an NonUniqueObjectException a different object with the same identifier...
the user2 personal data. are ok., but the interests aren't inserted.
even if user 2 has not one equal interest as user1

Hibernate version: 2.1.6

Code between sessionFactory.openSession() and session.close():

Transaction tx = session.beginTransaction();
session.save(obj);
tx.commit();

Full stack trace of any exception that occurs:

net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 20, of class: criteria.Criteria
at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1677)
at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1443)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1470)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1393)
at net.sf.hibernate.engine.Cascades$4.cascade(Cascades.java:114)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:436)
at net.sf.hibernate.engine.Cascades.cascadeCollection(Cascades.java:526)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:452)
at net.sf.hibernate.engine.Cascades.cascade(Cascades.java:503)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:952)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:775)
at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
at dbUtil.Inserter.insert(Inserter.java:26)
at register.HandyModelAction.execute(HandyModelAction.java:42)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:184)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:833)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:732)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:619)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:688)
at java.lang.Thread.run(Thread.java:534)

Name and version of the database you are using:

MySQL 4.0



does somebody have an idea? thanks a lot for, help!

cheers rouven


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 21, 2004 5:51 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
well ... i'm far away from beeing a hibernate-expert ...but i'll try to answer in my bad english ...

This Exception occurs, like the exeption-texts 'says', when you try to associate (e.g. save) an object B with the same primary key as an object A (which is registered allready).

Therefore my question: What's the lifecycle of your hibernate-session? It's hard to understand for me, how this exeption can occur in your use-case.

gtx
curio


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 21, 2004 6:23 am 
Newbie

Joined: Wed Oct 06, 2004 3:20 am
Posts: 12
public static void insert (Object obj){

try {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.save(obj);
tx.commit();
HibernateUtil.closeSession();
}
catch (Exception e) {
e.printStackTrace();
}
}

***********************************************************
public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {

try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (HibernateException e) {
//log.error("Initial SessionFactory creation failed.", ex);
e.printStackTrace();
throw new ExceptionInInitializerError(e);

}
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet

if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
System.out.println(s);
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
System.out.println(s);
s.close();
}
}

**********************************************************

I hope this will help you to answer my question...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 21, 2004 7:51 am 
Expert
Expert

Joined: Tue Oct 05, 2004 9:45 am
Posts: 263
hmm ... i think i cannot help more than i've tried ...

but let us think another way ...

take a look at the object you're trying to insert ... perhaps this object has references to Instance A of Class A and Instance B of Class A which both have the same identifier-values.

gtx
curio


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 21, 2004 11:27 am 
Newbie

Joined: Wed Oct 06, 2004 3:20 am
Posts: 12
ok, we have solved the problem. the problem was that there were two objects in de http session with the same identifier. so we first remove one object bevor we create the second object.

so long...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.