It does not seem that hibernate will persist transient entities that serve as keys in maps when persisting the map, even with cascade="all."
I am new to hibernate, but can find no reference to this behavior in the most recent documentation; is this a bug or intended behavior? Is there a way around it?
Relevant info below - Thanks so much for any advice.
--Josh
Hibernate version:
3.2cr3
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 package="org.jei.example">
<class name="Parent" table="OBJECTS">
<id name="id" column="OBJECT_ID" access="field">
<generator class="assigned" />
</id>
<map name="store" access="field" cascade="all">
<key column="OBJECT_ID" />
<map-key-many-to-many class="ChildA" column="aid" />
<element type="double"/>
</map>
</class>
</hibernate-mapping>
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 package="org.jei.example">
<class name="ChildA" table="AOBJECTS">
<id name="id" column="OBJECT_ID" access="field">
<generator class="assigned" />
</id>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Nothing tricky going on here. All children are added to the parent before the session is opened - I'm relying upon cascade to dtrt once I persist the object; pseudocode is something like:
Code:
ChildA a1child = new ChildA(1);
ChildA a2child = new ChildA(2);
Parent parent = new Parent(1);
parent.addChild(a1child,4.0d);
parent.addChild(a2Child,2.0d);
...
HibernateUtil.getSessionFactory().getCurrentSession().save(parent);
The "Parent" class is basically just a wrapper around a map.
Log and errorEssentially, the log indicates that rows are being inserted into the table backing the map before the entities which are keys in the map...
Code:
17:51:19,014 DEBUG SchemaExport:303 - create table AOBJECTS (OBJECT_ID integer not null, primary key (OBJECT_ID))
17:51:19,154 DEBUG SchemaExport:303 - create table OBJECTS (OBJECT_ID integer not null, primary key (OBJECT_ID))
17:51:19,248 DEBUG SchemaExport:303 - create table store (OBJECT_ID integer not null, elt double precision, aid integer not null, primary key (OBJECT_ID, aid))
17:51:19,342 DEBUG SchemaExport:303 - alter table store add index FK68AF8E15198EE93 (OBJECT_ID), add constraint FK68AF8E15198EE93 foreign key (OBJECT_ID) references OBJECTS (OBJECT_ID)
17:51:19,623 DEBUG SchemaExport:303 - alter table store add index FK68AF8E194937BAF (aid), add constraint FK68AF8E194937BAF foreign key (aid) references AOBJECTS (OBJECT_ID)
17:51:19,936 INFO SchemaExport:196 - schema export complete
17:51:20,076 DEBUG SQL:393 - select parent_.OBJECT_ID from OBJECTS parent_ where parent_.OBJECT_ID=?
17:51:20,092 DEBUG IntegerType:80 - binding '1' to parameter: 1
17:51:20,123 DEBUG SQL:393 - insert into OBJECTS (OBJECT_ID) values (?)
17:51:20,123 DEBUG IntegerType:80 - binding '1' to parameter: 1
17:51:20,123 DEBUG SQL:393 - insert into store (OBJECT_ID, aid, elt) values (?, ?, ?)
17:51:20,123 DEBUG IntegerType:80 - binding '1' to parameter: 1
17:51:20,123 DEBUG IntegerType:80 - binding '1' to parameter: 2
17:51:20,123 DEBUG DoubleType:80 - binding '4.0' to parameter: 3
17:51:20,123 DEBUG SQL:393 - insert into store (OBJECT_ID, aid, elt) values (?, ?, ?)
17:51:20,123 DEBUG IntegerType:80 - binding '1' to parameter: 1
17:51:20,123 DEBUG IntegerType:80 - binding '2' to parameter: 2
17:51:20,123 DEBUG DoubleType:80 - binding '2.0' to parameter: 3
17:51:20,139 WARN JDBCExceptionReporter:71 - SQL Error: 1452, SQLState: 23000
17:51:20,139 ERROR JDBCExceptionReporter:72 - Duplicate key or integrity constraint violation message from server: "Cannot add or update a child row: a foreign key constraint fails (`test/store`, CONSTRAINT `FK68AF8E194937BAF` FOREIGN KEY (`aid`) REFERENCES `aobjects` (`OBJECT_ID`))"
17:51:20,139 ERROR AbstractFlushingEventListener:300 - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update