views:

7455

answers:

6

I have two user Objects and while I try to save the object using

sessio.save(userObj)

I am getting the following error :

Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
[com.pojo.rtrequests.User#com.pojo.rtrequests.User@d079b40b]

I am creating the session using

BaseHibernateDAO dao = new BaseHibernateDAO();          

rtsession = dao.getSession(userData.getRegion(),
                           BaseHibernateDAO.RTREQUESTS_DATABASE_NAME);

rttrans = rtsession.beginTransaction();
rttrans.begin();

rtsession.save(userObj1);
rtsession.save(userObj2);

rtsession.flush();
rttrans.commit();

rtsession.close() // in finally block

I also tried doing the session.clear() before saving, still no luck.

This is for the first I am getting the session object when a user request comes, so i am getting why is saying that object is present in session.

Any suggestions?

A: 

Are your Id mappings correct? If the database is responsible for creating the Id through an identifier, you need to map your userobject to that ..

cwap
yes my mapping for id is correct and unique id is passed
harshit
+3  A: 

I have had this error many times and it can be quite hard to track down...

Basically, what hibernate is saying is that you have two objects which have the same identifier (same primary key) but they are not the same object.

I would suggest you break down your code, i.e. comment out bits until the error goes away and then put the code back until it comes back and you should find the error it.

It most often happens via cascading saves where there is a cascade save between object A and B, but object B has already been associated with the session with a different.

What primary key generator are you using?

The reason I ask is this error is related to how you're telling hibernate to ascertain the persistent state of an object (i.e. whether an object is persistent or not). The error could be happening because hibernate is trying to persist and object that is already persistent. In fact, if you use save hibernate will try and persist that object, and maybe there is already an object with that same primary key associated with the session.

Michael Wiles
@Michael Wiles: Nice Answer. Still it helps me after a year!!!
NooBDevelopeR
A: 

Another thing that worked for me was to make the instance variable Long in place of long


I had my primary key variable long id; changing it to Long id; worked

All the best

A: 

only get the object inside the session, example:

MyObject ob = null;

ob = (MyObject) session.get(MyObject.class, id);

A: 

This happens also when for example you query the object of certain class and don't close the session and try to save or update another object of the same class. So, you can either close the session after the query is done or load the object by session.load(Object.class, Object.getId) "Not So sure about this one".

PS: Remember to check if the session is closed, if so open another session before saving or updating :)

Object
A: 

This is only one point where hibernate makes more problems than it solves. In my case there are many objects with the same identifier 0, because they are new and don't have one. The db generates them. Somewhere I have read that 0 signals Id not set. The intuitive way to persist them is iterating over them and saying hibernate to save the objects. But You can't do that - "Of course You should know that hibernate works this and that way, therefore You have to.." So now I can try to change Ids to Long instead of long and look if it then works. In the end it's easier to do it with a simple mapper by your own, because hibernate is just an additional intransparent burden. Another example: Trying to read parameters from one database and persist them in another forces you to do nearly all work manually. But if you have to do it anyway, using hibernate is just additional work.

Hein