I'm having troubles with a table having a table having a many to many relationship to itself since a requirement can have multiple parents and multiple children. I basically created entities from database using NetBeans wizards and everything else seems to work fine. But doing unit tests for this parent/child relationship I started having failures. Basically, when I add a child to a requirement somehow the child ends up having the parent as a child as well.
I believe is somehow related to the JoinTable having 2 columns pointing to the same key.
Here's the relevant entity code mapping this:
@JoinTable(name = "requirement_has_requirement", joinColumns = {
@JoinColumn(name = "requirement_id", referencedColumnName = "id"),
@JoinColumn(name = "requirement_version", referencedColumnName = "version")}, inverseJoinColumns = {
@JoinColumn(name = "parent_requirement_id", referencedColumnName = "id"),
@JoinColumn(name = "parent_requirement_version", referencedColumnName = "version")})
@ManyToMany
private List<Requirement> requirementList;
@ManyToMany(mappedBy = "requirementList")
private List<Requirement> requirementList1;
If needed, the code can be obtained here: https://javydreamercsw@bitbucket.org/javydreamercsw/validation-manager
Edit The questions are hard to answer, since in reality it doesn't work. In theory requirementList should have child and requirementList1 the parent of the relationship.
When I try to add a child I do something like:
requirement.getRequirementList().add(requirement2);
<Persist requirement here>
Assuming the two requirements are proper valid entities.