I have an association between two tables, with a 1->M relationship from one to the other. I use the mapping file to create a map of this relationship with cascade set to "all-delete-orphan". When I use InnoDB tables, I cannot save an new object, filled with the map, and then saving within a transaction - I always get an Constraint violation. When the table was MyISAM, and there was no contraint enforcement, this didn't happen. I really need to move to InnoDB. Any help would be much appreciated.
Hibernate version: 3.2.4 sp1
Mapping documents:
Code:
<hibernate-mapping>
<class name="Role" table="roles">
<cache usage="read-write" />
<id name="id" type="int" unsaved-value="0">
<column name="id" length="11" not-null="true" unique="true" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="name" length="50" not-null="false" unique="true" />
</property>
<property name="createdDate" type="java.util.Date">
<column name="created_date" not-null="false" sql-type="timestamp" />
</property>
<property name="modifiedDate" type="java.util.Date">
<column name="modified_date" not-null="false" sql-type="timestamp" />
</property>
<map name="acls" lazy="false" table="acls" cascade="all-delete-orphan" order-by="dispatcher">
<key column="role_id" />
<index column="dispatcher" type="string" />
<one-to-many class="Acl" />
</map>
</class>
<class name="Acl" table="acl">
<cache usage="read-write" />
<id name="id" type="int" unsaved-value="0">
<column name="id" length="11" not-null="true" unique="true" />
<generator class="identity" />
</id>
<property name="role" type="int">
<column name="role_id" not-null="true" />
</property>
<property name="dispatcher" type="java.lang.String">
<column name="dispatcher" length="10" not-null="true" />
</property>
<property name="readWrite" type="boolean">
<column name="read_write" not-null="false" />
</property>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
// map of new acls already made
Role role = new Role();
role.setAcls(map);
try {
tx = ds.beginTransaction();
ds.saveOrUpdate(role);
tx.commit();
completed = true;
} catch (HibernateException he) {
if (tx != null)
tx.rollback();
}
Full stack trace of any exception that occurs:Code:
Jun 15 22:36:47 org.hibernate.util.JDBCExceptionReporter[DEBUG]: could not insert: [Acl] [insert into acl (role_id, dispatcher, read_write) values (?, ?, ?)]
com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`shazam/acl`, CONSTRAINT `FK1526B9AB69746F3` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`))
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3249)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1541)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1455)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1440)
at com.mysql.jdbc.jdbc2.optional.PreparedStatementWrapper.executeUpdate(PreparedStatementWrapper.java:840)
at com.caucho.sql.UserPreparedStatement.executeUpdate(UserPreparedStatement.java:117)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at InitialSetup.generateLoggingTables(InitialSetup.java:239)
at InitialSetup.init(InitialSetup.java:69)
at GlobalData.init(GlobalData.java:250)
at ConfigServlet.init(ConfigServlet.java:30)
at com.caucho.server.dispatch.ServletConfigImpl.createServletImpl(ServletConfigImpl.java:646)
at com.caucho.server.dispatch.ServletConfigImpl.createServlet(ServletConfigImpl.java:587)
at com.caucho.server.dispatch.ServletManager.init(ServletManager.java:154)
at com.caucho.server.webapp.Application.start(Application.java:1654)
at com.caucho.server.deploy.DeployController.startImpl(DeployController.java:621)
at com.caucho.server.deploy.StartAutoRedeployAutoStrategy.startOnInit(StartAutoRedeployAutoStrategy.java:72)
at com.caucho.server.deploy.DeployController.startOnInit(DeployController.java:509)
at com.caucho.server.deploy.DeployContainer.start(DeployContainer.java:153)
at com.caucho.server.webapp.ApplicationContainer.start(ApplicationContainer.java:670)
at com.caucho.server.host.Host.start(Host.java:420)
at com.caucho.server.deploy.DeployController.startImpl(DeployController.java:621)
at com.caucho.server.deploy.StartAutoRedeployAutoStrategy.startOnInit(StartAutoRedeployAutoStrategy.java:72)
at com.caucho.server.deploy.DeployController.startOnInit(DeployController.java:509)
at com.caucho.server.deploy.DeployContainer.start(DeployContainer.java:153)
at com.caucho.server.host.HostContainer.start(HostContainer.java:504)
at com.caucho.server.resin.ServletServer.start(ServletServer.java:971)
at com.caucho.server.deploy.DeployController.startImpl(DeployController.java:621)
at com.caucho.server.deploy.AbstractDeployControllerStrategy.start(AbstractDeployControllerStrategy.java:56)
at com.caucho.server.deploy.DeployController.start(DeployController.java:517)
at com.caucho.server.resin.ResinServer.start(ResinServer.java:551)
at com.caucho.server.resin.Resin.init(Resin.java)
at com.caucho.server.resin.Resin.main(Resin.java:625)
Jun 15 22:36:47 org.hibernate.util.JDBCExceptionReporter[WARN]: SQL Error: 1452, SQLState: 23000
Jun 15 22:36:47 org.hibernate.util.JDBCExceptionReporter[ERROR]: Cannot add or update a child row: a foreign key constraint fails (`shazam/acl`, CONSTRAINT `FK1526B9AB69746F3` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`))
Name and version of the database you are using:MySQL 5.0.27 Max
The generated SQL (show_sql=true):Code:
Hibernate: insert into roles (name, created_date, modified_date) values (?, ?, ?)
Hibernate: insert into acl (role_id, dispatcher, read_write) values (?, ?, ?)
Hibernate: insert into acl (role_id, dispatcher, read_write) values (?, ?, ?)
Hibernate: insert into acl (role_id, dispatcher, read_write) values (?, ?, ?)
Debug level Hibernate log excerpt:Code:
Jun 15 22:36:47 org.hibernate.jdbc.AbstractBatcher[DEBUG]: about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
Jun 15 22:36:47 org.hibernate.jdbc.ConnectionManager[DEBUG]: opening JDBC connection
Jun 15 22:36:47 org.hibernate.SQL[DEBUG]: insert into roles (name, created_date, modified_date) values (?, ?, ?)
Jun 15 22:36:47 org.hibernate.jdbc.AbstractBatcher[DEBUG]: preparing statement
Jun 15 22:36:47 org.hibernate.persister.entity.AbstractEntityPersister[DEBUG]: Dehydrating entity: [Role#<null>]
Jun 15 22:36:47 org.hibernate.type.StringType[DEBUG]: binding 'ADMIN' to parameter: 1
Jun 15 22:36:47 org.hibernate.type.TimestampType[DEBUG]: binding '2007-06-15 22:36:47' to parameter: 2
Jun 15 22:36:47 org.hibernate.type.TimestampType[DEBUG]: binding '2007-06-15 22:36:47' to parameter: 3
Jun 15 22:36:47 org.hibernate.id.IdentifierGeneratorFactory[DEBUG]: Natively generated identity: 1
Jun 15 22:36:47 org.hibernate.jdbc.AbstractBatcher[DEBUG]: about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
Jun 15 22:36:47 org.hibernate.jdbc.AbstractBatcher[DEBUG]: closing statement
Jun 15 22:36:47 org.hibernate.jdbc.ConnectionManager[DEBUG]: aggressively releasing JDBC connection
Jun 15 22:36:47 org.hibernate.jdbc.ConnectionManager[DEBUG]: releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
Jun 15 22:36:47 org.hibernate.engine.Cascade[DEBUG]: processing cascade ACTION_SAVE_UPDATE for: Role
Jun 15 22:36:47 org.hibernate.engine.Cascade[DEBUG]: cascade ACTION_SAVE_UPDATE for collection: Role.acls
Jun 15 22:36:47 org.hibernate.engine.CascadingAction[DEBUG]: cascading to saveOrUpdate: Acl
Jun 15 22:36:47 org.hibernate.engine.IdentifierValue[DEBUG]: id unsaved-value: 0
Jun 15 22:36:47 org.hibernate.event.def.AbstractSaveEventListener[DEBUG]: transient instance of: Acl
Jun 15 22:36:47 org.hibernate.event.def.DefaultSaveOrUpdateEventListener[DEBUG]: saving transient instance
Jun 15 22:36:47 org.hibernate.event.def.AbstractSaveEventListener[DEBUG]: saving [Acl#<null>]
Jun 15 22:36:47 org.hibernate.event.def.AbstractSaveEventListener[DEBUG]: executing insertions
Jun 15 22:36:47 org.hibernate.event.def.AbstractSaveEventListener[DEBUG]: executing identity-insert immediately
Jun 15 22:36:47 org.hibernate.cache.UpdateTimestampsCache[DEBUG]: Pre-invalidating space [acl]
Jun 15 22:36:47 org.hibernate.persister.entity.AbstractEntityPersister[DEBUG]: Inserting entity: Acl (native id)
Jun 15 22:36:47 org.hibernate.jdbc.AbstractBatcher[DEBUG]: about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
Jun 15 22:36:47 org.hibernate.jdbc.ConnectionManager[DEBUG]: opening JDBC connection
Jun 15 22:36:47 org.hibernate.SQL[DEBUG]: insert into acl (role_id, dispatcher, read_write) values (?, ?, ?)
Jun 15 22:36:47 org.hibernate.jdbc.AbstractBatcher[DEBUG]: preparing statement
Jun 15 22:36:47 org.hibernate.persister.entity.AbstractEntityPersister[DEBUG]: Dehydrating entity: [Acl#<null>]
Jun 15 22:36:47 org.hibernate.type.IntegerType[DEBUG]: binding '0' to parameter: 1
Jun 15 22:36:47 org.hibernate.type.StringType[DEBUG]: binding 'acl' to parameter: 2
Jun 15 22:36:47 org.hibernate.type.BooleanType[DEBUG]: binding 'true' to parameter: 3
Jun 15 22:36:47 org.hibernate.jdbc.AbstractBatcher[DEBUG]: about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
Jun 15 22:36:47 org.hibernate.jdbc.AbstractBatcher[DEBUG]: closing statement
Jun 15 22:36:47 org.hibernate.jdbc.ConnectionManager[DEBUG]: aggressively releasing JDBC connection
Jun 15 22:36:47 org.hibernate.jdbc.ConnectionManager[DEBUG]: releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
Jun 15 22:36:47 org.hibernate.util.JDBCExceptionReporter[DEBUG]: could not insert: [Acl] [insert into acl (role_id, dispatcher, read_write) values (?, ?, ?)]