I'm trying to understand how to use the any and many-to-any mappings, but haven't found any complete examples of how they should be used. I tried to put together a simple test, but can't get it working.
The idea I'm trying to implement is that two unrelated objects can have collections of children of the same type. For this example, I created ParentA and ParentB (only parent A is included below because B is an identical copy).
The parent tables have only an ID column, and the child table has an ID column, parent entity name, and parent primary key column.
Sorry for the long post...
Here is the example code:
Quote:
Code:
public class ParentA {
protected int parentAId;
protected Set children;
public int getParentAId() {
return this.parentAId;
}
public void setParentAId(int parentAId) {
this.parentAId = parentAId;
}
public Set getChildren() {
return children;
}
public void setChildren(Set children) {
this.children = children;
}
}
Quote:
Code:
public class Child {
protected int childId;
protected String parentEntityName;
protected int parentId;
protected Object parent;
public int getChildId() {
return this.childId;
}
public void setChildId(int childId) {
this.childId = childId;
}
public String getParentTableName() {
return this.parentTableName;
}
public void setParentEntityName(String parentEntityName) {
this.parentEntityName = parentEntityName;
}
public int getParentId() {
return this.parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
public Object getParent() {
return parent;
}
public void setParent(Object parent) {
this.parent = parent;
}
}
And mapping files:
Quote:
Code:
<hibernate-mapping>
<class name="test.ParentA" table="PARENT_A" dynamic-update="false" dynamic-insert="false">
<id name="parentAId" column="PARENT_A_ID" type="int">
<generator class="hilo"/>
</id>
<set name="children" table="CHILD" lazy="false" inverse="false" cascade="all" sort="unsorted">
<key column="CHILD_ID" />
<one-to-many class="test.Child" />
</set>
</class>
</hibernate-mapping>
Quote:
Code:
<hibernate-mapping>
<class name="test.Child" table="CHILD" dynamic-update="false" dynamic-insert="false">
<id name="childId" column="CHILD_ID" type="int">
<generator class="hilo"/>
</id>
<any name="parent" meta-type="class" id-type="int">
<column name="PARENT_ENTITY_NAME"/>
<column name="PARENT_ID"/>
</any>
</class>
</hibernate-mapping>
When I run the following test, I get a strange error indicating its trying to update the child which has not been inserted into its table yet.
Quote:
Code:
Set children = new HashSet();
Child child1 = new Child();
children.add(child1);
ParentA parent = new ParentA();
parent.setChildren(children);
session.save(parent);
The error output is:
Quote:
Code:
[junit] Hibernate: insert into PARENT_A (PARENT_A_ID) values (?)
[junit] Hibernate: update CHILD set PARENT_ENTITY_NAME=?, PARENT_ID=? where CHILD_ID=?
[junit] - Could not synchronize database state with session
[junit] net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
[junit] at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
...
[junit] ------------- ---------------- ---------------
[junit] Testcase: testPersistence(test.MultiParentTest): Caused an ERROR
[junit] SQL insert, update or delete failed (row not found)
[junit] net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
[junit] at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
[junit] at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:687)
...
Any ideas why it is trying to do an update instead of insert? I'm using an Oracle 8i database. Do the mappings seem corerct, or am I completely misunderstanding how to use them?
Thanks,
Jonathan