Issue Details (XML | Word | Printable)

Key: HHH-3160
Type: Sub-task Sub-task
Status: Open Open
Priority: Major Major
Assignee: Gail Badner
Reporter: Gail Badner
Votes: 10
Watchers: 6
Operations

If you were logged in you would be able to see more operations.
Hibernate Core
HHH-3126

Support one-to-many list associations with constraints on both (owner_id, position) and (child_id)

Created: 04/Mar/08 06:08 PM   Updated: 01/Feb/09 11:14 PM
Component/s: None
Affects Version/s: None
Fix Version/s: None

Time Tracking:
Not Specified

File Attachments: 1. File workaround.sql (1 kB)
2. File workaround.sql (0.9 kB)

Issue Links:
Fix
 


 Description  « Hide
The logic used for removing entities from one-to-many list associations can cause ConstraintViolationException will be thrown if there are constraints on both (owner_id, position) and (child_id) in the "collection table". If the association is on a join table, the "collection table" is the join table; otherwise, the "collection table" is the child entity table..

Currently, SchemaExport does not put a constraint on (owner_id, position) when exporting one-to-many list associations on a foreign key. SchemaExport should be updated to also export this constraint.

See HHH-1268 for a description of how to reproduce this issue.

For one-to-many list associations on a foreign key, the workaround is to define the unique constraint on (owner_id, position) in the child entity table as deferred. It is assumed that the primary key in the child entity table is the child ID.

For a one-to-many list association on a join table, the workaround is to define the constraint on (child_id) in the join table as deferred. It is assumed that the primary key in the join table is (owner_id, position).

If your Dialect does not support deferred constraints, then the only workaround is to exclude the unique constraint.

 All   Comments   Work Log   Change History   FishEye      Sort Order: Ascending order - Click to sort in descending order
Gail Badner added a comment - 05/Mar/08 01:38 PM
I made corrections to the workaround in the Description section.

Josh Moore added a comment - 19/Aug/08 04:27 AM
We've run into this problem on Postgres <8.4, which does not support deferred unique constraints yet. I'll attach one possible workaround, which for the updates:

update groupexperimentermap set child=7204, child_index=0 where id=7561;
update groupexperimentermap set child=7204, child_index=2 where id=7563;

where groupexperimentermap has a constraint "UNIQUE (child, child_index)" first moves any constraint violating rows to child_index = -1.

Would it be possible for Hibernate to do the same?

Josh Moore added a comment - 19/Aug/08 04:29 AM
Workaround via triggers which checks for any unique constraint violations and performs an extra "UPDATE" statement to change the column used for ordering (here: child_index) to -1.

Josh Moore added a comment - 19/Aug/08 07:49 AM
Attaching an improved version with the table definition.

Daniel Alexiuc added a comment - 01/Feb/09 11:14 PM
Since Hibernate does not support deferred constraints either - another possible (and very ugly) workaround is to use a many-to-many relationship instead of a one-to-many.