• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Caught a NonUnique Exception while adding more rows to the table.

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am trying to insert many rows to database and delete many rows from database using hibernate sessions.but if i enter some 4 rows it will work fine,but if i enter many rows(say 10) it is giving this exception.
Please help me...
Thanks in Advance.....
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.bvn.finance.services.treatment.collectors.Collector#12]
at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:556)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:258)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:216)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:502)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:494)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:134)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
at org.hibernate.engine.Cascade.cascade(Cascade.java:223)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.cascadeOnUpdate(DefaultSaveOrUpdateEventListener.java:331)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:303)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:216)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:502)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:494)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:134)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:385)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:242)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)


 
Amith Bhavikatti
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help me Guys................
 
Author
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you take the flush and evict calls out?
 
Amith Bhavikatti
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Javid,

Thanks for your reply.
I have modified my code but still that exception comes randomly any time.
Please suggest me.
Here is my code:
<code>
public void insDelAssignmentByLetters(ArrayList delAssignmentByLetterList,ArrayList addingtoAssignmentByLetterList,String remoteUsername)
{
if(log.isDebugEnabled()) log.debug("entering addAssignmentByLetters()....");
Session session = HibernateUtil.getCurrentUtil().getCurrentSession();
Transaction tx = session.beginTransaction();
try
{
for ( int i=0; i< delAssignmentByLetterList.size(); i++)
{
AssignmentByLetter delAssignList = (AssignmentByLetter) delAssignmentByLetterList.get(i);
deleteAsgLetter(delAssignList,remoteUsername,session);
}

for ( int i=0; i< addingtoAssignmentByLetterList.size(); i++)
{
AssignmentByLetter addingAssignmentByLettersList = (AssignmentByLetter) addingtoAssignmentByLetterList.get(i);
insertToAsgLetter(addingAssignmentByLettersList,remoteUsername,session);
}
}
catch(HibernateException ex)
{
log.error("Caught a hibernate exception while adding AssignmentByLetter.",ex);
throw new DataAccessException("addAssignmentByLetters","AssignmentByLetter");
}

finally
{
tx.commit();
session.close();
}

}

private void deleteAsgLetter(AssignmentByLetter deleteAssignObj,String remoteUsername,Session session){

try
{
if(log.isDebugEnabled()) log.debug("Remote UserName : "+remoteUsername);
if(log.isDebugEnabled()) log.debug("Deleting From AssignmentByLetter Table..."+ "AssignmentID --> " + deleteAssignObj.getAssignmentID());
session.getNamedQuery("deleteAssignmentByLetter").setInteger("assignmentIDD",deleteAssignObj.getAssignmentID()).executeUpdate();
}
catch(HibernateException ex)
{
log.error("Caught a hibernate exception in deleteAsgLetter(): ",ex);
throw new DataAccessException("deleteAsgLetter","AssignmentByLetter");
}
}

private void insertToAsgLetter(AssignmentByLetter insertObj,String remoteUsername,Session session)
{

try{
if(log.isDebugEnabled()) log.debug("entering insertToAsgLetter()....");
if(log.isDebugEnabled()) log.debug("Remote UserName : "+remoteUsername);
if(log.isDebugEnabled()) log.debug("Inserting into AssignmentByLetter Table..."+ "StartValue --> " + insertObj.getStartValue());
if(log.isDebugEnabled()) log.debug("Inserting into AssignmentByLetter Table..."+ "EndValue --> " + insertObj.getEndValue());
if(log.isDebugEnabled()) log.debug("Inserting into AssignmentByLetter Table..."+ "CollectorId --> " + insertObj.getCollector().getCollectorID());
if(log.isDebugEnabled()) log.debug("Inserting into AssignmentByLetter Table..."+ "GroupId --> " + insertObj.getGroup().getGroupID());
insertObj.setCreated(new Date());
session.saveOrUpdate(insertObj);
}
catch(HibernateException ex)
{
log.error("Caught a hibernate exception in insertToAsgLetter(): ",ex);
throw new DataAccessException("insertToAsgLetter","AssignmentByLetter");
}

}
</code>
 
Javid Jamae
Author
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amith, please fix your code block in your last e-mail.

-Are you perhaps creating a new SessionFactory for each user or unit of work?
-Are you hitting this error when multiple users/threads are on the system?
-Do you have anything else that might be writing data into that table?
-Can you show us the mapping file and what key generation strategy you're using?
-What database are you using?
 
Amith Bhavikatti
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Javid,

I am using singleton on sessionFactory.
I am getting this error for a single user login.
no only delete and insert.
This is the mapping file code


And this is the code written to delete and insert into database.
 
Something must be done about this. Let's start by reading this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic