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)