Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.05
Mapping documents:
GroupOfApplicants.hbm.xml
<hibernate-mapping>
<class
name="org.tbag.dom.GroupOfApplicants"
table="GroupOfApplicants">
<id
name="id"
column="ID"
type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">GroupOfApplicants_SEQ</param>
</generator>
</id>
<version
name="latestVersion"
column="LATEST_VERSION"
type="int"
unsaved-value="negative" />
<set
name="applications"
inverse="true"
cascade="save-update"
lazy="true"
sort="unsorted">
<key column="GROUP_OF_APPLICANTS_ID"></key>
<one-to-many
class="org.tbag.dom.Application" />
</set>
<set
name="customers"
table="CUSTOMER_GROUP_OF_APPLICANTS"
lazy="true"
cascade="save-update"
sort="unsorted">
<key column="GROUP_OF_APPLICANTS_ID"></key>
<many-to-many
class="org.tbag.dom.Customer"
column="CUSTOMER_ID"
outer-join="auto" />
</set>
<property
name="lastUpdatedBy"
type="string"
update="true"
insert="true"
column="LAST_UPDATED_BY"
length="20" />
<property
name="lastUpdated"
type="date"
update="true"
insert="true"
column="LAST_UPDATED" />
</class>
</hibernate-mapping>
Application.hbm.xml
<hibernate-mapping>
<class
name="org.tbag.dom.Application"
table="Application">
<id
name="id"
column="ID"
type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">Application_SEQ</param>
</generator>
</id>
<version
name="latestVersion"
column="LATEST_VERSION"
type="int"
unsaved-value="negative"/>
<property
name="applicationNumber"
type="java.lang.String"
update="true"
insert="true"
column="applicationNumber"/>
<property
name="existingCustomer"
type="boolean"
update="true"
insert="true"
column="existingCustomer"/>
<many-to-one
name="groupOfApplicants"
class="org.tbag.dom.GroupOfApplicants"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="GROUP_OF_APPLICANTS_ID"
not-null="true"/>
<property
name="lastUpdatedBy"
type="string"
update="true"
insert="true"
column="LAST_UPDATED_BY"
length="20"/>
<property
name="lastUpdated"
type="date"
update="true"
insert="true"
column="LAST_UPDATED"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
public void testSaveGroupOfApplicants()
{
// Setup fixture
BaseDAOHibernate groupOfApplicantsDAO = new GroupOfApplicantsDAO();
// deleteFromTables();
Application a1 = new Application();
a1.setApplicationNumber("num1");
a1.setExistingCustomer(true);
Application a2 = new Application();
a2.setApplicationNumber("num2");
a2.setExistingCustomer(false);
GroupOfApplicants goa = new GroupOfApplicants();
goa.addApplication(a1);
goa.addApplication(a2);
// Execute method under test
groupOfApplicantsDAO.saveObject(goa);
// Verify results
assertNotNull(goa.getId());
}
Full stack trace of any exception that occurs:
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:782)
at java.util.HashMap$KeyIterator.next(HashMap.java:818)
at org.hibernate.engine.Cascades.cascadeCollection(Cascades.java:860)
at org.hibernate.engine.Cascades.cascade(Cascades.java:739)
at org.hibernate.engine.Cascades.cascade(Cascades.java:817)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:361)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:263)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:158)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:96)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:416)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:411)
at org.springframework.orm.hibernate3.HibernateTemplate$18.doInHibernate(HibernateTemplate.java:673)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:358)
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:670)
at com.nr.infrastructure.dao.hibernate.BaseDAOHibernate.saveObject(BaseDAOHibernate.java:67)
at org.tbag.dom.GroupOfApplicantsTest.testSaveGroupOfApplicants(GroupOfApplicantsTest.java:40)
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 junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Name and version of the database you are using: Oracle 10.1.0.2.0
Other comments:
Note GroupOfApplicants object uses a HashSet, as the implementation of the Set used to reference associated Application objects.
I have done some investigation and run some tests and have discovered the following:
1. If there is only one Application object injected into the GroupOfApplicants object the code works.
2. If I remove the "version" properties from both GroupOfApplicants and Application hbm files (and the database) then this code works.