Hi,
I have the following:
Code:
public class Family extends BaseObject {
private Parent wife = new Parent();
private Parent husband = new Parent();
...
@ManyToOne(optional=true)
@JoinColumn(name="wife_id", nullable=true)
@Cascade(value=org.hibernate.annotations.CascadeType.ALL)
public Parent getWife() {
return wife;
}
public void setWife(Parent wife) {
this.wife = wife;
}
//husband's getter and setter analogically as wife's
}
public class Parent extends BaseObject {
@OneToMany(cascade=CascadeType.ALL, mappedBy="??????????")
public Family getFamily() {
return family;
}
public void setFamily(Family family) {
this.family = family;
}
}
I would like to have an access to a Family object after loading a Parent object. I guess I should use @OneToMany mapping in my Parent object like above. If there was only one Parent in the family (wife for example) - there would be no problem:
Code:
public class Parent extends BaseObject {
@OneToMany(cascade=CascadeType.ALL, mappedBy="wife")
public Family getFamily() {
return family;
}
public void setFamily(Family family) {
this.family = family;
}
}
But when there two Parent objects in Family object:wife and husband - I am not sure how to set mappedBy in Parent object.
The only way of resolve this problem which comes to my mind is to rearrange associations that it would be:
Code:
public class Parent extends BaseObject {
boolean isWife;
@ManyToOne
@JoinColumn(name="family_id")
@Cascade(value=org.hibernate.annotations.CascadeType.ALL)
public Family getFamily() {
return family;
}
public void setFamily(Family family) {
this.family = family;
}
}
[code]public class Family extends BaseObject {
private Parent wife = new Parent();
private Parent husband = new Parent();
...
@OneToMany(cascade=CascadeType.ALL, mappedBy="family")
public Parent getWife() {
return wife;
}
public void setWife(Parent wife) {
this.wife = wife;
}
//husband's getter and setter analogically as wife's
}
Is there another way of doing that (with ManyToOne association in the Family class)?
Regards,
Pawel