Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your mapping is totally valid and works with EclipseLink as JPA 2.0 implementation (without the <code>Fetch</code> annotation of course), but indeed fails with Hibernate. </p> <p>Here is the DDL with Hibernate:</p> <pre><code>create table foo_bar_map (foo_id bigint not null, bar_id bigint not null, order_index integer not null, primary key (foo_id, order_index), unique (bar_id)) alter table foo_bar_map add constraint FK14F1CB7FA042E82 foreign key (bar_id) references Bar4022509 alter table foo_bar_map add constraint FK14F1CB7B6DBCCDC foreign key (foo_id) references Foo4022509 </code></pre> <p>So let's say <code>Foo#1</code> holds a list with <code>Bar#1</code>, <code>Bar#2</code>, <code>Bar#3</code>, the join table contains:</p> <pre><code>foo_id | bar_id | order_index 1 | 1 | 1 1 | 2 | 2 1 | 3 | 3 </code></pre> <p>When removing, say the first item from the list, Hibernate first <code>delete</code> the last row (WTF?) from the join table:</p> <pre><code>foo_id | bar_id | order_index 1 | 1 | 1 1 | 2 | 2 </code></pre> <p>And then tries to <code>update</code> the <code>bar_id</code> column in the join table instead of the <code>order_index</code> (WTF!?) to reflect the "new" ordering of the items in the list. First (schematically):</p> <pre><code>foo_id | bar_id | order_index 1 | 2 | 1 1 | 2 | 2 </code></pre> <p>where the next step would result in: </p> <pre><code>foo_id | bar_id | order_index 1 | 2 | 1 1 | 3 | 2 </code></pre> <p>Obviously, this approach doesn't sound right and <strong>doesn't work</strong> because of the <code>unique</code> constraint on <code>bar_id</code>. More generally, why the hell does Hibernate mess with the <code>bar_id</code> instead of updating the <code>order_index</code> column? </p> <p>I consider this to be an Hibernate bug (reported as <s><a href="https://hibernate.atlassian.net/browse/HHH-5694" rel="nofollow noreferrer">HHH-5694</a></s>, see <a href="https://hibernate.atlassian.net/browse/HHH-1268" rel="nofollow noreferrer">HHH-1268</a> now).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload