Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7615451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:38:51+00:00 2026-05-31T02:38:51+00:00

I get an MySQLIntegrityConstraintViolationException when saving an object to my database. I know what

  • 0

I get an MySQLIntegrityConstraintViolationException when saving an object to my database. I know what this error means, but I cannot work around it.

Error: Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '12345' for key 'PRIMARY'

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.

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); // <== error
    tx.commit();
}
catch (Exception e)
{
    //... handle exceptions
}
finally
{
    if (tx.isActive())
    {
        // Error occurred so rollback the transaction
        tx.rollback();
    }
    pm.close();
}

Course.java

@PersistenceCabable
public class Course {
    // ...

    @Persistent
    private Set<Studypath> studypaths;
}

Studypath.java

@PersistenceCabable
public class Studypath {
    // ...

    @Persistent
    @PrimaryKey
    private Integer studypathID;
}

Is there any obvious mistake I’m missing? Thanks in advance!

Update (log):

DEBUG [DataNucleus.Datastore.Native] - SELECT 'Courses.Studypath' AS NUCLEUS_TYPE, ... FROM `STUDYPATH` `A0` WHERE `A0`.`STUDYPATHID` = <12345> // 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` = <1111> // 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 (...,<1111>)
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 (...,<12345>); INSERT INTO `STUDYPATH` (...,`STUDYPATHID`) VALUES (<54321>)]
ERROR [DataNucleus.Datastore] - Exception thrown
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:38:52+00:00Added an answer on May 31, 2026 at 2:38 am

    I’m not sure it’s kosher to associate a detached JDO to a transient one. There’s no easy way for the ORM to know the relation is an existing JDO.

    If it’s really in the same code path, I’d associate the persistent instance:

    c.addStudypath(dbStudypath);
    

    Otherwise I would makePersistent(detachedStudypath) before associating it (assuming your class is @Detachable)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error when I try to call $func('something') : if(($object instanceof MyObject)
I get this 'error' when running PEVerify on a custom generated assembly. [MD](0x8013124C): Error:
I get this error when trying ruby script/console Rails requires RubyGems >= . Please
get current array position pointer while doing foreach $object->result_array() Hi, consider this, a case.
I get this error while installing cmake on my machine. Error: Target org.macports.configure returned:
I get this error when trying to do spiriting with compass. Template::Error (can't convert
Get error 'ClassLibrary3.Class1.a' is a 'property' but is used like a 'type' when i
get this from my database: 252.587254564 Well i wanna remove the .587254564 and keep
i get a valid fd object from a caller. How can i find out
I get the following error: Client does not support authentication protocol requested by server;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.