Hibernate version:
3.0.5
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Principal" table="my_principal">
<id name="id">
<generator class="native"/>
</id>
<property name="name" type="string">
<column name="name" length="32" not-null="true" unique="true"/>
</property>
<property name="description" type="string">
<column name="description" length="1024"/>
</property>
<set name="groups" table="my_group_person" inverse="true">
<key column="principal_id"/>
<many-to-many class="Group" column="group_id"/>
</set>
<joined-subclass name="Person" table="my_person">
<key column="id"/>
<property name="personID" type="string">
<column name="person_id" length="32" unique="true" not-null="true"/>
</property>
<property name="userName" type="string">
<column name="name" length="32" unique="true" not-null="true"/>
</property>
<property name="firstName" type="string">
<column name="first_name" length="32" not-null="true"/>
</property>
<property name="middleName" type="string">
<column name="middle_name" length="32"/>
</property>
<property name="lastName" type="string">
<column name="last_name" length="32" not-null="true"/>
</property>
<property name="emailAddress" type="string">
<column name="email_address" length="64"/>
</property>
<property name="primaryPhoneNumber" type="string">
<column name="primary_phone_number" length="32" />
</property>
<property name="password" type="string">
<column name="password" length="32"/>
</property>
</joined-subclass>
<joined-subclass name="Group" table="my_group">
<key column="id"/>
<set name="members" table="my_group_person">
<key column="group_id"/>
<many-to-many class="Principal" column="principal_id"/>
</set>
</joined-subclass>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():GroupTest:
Code:
public void testOne() {
try {
log.debug("creating a person");
Person person1 = new Person();
person1.setName("person 1");
person1.setPersonID("7652845");
person1.setFirstName("Person");
person1.setLastName("1");
personManager.createPerson(person1);
Group group1 = new Group();
group1.setName("group 1");
groupManager.createGroup(group1);
person1 = personManager.loadPerson("person 1");
group1 = groupManager.loadGroup("group 1");
group1.addMember(person1);
groupManager.updateGroup(group1);
person1 = (Person)personManager.loadPerson(person1.getId());
log.debug("loaded parent: "+person1.getId());
log.debug("name: "+person1.getName());
Collection groups = person1.getGroups();
Iterator i = groups.iterator();
while (i.hasNext()) {
Group cc = (Group)i.next();
log.debug(" in group: "+cc.getName());
}
group1 = (Group)groupManager.loadGroup(group1.getId());
log.debug("loaded child: "+person1.getId());
log.debug("name: "+group1.getName());
Collection members = group1.getMembers();
i = members.iterator();
while (i.hasNext()) {
Person pp = (Person)i.next();
log.debug(" member: "+pp.getName());
}
} catch (Exception e) {
e.printStackTrace();
fail("error");
} catch (Error e) {
e.printStackTrace();
fail("error");
}
}
public void testTwo() {
try {
log.debug("verifying person 1 exists");
Person person1 = (Person)personManager.loadPerson("person 1");
assertNotNull(person1);
log.debug("loaded parent: "+person1.getId());
log.debug("name: "+person1.getName());
Collection groups = person1.getGroups();
assertEquals(1, groups.size());
Iterator i = groups.iterator();
while (i.hasNext()) {
Group cc = (Group)i.next();
log.debug(" in group: "+cc.getName());
}
log.debug("deleting person");
{133} personManager.deletePerson("person 1");
log.debug("verifying that the person is gone, but the group remains with nothing in it");
Group group1 = (Group)groupManager.loadGroup("group 1");
assertNotNull(group1);
log.debug("loaded child: "+group1.getId());
log.debug("name: "+group1.getName());
Collection members = group1.getMembers();
assertEquals(0, members.size());
i = members.iterator();
while (i.hasNext()) {
Person pp = (Person)i.next();
log.debug(" member: "+pp.getName());
}
} catch (Exception e) {
e.printStackTrace();
fail("error");
} catch (Error e) {
e.printStackTrace();
fail("error");
}
}
Extending Spring HibernateDaoSupport and using inherited methods to create/update/delete. Simple persisters with nothing fancy. For example:
Code:
public void createGroup(Group g) throws PersisterException {
create(g);
}
public void updateGroup(Group g) throws PersisterException {
update(g);
}
Full stack trace of any exception that occurs:Code:
GroupLoadException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at GroupManager.loadGroup(GroupManager.java:114)
at GroupManager$$FastClassByCGLIB$$26c0884e.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:661)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:606)
at GroupManager$$EnhancerByCGLIB$$fe30085b.loadGroup(<generated>)
at GroupTest.testTwo(GroupTest.java:133)
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.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:325)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeInVM(JUnitTask.java:848)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:556)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:532)
at org.apache.tools.ant.Task.perform(Task.java:341)
at org.apache.commons.jelly.tags.ant.AntTag.doTag(AntTag.java:185)
at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:279)
at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:135)
at org.apache.commons.jelly.TagSupport.invokeBody(TagSupport.java:233)
at org.apache.commons.jelly.tags.core.IfTag.doTag(IfTag.java:88)
at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:279)
at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:135)
at org.apache.maven.jelly.tags.werkz.MavenGoalTag.runBodyTag(MavenGoalTag.java:79)
at org.apache.maven.jelly.tags.werkz.MavenGoalTag$MavenGoalAction.performAction(MavenGoalTag.java:110)
at com.werken.werkz.Goal.fire(Goal.java:639)
at com.werken.werkz.Goal.attain(Goal.java:575)
at com.werken.werkz.Goal.attainPrecursors(Goal.java:488)
at com.werken.werkz.Goal.attain(Goal.java:573)
at org.apache.maven.plugin.PluginManager.attainGoals(PluginManager.java:671)
at org.apache.maven.MavenSession.attainGoals(MavenSession.java:263)
at org.apache.maven.cli.App.doMain(App.java:488)
at org.apache.maven.cli.App.main(App.java:1239)
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 com.werken.forehead.Forehead.run(Forehead.java:551)
at com.werken.forehead.Forehead.main(Forehead.java:581)
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:48)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:711)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1315)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
at com.logicalapps.persistence.hibernate.GenericHibernatePersister.loadByKey(GenericHibernatePersister.java:158)
at GroupHibernatePersister.loadGroup(GroupHibernatePersister.java:49)
at GroupManager.loadGroup(GroupManager.java:110)
... 49 more
Caused by: java.sql.BatchUpdateException: Batch entry 0 delete from my_principal where id=1 was aborted. Call getNextException to see the cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2392)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1257)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:334)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2451)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
... 59 more
Name and version of the database you are using:PostgreSQL 8.0.3
The generated SQL (show_sql=true):Code:
Hibernate: select nextval ('hibernate_sequence')
Hibernate: select nextval ('hibernate_sequence')
Hibernate: insert into my_principal (name, description, id) values (?, ?, ?)
Hibernate: insert into my_person (person_id, name, first_name, middle_name, last_name, email_address, primary_phone_number, password, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into my_principal (name, description, id) values (?, ?, ?)
Hibernate: insert into my_group (id) values (?)
Hibernate: select this_.id as id0_, this_1_.name as name0_0_, this_1_.description as descript3_0_0_, this_.person_id as person2_2_0_, this_.name as name2_0_, this_.first_name as first4_2_0_, this_.middle_name as middle5_2_0_, this_.last_name as last6_2_0_, this_.email_address as email7_2_0_, this_.primary_phone_number as primary8_2_0_, this_.password as password2_0_ from my_person this_ inner join my_principal this_1_ on this_.id=this_1_.id where this_1_.name=?
Hibernate: select this_.id as id0_, this_1_.name as name0_0_, this_1_.description as descript3_0_0_ from my_group this_ inner join my_principal this_1_ on this_.id=this_1_.id where this_1_.name=?
Hibernate: select this_.id as id0_, this_1_.name as name0_0_, this_1_.description as descript3_0_0_, this_.person_id as person2_2_0_, this_.name as name2_0_, this_.first_name as first4_2_0_, this_.middle_name as middle5_2_0_, this_.last_name as last6_2_0_, this_.email_address as email7_2_0_, this_.primary_phone_number as primary8_2_0_, this_.password as password2_0_ from my_person this_ inner join my_principal this_1_ on this_.id=this_1_.id where this_.id=?
Hibernate: select this_.id as id0_, this_1_.name as name0_0_, this_1_.description as descript3_0_0_ from my_group this_ inner join my_principal this_1_ on this_.id=this_1_.id where this_.id=?
Hibernate: insert into my_group_person (group_id, principal_id) values (?, ?)
Hibernate: select this_.id as id0_, this_1_.name as name0_0_, this_1_.description as descript3_0_0_, this_.person_id as person2_2_0_, this_.name as name2_0_, this_.first_name as first4_2_0_, this_.middle_name as middle5_2_0_, this_.last_name as last6_2_0_, this_.email_address as email7_2_0_, this_.primary_phone_number as primary8_2_0_, this_.password as password2_0_ from my_person this_ inner join my_principal this_1_ on this_.id=this_1_.id where this_1_.name=?
Hibernate: select groups0_.principal_id as principal1_1_, groups0_.group_id as group2_1_, group1_.id as id0_, group1_1_.name as name0_0_, group1_1_.description as descript3_0_0_ from my_group_person groups0_ inner join my_group group1_ on groups0_.group_id=group1_.id left outer join my_principal group1_1_ on group1_.id=group1_1_.id where groups0_.principal_id=?
Hibernate: select this_.id as id0_, this_1_.name as name0_0_, this_1_.description as descript3_0_0_, this_.person_id as person2_2_0_, this_.name as name2_0_, this_.first_name as first4_2_0_, this_.middle_name as middle5_2_0_, this_.last_name as last6_2_0_, this_.email_address as email7_2_0_, this_.primary_phone_number as primary8_2_0_, this_.password as password2_0_ from my_person this_ inner join my_principal this_1_ on this_.id=this_1_.id where this_1_.name=?
Hibernate: delete from my_person where id=?
Hibernate: delete from my_principal where id=?
Debug level Hibernate log excerpt:Code:
java.sql.BatchUpdateException: Batch entry 0 delete from my_principal where id=1 was aborted. Call getNextException to see the cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2392)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1257)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:334)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2451)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:48)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:711)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1315)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
at com.logicalapps.persistence.hibernate.GenericHibernatePersister.loadByKey(GenericHibernatePersister.java:158)
at GroupHibernatePersister.loadGroup(GroupHibernatePersister.java:49)
at GroupManager.loadGroup(GroupManager.java:110)
at GroupManager$$FastClassByCGLIB$$26c0884e.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:661)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:606)
at GroupManager$$EnhancerByCGLIB$$fe30085b.loadGroup(<generated>)
at GroupTest.testTwo(GroupTest.java:133)
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.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:325)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeInVM(JUnitTask.java:848)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:556)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:532)
at org.apache.tools.ant.Task.perform(Task.java:341)
at org.apache.commons.jelly.tags.ant.AntTag.doTag(AntTag.java:185)
at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:279)
at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:135)
at org.apache.commons.jelly.TagSupport.invokeBody(TagSupport.java:233)
at org.apache.commons.jelly.tags.core.IfTag.doTag(IfTag.java:88)
at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:279)
at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:135)
at org.apache.maven.jelly.tags.werkz.MavenGoalTag.runBodyTag(MavenGoalTag.java:79)
at org.apache.maven.jelly.tags.werkz.MavenGoalTag$MavenGoalAction.performAction(MavenGoalTag.java:110)
at com.werken.werkz.Goal.fire(Goal.java:639)
at com.werken.werkz.Goal.attain(Goal.java:575)
at com.werken.werkz.Goal.attainPrecursors(Goal.java:488)
at com.werken.werkz.Goal.attain(Goal.java:573)
at org.apache.maven.plugin.PluginManager.attainGoals(PluginManager.java:671)
at org.apache.maven.MavenSession.attainGoals(MavenSession.java:263)
at org.apache.maven.cli.App.doMain(App.java:488)
at org.apache.maven.cli.App.main(App.java:1239)
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 com.werken.forehead.Forehead.run(Forehead.java:551)
at com.werken.forehead.Forehead.main(Forehead.java:581)
2005-08-12 22:29:30,518 WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: null
2005-08-12 22:29:30,518 ERROR org.hibernate.util.JDBCExceptionReporter - Batch entry 0 delete from my_principal where id=1 was aborted. Call getNextException to see the cause.
2005-08-12 22:29:30,518 WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 23503
2005-08-12 22:29:30,518 ERROR org.hibernate.util.JDBCExceptionReporter - ERROR: update or delete on "my_principal" violates foreign key constraint "fk752718c841b27ada" on "my_group_person"
2005-08-12 22:29:30,520 ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:63)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:181)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:48)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:711)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1315)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
at com.logicalapps.persistence.hibernate.GenericHibernatePersister.loadByKey(GenericHibernatePersister.java:158)
at GroupHibernatePersister.loadGroup(GroupHibernatePersister.java:49)
at GroupManager.loadGroup(GroupManager.java:110)
at GroupManager$$FastClassByCGLIB$$26c0884e.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:661)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:122)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:144)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:606)
at GroupManager$$EnhancerByCGLIB$$fe30085b.loadGroup(<generated>)
at GroupTest.testTwo(GroupTest.java:133)
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.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:325)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeInVM(JUnitTask.java:848)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:556)
at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:532)
at org.apache.tools.ant.Task.perform(Task.java:341)
at org.apache.commons.jelly.tags.ant.AntTag.doTag(AntTag.java:185)
at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:279)
at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:135)
at org.apache.commons.jelly.TagSupport.invokeBody(TagSupport.java:233)
at org.apache.commons.jelly.tags.core.IfTag.doTag(IfTag.java:88)
at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:279)
at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:135)
at org.apache.maven.jelly.tags.werkz.MavenGoalTag.runBodyTag(MavenGoalTag.java:79)
at org.apache.maven.jelly.tags.werkz.MavenGoalTag$MavenGoalAction.performAction(MavenGoalTag.java:110)
at com.werken.werkz.Goal.fire(Goal.java:639)
at com.werken.werkz.Goal.attain(Goal.java:575)
at com.werken.werkz.Goal.attainPrecursors(Goal.java:488)
at com.werken.werkz.Goal.attain(Goal.java:573)
at org.apache.maven.plugin.PluginManager.attainGoals(PluginManager.java:671)
at org.apache.maven.MavenSession.attainGoals(MavenSession.java:263)
at org.apache.maven.cli.App.doMain(App.java:488)
at org.apache.maven.cli.App.main(App.java:1239)
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 com.werken.forehead.Forehead.run(Forehead.java:551)
at com.werken.forehead.Forehead.main(Forehead.java:581)
Caused by: java.sql.BatchUpdateException: Batch entry 0 delete from my_principal where id=1 was aborted. Call getNextException to see the cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2392)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1257)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:334)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2451)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
... 59 more
Now the error is exepected, because the person is being deleted and the association table entry that references it is not. My question is how do I get hibernate to remove association table entries when an associated object is deleted so that this doesn't occur? I assume that there is a way to map this correctly so that hibernate can handle the association removal, but I have not found an example that works yet. It would be preferable to not have to remove the associations, update the Group object, then delete it.
The create works fine, the association is created fine, Group objects can be loaded and members retrieved, Principal objects can be loaded and groups retrieved. Everything is fine until I try to remove a person object. I can avoid this error by adding code to remove the prinicpal from the group it is in before trying to remove it (on line 133) like:
Code:
Group group1 = (Group)groupManager.loadGroup("group 1");
group1.removeMember(person1);
groupManager.updateGroup(group1);
Is that really what I need to do? Is there no way for hibernate to keep track of that association and remove the association table entries on the removal of an associated object?
I have tried different inverse settings, different cascade settings, etc. Can't cascading do the job of removing associations before removing the object? I had thought that a cascade setting of delete-orphan would do the trick (removing just the association when on of the associated objects was being removed), instead the object, association table entry *and* the other associated object are removed. That is not the behavior I am looking for either.
I have also tried to ensure that my classes correctly deal with the association collections:
Code:
Group:
public void addMember(Principal p) {
members.add(p);
p.getGroups().add(this);
}
public void removeMember(Principal p) {
members.remove(p);
p.getGroups().remove(this);
}
Principal:
public Set getGroups() {
return groups;
}
public void setGroups(Set groups) {
this.groups = groups;
}
Thanks in advance for any help.