All,
I am have a simple recursive relationship on a client table. The scenario is this: a single client may be composed of one or more sub clients. Hence ABC Company is comprised of Jane Doe and John Doe. To do this there is a parentclientID on the client table that references the clientid:.
Code:
CREATE TABLE "DB2ADMIN"."FBCLIENT" (
"CLIENTID" INTEGER NOT NULL,
"CLNTTYPE" CHAR(1) ,
"MEMBERNO" VARCHAR(6) ,
"FNAME" VARCHAR(50) ,
"MNAME" VARCHAR(50) ,
"LNAME" VARCHAR(50) ,
"CMPYNAME" VARCHAR(75) ,
"PARENTCLIENTID" INTEGER
)
I have set my class mappings as follows:
Code:
public class ClientVO
{
@Id @NotNull @Column(name="CLIENTID")
public Long getId() {
return id;
}
@OneToMany(mappedBy="parentClient",fetch=FetchType.LAZY)
public Set<ClientVO> getClientMembers() {
return clientMembers;
}
@ManyToOne()
@JoinColumn (name="parentclientid" )
public ClientVO getParentClient() {
return parentClient;
}
}
The problem I am having is that the getClientMembers returns a Set with only one object even if there are multiple sub clients. So, if ABC client had two subclients - John Doe and Jane Doe, only one would be found in the set returned by the getMemberClients() method. The generated SQL looks ok (see below), when I run in natively in the database the correct number of rows are returned.
However, I cannot figure out what I am doing wrong. Is it possible that this type of bidirectional one-to-many does not work in a recursive relationship or is there a better way I could do doing this?
Hibernate version: 3.2
Name and version of the database you are using:DB2
The generated SQL (show_sql=true):Code:
select clientmemb0_.parentclientid as parentc17_2_, clientmemb0_.CLIENTID as CLIENTID2_, clientmemb0_.CLIENTID as CLIENTID2_1_, clientmemb0_.SEARCH as SEARCH2_1_, clientmemb0_.parentclientid as parentc17_2_1_, clientmemb0_.MEMBERNO as MEMBERNO2_1_, clientmemb0_.NOTES as NOTES2_1_, clientmemb0_.CLNTTYPE as CLNTTYPE2_1_, clientmemb0_.FNAME as FNAME2_1_, clientmemb0_.LNAME as LNAME2_1_, clientmemb0_.BIRTHDATE as BIRTHDATE2_1_, clientmemb0_.SEX as SEX2_1_, clientmemb0_.MARITAL as MARITAL2_1_, clientmemb0_.MNAME as MNAME2_1_, clientmemb0_.OCCUPATION as OCCUPATION2_1_, clientmemb0_.SSN as SSN2_1_, clientmemb0_.PREFIX as PREFIX2_1_, clientmemb0_.SUFFIX as SUFFIX2_1_, clientmemb0_.CMPYNAME as CMPYNAME2_1_, drivingdis1_.CLIENTID as CLIENTID18_0_, drivingdis1_.DRIVINGDISTANCE as DRIVINGD2_18_0_, drivingdis1_.LOCATIONID_1 as LOCATIONID3_18_0_, drivingdis1_.LOCATIONID_2 as LOCATIONID4_18_0_, drivingdis1_.MAPURI as MAPURI18_0_, drivingdis1_.STATUS as STATUS18_0_ from DB2ADMIN.FBCLIENT clientmemb0_ left outer join FBDRVDSTNC drivingdis1_ on clientmemb0_.CLIENTID=drivingdis1_.CLIENTID where clientmemb0_.parentclientid=?