[Solved] org.hibernate.NonUniqueObjectException

org.hibernate.NonUniqueObjectException:

A different object with the same identifier value was already associated with the session : [in.javadomain.ContentVO#0]

 

When I put my code like this, I got the above “org.hibernate.NonUniqueObjectException” exception, because session opening and closing has been done outside the for each loop which caused this nonunique object exception.

 

[java]Set<ContentVO> extractedSaveSet = czc.getCouponsFrmCSV();

session = sessionFactory.openSession();

for (ContentVO content : extractedSaveSet) {
session.beginTransaction();

session.save(content);

session.getTransaction().commit();

}
session.close();

[/java]

 

So I changed it this way then issue got resolved.

 

[java]
Set<ContentVO> extractedSaveSet = czc.getCouponsFrmCSV();

for (ContentVO content : extractedSaveSet) {
session = sessionFactory.openSession();
session.beginTransaction();

session.save(content);

session.getTransaction().commit();

session.close();
}

[/java]

extractedSaveSet à Set which contains the ContentVO.

 

 

ContentVO – ORM VO.

I want to save all the ORM VO[ContentVO] values whichever present in the Set, for that I put one session and tried saving and committing everything[Kept session opening and closing outside the loop]. But I got the “org.hibernate.NonUniqueObjectException” exception. So I created session and closed it on individual VO saving which resolved this issue. [Created and closed the session inside the for each loop, so that it will be created separately for each row savings].

Leave a Reply