I am not sure what I am missing to make a bi-directional onetomany relationship (hibernate engine). Reduced 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 display, a join table is not created.
2. When I delete mappedBy (@OneToMany (cascade = CascadeType.PERSIST)), a connection table is created and I maintain the connection through Person. The "personFrom" field is empty, but I think this is normal, since the relationship is maintained through the connection table.
I also tried to specify the join column in Relationship, it didn't make any difference. Any help greatly appreciated. thank.
Edit: 1
As with Dan's comment, if you need to see the full content of the domain class, I expanded it 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;
}
, , personFrom, personTo .
Role extends Model {
@ManyToOne
public RoleType roleType;
@ManyToOne
public Person createdBy;
}