Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA: Merge @OneToMany Duplicate entry error
    primarykey
    data
    text
    <p>I'm alwways getting the following error:</p> <pre><code>Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '2-1' for key 'PRIMARY' Error Code: 1062 Call: INSERT INTO ENTITY_ENTITY (relationships_ID, Entity_ID) VALUES (?, ?) bind =&gt; [1, 2] Query: DataModifyQuery(sql="INSERT INTO ENTITY_ENTITY (relationships_ID, Entity_ID) VALUES (?, ?)") </code></pre> <p>My classes look like:</p> <pre><code>@javax.persistence.Entity public class Entity { @Id @GeneratedValue(strategy = GenerationType.TABLE) protected Long id; @Column(nullable=false, updatable=false) protected String name; @OneToMany(cascade=CascadeType.MERGE) protected Collection&lt;Entity&gt; relationships = new ArrayList&lt;Entity&gt;(); } </code></pre> <p>One instance of this class can reference other instances of the same type (self-referencing). Now, while creating one instance which references another, everything works fine. But the problem is, when such an (detached - I'm not quite sure, because the instance is edited via a Facelets form) instance is updated, i.e. the name, then by executing the EntityManager merge operation the exception - as mentioned above - is thrown.</p> <p><strong>EDIT:</strong><br> 1. Create one instance of Entity (i.e. E1).<br> 2. Persist E1 via em.persist(E1).<br> 3. Later, create another instance of Entity (E2) which refreneces E1 via the property <em>relationships</em>.<br> 4. And persist E2: em.persist(E2).</p> <p>--- Until now everything works fine. </p> <ol start="5"> <li>Load to browse all persisted Entity instances and choose E2 for editing (via a Web interface Facelet).</li> <li>Change i.e. the name of E2. </li> <li>To save the changes: Call em.merge(E2). </li> </ol> <p>---Now the Exception is thrown!</p> <p><strong>END EDIT</strong></p> <p>What's wrong with my code? Please, help me!!!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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