Hibernate version: Latest version of Hibernate.
I have the following 'uml' scheme:
----
//Root class.
abstract class SyntaxComponent
private String name;
----
//Subclass of SyntaxComponent
class Rule extends SyntaxComponent
private
Operation op;
----
//Abstract Subclass of SyntaxComponent
abstract class Term extends SyntaxComponent
----
//Abstract Subclass of SyntaxComponent
abstract class Operation extends SyntaxComponent
----
//Subclass of Operation
class ComparisonOp extends Operation
private
Term leftTerm;
private
Term rightTerm;
----
//Subclass of Term
class Var extends Term
----
The problem is that I want to have one-to-one associations with subclasses from the same root. I have the following mapping: (wich doesn't give any problems):
I use the "Table per subclass" strategy...
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<!-- @author Stijn Deroo-Van Maele -->
<!-- SyntaxComponent.hbm.xml -->
<!-- ORM mapping for 'SyntaxComponent' class -->
<hibernate-mapping>
<class
name="syntaxtree.SyntaxComponent"
table="SYNTAX_COMPONENT">
<id
name="id"
column="SYNTAX_COMPONENT_ID"
type="long">
<generator class="native"/>
</id>
<property
name="name"
column="NAME"
not-null="false"
unique="false"
type="string"/>
<joined-subclass
name="syntaxtree.Rule"
table="RULE">
<key column="RULE_ID"/>
<many-to-one
name="[color=red]operation[/color]"
class="syntaxtree.Operation"
column="OPERATION_ID"
cascade="all"
unique="true"/>
</joined-subclass>
<joined-subclass
name="syntaxtree.Operation"
table="OPERATION">
<key column="OPERATION_ID"/>
<property
name="operatorType"
column="OPERATOR_TYPE"
type="string"
/>
<joined-subclass
name="syntaxtree.ComparisonOp"
table="COMPARISON_OP">
<key column="COMPARISON_OP_ID"/>
<one-to-one name="leftTerm" class="syntaxtree.Term"/>
<one-to-one name="rightTerm" class="syntaxtree.Term"/>
</joined-subclass>
</joined-subclass>
<joined-subclass
name="syntaxtree.Term"
table="TERM">
<key column="TERM_ID"/>
<joined-subclass
name="syntaxtree.Var"
table="VAR">
<key column="VAR_ID"/>
</joined-subclass>
</joined-subclass>
</class>
</hibernate-mapping>
What's wrong? Hibernate won't store a rule, with an Operation member value. There are no faults, but in the database, nothing is filled in, only in the Rule table. (store(new Rule(new Operation))).
Is it possible to map everyting in one file like this above, or do I have to make different mapping files also for class Operation. And how do I declare it to be a subtype of SyntaxComponent?