-->
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.  [ 1 post ] 
Author Message
 Post subject: I'm not understanding this NonUniqueObjectException! Help?
PostPosted: Thu Oct 08, 2009 4:45 pm 
Newbie

Joined: Thu Oct 08, 2009 4:33 pm
Posts: 1
Hi all,

This program is taking in a jaxb object and inspecting it and putting its values into a database.

The relevant tables are SystemRequirements, LanguageFilters and Languages

LanguageFilters has a foreign key into SystemRequirements as well as one into Languages.

The program creates the SystemRequirement first, and it does this fine. Then it looks at the incoming LanguageFilters in the jaxb object and tries to look for those Languages in our Language table ( a pre-populated table). Then once it has found the language it's looking for, I create a LanguageFilter Bean and try to save it. That's when all hell breaks loose with the NonUniqueObjectException.

Does anyone have any idea of why this would happen? Here is the code and the stacktrace. Thanks!!

Code:
public class Test{
   
   static Session session;
   
   private static void insertSystemRequirement(SystemReqsType sysReq)
   {
      SystemRequirements dbSysReq = new SystemRequirements();
      
      dbSysReq.setRequireAdmin(sysReq.isRequireAdmin());      
      dbSysReq.setJreCheck(sysReq.isJreCheck());
      dbSysReq.setXpServicePack(new Integer(sysReq.getXpservicepack().intValue()));
      dbSysReq.setProductName(sysReq.getProductName());
      dbSysReq.setCreatedAt(new Date());
      dbSysReq.setUpdatedAt(new Date());
      session.save(dbSysReq);
      setLangFiltersForSysReq(sysReq.getLangFilter(), dbSysReq);
   }
   
   private static void setLangFiltersForSysReq(List<String> list, SystemRequirements dbSysReq)
   {
      
      for (String s : list )
      {
         LanguageFilters lf = new LanguageFilters();
         lf.setCreatedAt(new Date());
         lf.setUpdatedAt(new Date());
         lf.setSystemRequirements(dbSysReq);
         Languages l = (Languages)session.createQuery("from Languages where name = '"+s+"'" ).uniqueResult();
         if (l!=null)
         {
            lf.setLanguages(l);
            session.save(lf); //THIS IS LINE 52, FYI
         }
      }
      
   }
   
   
    public static void main(String[] args)
    {
       session = HibernateUtil.getSessionFactory().getCurrentSession();
       session.beginTransaction();
       insertSystemRequirement(alreadyCreatedJAXBObject);
       session.getTransaction().commit();
    }
}


Code:
Exception in thread "main" org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [beans.LanguageFilters#0]
   at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:191)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:144)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
   at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
   at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
   at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:562)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:550)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:546)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
   at $Proxy0.save(Unknown Source)
   at Test.setLangFiltersForSysReq(Test.java:52)
   at Test.insertSystemRequirement(Test.java:36)
   at Test.main(Test.java:63)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.