Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp
I have a many to many relationship between two objects resolved with a join table. I keep on getting NonUniqueObjectExceptions whenever I try to add to one side of the collection:
My code is as follows:
List updatedPayPlans = new ArrayList();
for(int i = 0; i < 2; i++){
OracleCompanyPolicyType cotype = new OracleCompanyPolicyType();
cotype.setCompanyNumber("08");
cotype.setEffectiveDate(new Date());
cotype.setPolicyType(new Integer(10));
cotype.setState("TX");
cotype.setTerm(new Integer(12));
cotype.setCompanyId(new Long(1286));
List payPlans = dao.findPayPlans("01");
for(int j = 0; j < payPlans.size(); j++){
OraclePayPlan payPlan = (OraclePayPlan)payPlans.get(j);
payPlan.addCompanyPolicyType(cotype);
updatedPayPlans.add(payPlan);
}
}
dao.saveBillingOperation(updatedPayPlans);
and my dao:
for(int i = 0; i < ops.size(); i++){
OraclePayPlan plan = (OraclePayPlan)ops.get(i);
//session.lock(plan, LockMode.NONE);
session.saveOrUpdate(plan);
//session.evict(plan);
}
In this test case there are two objects of type OraclePayPlan. I want to add to their respective CompanyPolicyType collection. I then populate an array list with all the updated pay plans and pass it off to a backend DAO. The problem is that Hibernate does not allow me to have a pay plan with the same identifier in the session. In this use case, a pay plan can have many company policy types. So I don't understand why Hibernate is still giving me this exception.
I've been battling this one since yesterday. If anyone knows please let me knowl Thanks
Hibernate version:
2.18
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="com.uaig.migration.oracle10g.vo.OraclePayPlan"
table="PAY_PLANS"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="payPlanId"
column="PAY_PLAN_ID"
type="java.lang.Long"
unsaved-value="0"
>
<generator class="sequence">
<param name="sequence">PAYPLAN_SEQ</param>
</generator>
</id>
<set
name="companyPolicyTypes"
table="CO_PLCY_TYPES_PAY_PLANS"
lazy="false"
inverse="false"
cascade="all"
sort="unsorted"
>
<key
column="PAY_PLAN_ID"
/>
<many-to-many
class="com.uaig.migration.oracle10g.vo.OracleCompanyPolicyType"
column="CO_POLTYPE_ID"
outer-join="false"
/>
</set>
<property
name="code"
type="java.lang.String"
update="true"
insert="true"
column="CODE"
length="2"
/>
<property
name="description"
type="java.lang.String"
update="true"
insert="true"
column="DESCRIPTION"
length="15"
/>
<property
name="downPayment"
type="java.math.BigDecimal"
update="true"
insert="true"
column="DOWN_PAYMENT"
length="5"
/>
<property
name="downDays"
type="java.lang.Integer"
update="true"
insert="true"
column="DOWN_DAYS"
length="3"
/>
<property
name="firstPercent"
type="java.math.BigDecimal"
update="true"
insert="true"
column="FIRST_PERCENT"
length="5"
/>
<property
name="firstDays"
type="java.lang.Integer"
update="true"
insert="true"
column="FIRST_DAYS"
length="3"
/>
<property
name="secondPercent"
type="java.math.BigDecimal"
update="true"
insert="true"
column="SECOND_PERCENT"
length="5"
/>
<property
name="secondDays"
type="java.lang.Integer"
update="true"
insert="true"
column="SECOND_DAYS"
length="3"
/>
<property
name="thirdPercent"
type="java.math.BigDecimal"
update="true"
insert="true"
column="THIRD_PERCENT"
length="5"
/>
<property
name="thirdDays"
type="java.lang.Integer"
update="true"
insert="true"
column="THIRD_DAYS"
length="3"
/>
<property
name="fourthPercent"
type="java.math.BigDecimal"
update="true"
insert="true"
column="FOURTH_PERCENT"
length="5"
/>
<property
name="fourthDays"
type="java.lang.Integer"
update="true"
insert="true"
column="FOURTH_DAYS"
length="3"
/>
<property
name="fifthPercent"
type="java.math.BigDecimal"
update="true"
insert="true"
column="FIFTH_PERCENT"
length="5"
/>
<property
name="fifthDays"
type="java.lang.Integer"
update="true"
insert="true"
column="FIFTH_DAYS"
length="3"
/>
<property
name="sixthPercent"
type="java.math.BigDecimal"
update="true"
insert="true"
column="SIXTH_PERCENT"
length="5"
/>
<property
name="sixthDays"
type="java.lang.Integer"
update="true"
insert="true"
column="SIXTH_DAYS"
length="3"
/>
<property
name="seventhPercent"
type="java.math.BigDecimal"
update="true"
insert="true"
column="SEVENTH_PERCENT"
length="5"
/>
<property
name="seventhDays"
type="java.lang.Integer"
update="true"
insert="true"
column="SEVENTH_DAYS"
length="3"
/>
<property
name="eighthPercent"
type="java.math.BigDecimal"
update="true"
insert="true"
column="EIGHTH_PERCENT"
length="5"
/>
<property
name="eighthDays"
type="java.lang.Integer"
update="true"
insert="true"
column="EIGHTH_DAYS"
length="3"
/>
<property
name="ninethPercent"
type="java.math.BigDecimal"
update="true"
insert="true"
column="NINETH_PERCENT"
length="5"
/>
<property
name="ninethDays"
type="java.lang.Integer"
update="true"
insert="true"
column="NINETH_DAYS"
length="3"
/>
<property
name="tenthPercent"
type="java.math.BigDecimal"
update="true"
insert="true"
column="TENTH_PERCENT"
length="5"
/>
<property
name="tenthDays"
type="java.lang.Integer"
update="true"
insert="true"
column="TENTH_DAYS"
length="3"
/>
<property
name="eleventhPercent"
type="java.math.BigDecimal"
update="true"
insert="true"
column="ELEVENTH_PERCENT"
length="5"
/>
<property
name="eleventhDays"
type="java.lang.Integer"
update="true"
insert="true"
column="ELEVENTH_DAYS"
length="3"
/>
<property
name="twelvethPercent"
type="java.math.BigDecimal"
update="true"
insert="true"
column="TWELVETH_PERCENT"
length="5"
/>
<property
name="twelethDays"
type="java.lang.Integer"
update="true"
insert="true"
column="TWELETH_DAYS"
length="3"
/>
<property
name="replacedPayPlan"
type="java.lang.String"
update="true"
insert="true"
column="REPLACED_PAY_PLAN"
length="2"
/>
<property
name="nonpayCancelFlag"
type="java.lang.String"
update="true"
insert="true"
column="NONPAY_CANCEL_FLAG"
length="1"
/>
<property
name="installmentBillingFee"
type="java.math.BigDecimal"
update="true"
insert="true"
column="INSTALLMENT_BILLING_FEE"
length="5"
/>
<property
name="billingInterestRate"
type="java.math.BigDecimal"
update="true"
insert="true"
column="BILLING_INTEREST_RATE"
length="5"
/>
<property
name="midTermPercent"
type="java.math.BigDecimal"
update="true"
insert="true"
column="MID_TERM_PERCENT"
length="3"
/>
<property
name="daysToBill"
type="java.lang.Integer"
update="true"
insert="true"
column="DAYS_TO_BILL"
length="3"
/>
<property
name="midTermAmount"
type="java.math.BigDecimal"
update="true"
insert="true"
column="MID_TERM_AMOUNT"
length="6"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-OraclePayPlan.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="com.uaig.migration.oracle10g.vo.OracleCompanyPolicyType"
table="COMPANY_POLICY_TYPES"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="companyPolicyTypeId"
column="CO_POLTYPE_ID"
type="java.lang.Long"
unsaved-value="0"
>
<generator class="sequence">
<param name="sequence">BILLINGOP_SEQ</param>
</generator>
</id>
<set
name="payPlans"
table="CO_PLCY_TYPES_PAY_PLANS"
lazy="true"
inverse="true"
cascade="all"
sort="unsorted"
>
<key
column="CO_POLTYPE_ID"
/>
<many-to-many
class="com.uaig.migration.oracle10g.vo.OraclePayPlan"
column="PAY_PLAN_ID"
outer-join="false"
/>
</set>
<property
name="companyNumber"
type="java.lang.String"
update="true"
insert="true"
column="COMPANY_NUMBER"
length="2"
/>
<property
name="state"
type="java.lang.String"
update="true"
insert="true"
column="STATE"
length="2"
/>
<property
name="policyType"
type="java.lang.Integer"
update="true"
insert="true"
column="POLICY_TYPE"
length="2"
/>
<property
name="payPlanId"
type="java.lang.Long"
update="true"
insert="true"
column="PAY_PLAN_ID"
length="4"
/>
<property
name="effectiveDate"
type="java.util.Date"
update="true"
insert="true"
column="EFFECTIVE_DATE"
length="7"
/>
<property
name="term"
type="java.lang.Integer"
update="true"
insert="true"
column="TERM"
length="3"
/>
<property
name="companyId"
type="java.lang.Long"
update="true"
insert="true"
column="COMPANY_ID"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-OracleCompanyPolicyType.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
public Integer saveBillingOperation(final List ops) {
HibernateTemplate template = getHibernateTemplate();
Object obj = template.execute(
new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException {
//saveList(ops, session);
for(int i = 0; i < ops.size(); i++){
OraclePayPlan plan = (OraclePayPlan)ops.get(i);
//session.lock(plan, LockMode.NONE);
session.saveOrUpdate(plan);
//session.evict(plan);
}
return new Integer(ops.size());
}
});
log.info("There were " + ((Integer)obj).intValue() + " billing operation records saved");
return (Integer)obj;
}
Full stack trace of any exception that occurs:
org.springframework.orm.hibernate.HibernateSystemException: a different object with the same identifier value was already associated with the session: 3056, of class: com.uaig.migration.oracle10g.vo.OraclePayPlan; nested exception is net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 3056, of class: com.uaig.migration.oracle10g.vo.OraclePayPlan
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 3056, of class: com.uaig.migration.oracle10g.vo.OraclePayPlan
at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1687)
at net.sf.hibernate.impl.SessionImpl.doUpdateMutable(SessionImpl.java:1453)
at net.sf.hibernate.impl.SessionImpl.doUpdate(SessionImpl.java:1480)
at net.sf.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:1403)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.springframework.orm.hibernate.HibernateTemplate$CloseSuppressingInvocationHandler.invoke(HibernateTemplate.java:1196)
at $Proxy1.saveOrUpdate(Unknown Source)
at com.uaig.migration.dao.OracleMigrationDAOImpl$13.doInHibernate(OracleMigrationDAOImpl.java:263)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:312)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:289)
at com.uaig.migration.dao.OracleMigrationDAOImpl.saveBillingOperation(OracleMigrationDAOImpl.java:256)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:282)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:155)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:56)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:174)
at $Proxy0.saveBillingOperation(Unknown Source)
at com.uaig.migration.dao.TestOracleMigrationDao.testSaveCompanyPolicyType(TestOracleMigrationDao.java:130)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)
Name and version of the database you are using:
Oracle 10g
The generated SQL (show_sql=true):
N/A
Debug level Hibernate log excerpt:N/A