Hello,
I am not sure what I am missing to make a bidirectional onetomany relationship (hibernate engine). A scaled down version of the domain model:
class Person {
@OneToMany(mappedBy="personFrom", cascade = CascadeType.PERSIST)
public List<Relationship> relationships;
}
class Relationship {
@ManyToOne
public Person personFrom;
@ManyToOne
public Person personTo;
}
Some of the observations:
1. with the above mapping, there is no join table created.
2. When I remove the mappedBy (@OneToMany(cascade = CascadeType.PERSIST) ), the join table is created and i could persist Relationship through Person. "personFrom" field is empty, but I think that is normal as the relation is maintained through the join table.
I also tried by specifying join column at Relationship, didn't make any difference. Any help, highly appreciated. thanks.
Edit:1
As per Dan's comment, if it matters to see the full content of the domain class, I have expanded them below.
class Relationship extends Model{
@ManyToOne
public RelationshipType relationshipType;
@ManyToOne
public Person personFrom;
@ManyToOne
public Person personTo;
@ManyToOne
public Person createdBy;
@ManyToOne
public Role roleFrom;
@ManyToOne
public Role roleTo;
@Override
public String toString() {
return relationshipType.toString();
}
}
class Person extends Model {
public Date dateCreated;
@Lob
public String description;
@OneToMany(cascade = CascadeType.ALL)
public List<Role> roles;
@OneToMany(mappedBy="personFrom", cascade = CascadeType.PERSIST)
public List<Relationship> relationships;
}
Role also related to Person, but I think keeping the personFrom, personTo helps to optimize my queries.
Role extends Model {
@ManyToOne
public RoleType roleType;
@ManyToOne
public Person createdBy;
}