Perhaps this problem is fixed already or my approach is wrong but i couldn't find any related topic in this forum.
I have a two associated class
ClassA and
ClassB.
ClassB is Tree like. ie It has a parent of type ClassB and a Set<ClassB> as children It also has many-to-one association
to ClassA.
ClassA also has association to ClassB
but with condition that it is linked to ClassB whose
parent is null and ClassA.Id is same as the ClassA's;
So my Classes are
Code:
public class ClassA
{
private int id;
private String name;
private ClassB classB;
{getters and setters}
}
public class ClassB
{
private int id;
private String name;
private ClassA classA;
private ClassB parent;
private Set<ClassB> children;
{getters and setters}
}
And the following hbm
<class name="com.ClassA" table="aclass">
<id column="id" name="id">
<generator class="increment"/>
</id>
<property name="name" column="name"/>
<one-to-one name="classB" property-ref="bref" class="com.ClassB">
<formula>id</formula>
<formula>null</formula>
</one-to-one> </class>
<class name="com.ClassB" table="bclass">
<id column="id" name="id">
<generator class="increment"/>
</id>
<property name="name" column="name"/>
<properties name="bref">
<many-to-one name="classA" class="com.ClassA" column="AClassId"/>
<many-to-one name="parent" class="com.ClassB" column="parentId"/>
</properties>
<set name="children">
<key column="parentId"/>
<one-to-many class="com.ClassB"/>
</set>
</class>
And sample table
Code:
Table aclass
ID NAME
1 A1
2 A2
Table bclass
ID NAME PARENTID ACLASSID
1 B1 (null) 1
2 B11 1 1
3 B12 1 1
4 B2 (null) 2
5 B21 4 2
I ran the following code
Code:
ClassA classA = (ClassA)session.load(ClassA.class,1);//1
ClassB classB = classA.getClassB();//2
ClassA dupClassA = classB.getClassA();//3
System.out.println(classA.getName()+ " " +classB.getName()+" "+dupClassA.getName());//4
I saw the following sql generated as i debugged the code on line 2. And a null pointer exception at line 3
select [
deleted for readability] from aclass classa0_ left outer join bclass classb1_ on
classa0_.id=classb1_.AClassId and null=classb1_.parentId where classa0_.id=?
but if i change my hbm with the following
<one-to-one name="classB" property-ref="bref" class="com.ClassB">
<formula>id</formula> <formula>-1</formula></one-to-one>and ran the following script
update classb set parentId = -1 where parentId is nullThe SQL at line 2 becomes
select [
deleted for readability] from aclass classa0_ left outer join bclass classb1_ on
classa0_.id=classb1_.AClassId and -1=classb1_.parentId where classa0_.id=?
While setting to -1 solves this problem the problem is if i use foreign-key in mysql then i have to create a row with id -1 which i definitely don't want.
The offending part is null=(something) it should be (something) is null or is there something i missed for passing null in formula
Another way I tried was move AClassId to aclass table replace by bclassId but all BClass must refer to an AClass object.While i could do for the top BClass but for the child BClasses i couldn't.Programatically I can get but i need at HQL level