Note that there are some explanatory texts on larger screens.

plurals
  1. POJDO / Duplicate entry exception
    primarykey
    data
    text
    <p>I get an <code>MySQLIntegrityConstraintViolationException</code> when saving an object to my database. I know what this error means, but I cannot work around it.</p> <p>Error: <code>Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '12345' for key 'PRIMARY'</code></p> <p>Basically, I want to save course objects to a database. Each course object may have several studypath objects, which can in turn be part of several course objects.</p> <pre><code>PersistenceManager pm = pmf.getPersistenceManager(); Transaction tx = pm.currentTransaction(); try { tx.begin(); Query query = pm.newQuery(Studypath.class,"studypathID == paramStudypathID"); query.declareParameters("Integer paramStudypathID"); query.setUnique(true); Studypath dbStudypath = (Studypath)query.execute(12345); Studypath detachedStudypath = null; if (dbStudypath != null) { detachedStudypath = (Studypath)pm.detachCopy(dbStudypath); } else { Studypath newStudypath = new Studypath(); // ... pm.makePersistent(newStudypath); detachedStudypath = (Studypath)pm.detachCopy(newStudypath); } tx.commit(); // now I want to add this detached studypath to my newly created course Course c = new Course(); c.addStudypath(detachedStudypath); tx.begin(); pm.makePersistent(c); // &lt;== error tx.commit(); } catch (Exception e) { //... handle exceptions } finally { if (tx.isActive()) { // Error occurred so rollback the transaction tx.rollback(); } pm.close(); } </code></pre> <p>Course.java</p> <pre><code>@PersistenceCabable public class Course { // ... @Persistent private Set&lt;Studypath&gt; studypaths; } </code></pre> <p>Studypath.java</p> <pre><code>@PersistenceCabable public class Studypath { // ... @Persistent @PrimaryKey private Integer studypathID; } </code></pre> <p>Is there any obvious mistake I'm missing? Thanks in advance!</p> <p>Update (log):</p> <pre><code>DEBUG [DataNucleus.Datastore.Native] - SELECT 'Courses.Studypath' AS NUCLEUS_TYPE, ... FROM `STUDYPATH` `A0` WHERE `A0`.`STUDYPATHID` = &lt;12345&gt; // this one already exists DEBUG [DataNucleus.Datastore.Retrieve] - Execution Time = 0 ms DEBUG [DataNucleus.Datastore.Retrieve] - Retrieving PreparedStatement for connection "jdbc:mysql://127.0.0.1/database, UserName=user, MySQL-AB JDBC Driver" DEBUG [DataNucleus.Datastore.Native] - SELECT 'Courses.Course' AS NUCLEUS_TYPE, ... FROM `COURSE` `A0` WHERE `A0`.`COURSEID` = &lt;1111&gt; // there is no such course, thus it gets created DEBUG [DataNucleus.Datastore.Retrieve] - Execution Time = 1 ms DEBUG [DataNucleus.Datastore.Retrieve] - Retrieving PreparedStatement for connection "jdbc:mysql://127.0.0.1/database, UserName=user, MySQL-AB JDBC Driver" DEBUG [DataNucleus.Datastore.Native] - INSERT INTO `COURSE` (...,`COURSEID`) VALUES (...,&lt;1111&gt;) DEBUG [DataNucleus.Datastore.Persist] - Execution Time = 1 ms (number of rows = 1) DEBUG [DataNucleus.Datastore.Retrieve] - Closing PreparedStatement org.datanucleus.store.rdbms.ParamLoggingPreparedStatement@3baac1b5 DEBUG [DataNucleus.Datastore.Persist] - The requested statement "INSERT INTO `STUDYPATH` (...) VALUES (...)" has been made batchable DEBUG [DataNucleus.Datastore.Persist] - Batch has been added to statement "INSERT INTO `STUDYPATH` (...) VALUES (...)" for processing (batch size = 1) DEBUG [DataNucleus.Datastore.Persist] - Adding statement "INSERT INTO `STUDYPATH` (...) VALUES (...)" to the current batch (new batch size = 2) DEBUG [DataNucleus.Datastore.Persist] - Batch has been added to statement "INSERT INTO `STUDYPATH` (...) VALUES (...)" for processing (batch size = 2) DEBUG [DataNucleus.Datastore.Native] - BATCH [INSERT INTO `STUDYPATH` (...,`STUDYPATHID`) VALUES (...,&lt;12345&gt;); INSERT INTO `STUDYPATH` (...,`STUDYPATHID`) VALUES (&lt;54321&gt;)] ERROR [DataNucleus.Datastore] - Exception thrown </code></pre>
    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