Hello there,
I have this problem when mapping a collection, whose elements are of different classes in the inheritence structure (I know, the inheritence structure or at least its usage is not very well thought through, but I'm trying to avoid changing it).
I have a list of objects, which I mapped using components (As the component class does not have an ID and does not reflect the exact structure of the respective table). The component class itself holds a reference of the type of the superclass ("relatedA" in code snippet; instances might be superclass instances or subclass instances (A or D)).
Anyway, when loading from the database, the items referenced by the component objects really have the type of the superclass, also in case they should be instances of the subclass. Cascade is set to "all", which I hoped would solve the problem.
Setting polymorphism="implicit" did not work either.
I also read about discriminators, but I don't think they will work for me, as I don't have these kind of type columns in all the respective tables.
I hope, the code below helps getting the problem across.
Any comment will be greatly appreciated.
Well, this is an extract from the class structure:
Code:
public class A{
//Elements are of type Component
private List components;
...
}
public abstract class B extends A{...}
public abstract class C extends B{...}
public class D extends C{...}
public class Component{
private A relatedA;
private Type type;
//getters and setters
...
}
public class Type{...}
Then here's the mapping file for class A:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class
name="A"
table="table_a">
...
<joined-subclass
name="B"
table="table_b">
<key column="ID" />
...
<bag name="collection" table="table_collection"
lazy="false" cascade="all">
<key column="ID"></key>
<composite-element
class="Component">
<many-to-one name="type"
class="Type"
cascade="all" outer-join="auto" update="true" insert="true"
column="TypeID" />
<many-to-one name="relatedA"
class="A"
cascade="all" outer-join="auto" update="true" insert="true"
column="ID2" />
</composite-element>
</bag>
...
<joined-subclass
name="C"
table="table_c">
<key column="ID" />
<joined-subclass
name="D"
table="table_d">
<key column="ID" />
...
</joined-subclass>
</joined-subclass>
</joined-subclass>
</class>
</hibernate-mapping>