gpt4 book ai didi

java - JDO/重复条目异常

转载 作者:行者123 更新时间:2023-11-29 08:57:20 24 4
gpt4 key购买 nike

将对象保存到数据库时,我收到 MySQLIntegrityConstraintViolationException 。我知道这个错误意味着什么,但我无法解决它。

错误:引起:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:键“PRIMARY”的重复条目“12345”

基本上,我想将类(class)对象保存到数据库中。每个类(class)对象可能有多个学习路径对象,这些对象又可以是多个类(class)对象的一部分。

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();
}

类(class).java

@PersistenceCabable
public class Course {
// ...

@Persistent
private Set<Studypath> studypaths;
}

Studypath.java

@PersistenceCabable
public class Studypath {
// ...

@Persistent
@PrimaryKey
private Integer studypathID;
}

我是否遗漏了任何明显的错误?提前致谢!

更新(日志):

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

最佳答案

我不确定将分离的 JDO 与 transient JDO 关联起来是否正确。 ORM 没有简单的方法可以知道该关系是现有的 JDO。

如果它确实位于同一代码路径中,我会关联持久实例:

c.addStudypath(dbStudypath);

否则我会在关联它之前 makePersistent(detachedStudypath) (假设你的类是 @Detachable)

关于java - JDO/重复条目异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9619955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com