Did you ever find an answer? I came to this forum to ask a very similar, perhaps identical question. Maybe somebody can help us both.
I think I have an error somewhere in either my code or my annotations. I am trying to persist a complex, compound object into a database. It is more complicated than this, but the essential part of my question is about an object with at least two levels of OneToMany relationships:
* an Advisory has zero or more Positions
* a Position has zero or more SeaContours
So basically
* object A has OneToMany with object B
* object B has OneToMany with object C
I have all this set up with bi-directional JPA annotations:
Code:
public class Advisory { ...
@OneToMany(cascade = CascadeType.ALL, mappedBy = "advisory")
public Collection<Position> getPositions() {
return positions;
}
}
public class Position { ...
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "advisory_id")
public Advisory getAdvisory() {
return advisory;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "position")
public Collection<SeaContour> getSeaContours() {
return seaContours;
}
}
public class SeaContour { ...
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="position_id")
public Position getPosition() {
return position;
}
}
I make sure to set up the bi-directional relationship correctly. Each parent/child object relationship is set on both ends.
Finally, the MySQL database has three tables (advisory, position, sea_contour) and both 'position' and 'sea_contour' have foreign keys back to their parent tables, with not-null on that foreign key column, and a foreign key constraint.
When I try to call Hibernate to persist the object I get an error:
Code:
EntityManager entityManager = getEntityManagerSingleton();
EntityTransaction entityTransaction = entityManager.getTransaction();
try {
entityTransaction.begin();
entityManager.merge(advisory);
entityTransaction.commit();
} catch (HibernateException e) { ...
Quote:
"Cannot add or update a child row: a foreign key constraint fails (`tropicalsystems_dev`.`position`, CONSTRAINT `fk_position_advisory1` FOREIGN KEY (`advisory_id`) REFERENCES `advisory` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)"
Now, finally, if I (first set the foreign key to not null, and then) remove that fk_position_advisory1 constraint, suddenly the entire compound object is saved no problem. This is unexpected, because as far as I can tell, my A <-> B relationship is set up exactly the same as my B <-> C relationship, but I don't get the same error from my B <-> C relationship. The sea_contour table has a similar constraint for its foreign key to the position table. If both child tables have the same constraint to their parent tables, then I would expect to receive the same error for the other fk constraint.
I assume that I have done *something* different in my A-B and B-C relationships, but I can't figure out what it is. I am a bit of a Hibernate n00b. The other possibility is that a bug in Hibernate is causing it to write compound objects in the wrong way.
In my search for an answer I have:
* absolutely verified that my object sets up the bidirectional relationship correctly
* verified that all my object id types are Long, and my database id types are INT
* made sure all my relationships are Cascade=ALL in both directions
Many thanks!