| The problem with this solution is it requires changes the database schema. Currently POST is Parent table and POSTDETAILS is the child table. With this solution I have to make POSTDETAILS as parent and POST and child table. I would say it is a good workaround but not a solution. Regarding the comment "need to persist first a Post in order to obtain its id, but you cannot do this without a PostDetails because of the optional = false option;". IMO, the right solution would be, Hibernate should insert the POST record first and then the PostDetails record, similar to what it does when optional=true. That way we don't get any ConstraintViolationException. Also also aligns with OneToMany mapping where we can have optional=false/true and Hibernate inserts the parent first then the child records. |