R
Rizwan
I have 2 classes "ClassA" (mapped to TableA) and "ClassB" (mapped to
TableB). There is many-to-many relationship between these classes which is
represented by "ClassAB" (mapped to TableAB). ClassAB is a little bit more
than just relationship between ClassA and ClassB so we go with a seperate
class.
public class ClassA {
private int classAId;
private Set classABs = new HashSet(); // of type ClassAB
...
}
public class ClassB {
private int classBId;
private Set classABs = new HashSet(); // of type ClassAB
...
}
public class ClassAB {
private int classABId;
private ClassA classA;
private ClassB classB;
...
}
I have these DAO methods:
public ClassA getClassAById( int classAId );
public ClassB getClassBById( int classBId );
public ClassAB getClassABById( int classABId );
public Set getClassABsByClassA( ClassA classA );
public Set getClassABsByClassB( ClassB classB );
When I call "ClassA getClassAById( int classAId )" method, first I retrieve
data from TableA, populate it in ClassA object and then I call the "Set
getClassABsByClassA( ClassA classA )" method. This method retreive data from
TableAB, populate it in ClassAB objects and put them in a Set and then
returns the Set. Then the "ClassA getClassAById( int classAId )" set that
Set in the ClassA object and then returns the ClassA object.
Problem is that "Set getClassABsByClassA( ClassA classA )" method also tries
to resolve all the references in ClassAB. Since ClassAB has 2 references
ClassA and ClassB so it retrieves them as well. So for resolving reference
to ClassA it calls "ClassA getClassAById( int classAId )" method. The result
is a loop:
* "ClassA getClassAById( int classAId )" calls "Set getClassABsByClassA(
ClassA classA )"
* "Set getClassABsByClassA( ClassA classA )" calls "ClassA getClassAById(
int classAId )"
Am I doing something wrong? Or is it how it is supposed to work? Please
reply.
Thanks
TableB). There is many-to-many relationship between these classes which is
represented by "ClassAB" (mapped to TableAB). ClassAB is a little bit more
than just relationship between ClassA and ClassB so we go with a seperate
class.
public class ClassA {
private int classAId;
private Set classABs = new HashSet(); // of type ClassAB
...
}
public class ClassB {
private int classBId;
private Set classABs = new HashSet(); // of type ClassAB
...
}
public class ClassAB {
private int classABId;
private ClassA classA;
private ClassB classB;
...
}
I have these DAO methods:
public ClassA getClassAById( int classAId );
public ClassB getClassBById( int classBId );
public ClassAB getClassABById( int classABId );
public Set getClassABsByClassA( ClassA classA );
public Set getClassABsByClassB( ClassB classB );
When I call "ClassA getClassAById( int classAId )" method, first I retrieve
data from TableA, populate it in ClassA object and then I call the "Set
getClassABsByClassA( ClassA classA )" method. This method retreive data from
TableAB, populate it in ClassAB objects and put them in a Set and then
returns the Set. Then the "ClassA getClassAById( int classAId )" set that
Set in the ClassA object and then returns the ClassA object.
Problem is that "Set getClassABsByClassA( ClassA classA )" method also tries
to resolve all the references in ClassAB. Since ClassAB has 2 references
ClassA and ClassB so it retrieves them as well. So for resolving reference
to ClassA it calls "ClassA getClassAById( int classAId )" method. The result
is a loop:
* "ClassA getClassAById( int classAId )" calls "Set getClassABsByClassA(
ClassA classA )"
* "Set getClassABsByClassA( ClassA classA )" calls "ClassA getClassAById(
int classAId )"
Am I doing something wrong? Or is it how it is supposed to work? Please
reply.
Thanks