Guest User

Untitled

a guest
Jul 31st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. JDO / Duplicate entry exception
  2. PersistenceManager pm = pmf.getPersistenceManager();
  3. Transaction tx = pm.currentTransaction();
  4.  
  5. try {
  6. tx.begin();
  7. Query query = pm.newQuery(Studypath.class,"studypathID == paramStudypathID");
  8. query.declareParameters("Integer paramStudypathID");
  9. query.setUnique(true);
  10. Studypath dbStudypath = (Studypath)query.execute(12345);
  11.  
  12. Studypath detachedStudypath = null;
  13. if (dbStudypath != null) {
  14. detachedStudypath = (Studypath)pm.detachCopy(dbStudypath);
  15. } else {
  16. Studypath newStudypath = new Studypath();
  17. // ...
  18. pm.makePersistent(newStudypath);
  19. detachedStudypath = (Studypath)pm.detachCopy(newStudypath);
  20. }
  21.  
  22. tx.commit();
  23.  
  24. // now I want to add this detached studypath to my newly created course
  25. Course c = new Course();
  26. c.addStudypath(detachedStudypath);
  27.  
  28. tx.begin();
  29. pm.makePersistent(c); // <== error
  30. tx.commit();
  31. }
  32. catch (Exception e)
  33. {
  34. //... handle exceptions
  35. }
  36. finally
  37. {
  38. if (tx.isActive())
  39. {
  40. // Error occurred so rollback the transaction
  41. tx.rollback();
  42. }
  43. pm.close();
  44. }
  45.  
  46. @PersistenceCabable
  47. public class Course {
  48. // ...
  49.  
  50. @Persistent
  51. private Set<Studypath> studypaths;
  52. }
  53.  
  54. @PersistenceCabable
  55. public class Studypath {
  56. // ...
  57.  
  58. @Persistent
  59. @PrimaryKey
  60. private Integer studypathID;
  61. }
  62.  
  63. DEBUG [DataNucleus.Datastore.Native] - SELECT 'Courses.Studypath' AS NUCLEUS_TYPE, ... FROM `STUDYPATH` `A0` WHERE `A0`.`STUDYPATHID` = <12345> // this one already exists
  64. DEBUG [DataNucleus.Datastore.Retrieve] - Execution Time = 0 ms
  65. DEBUG [DataNucleus.Datastore.Retrieve] - Retrieving PreparedStatement for connection "jdbc:mysql://127.0.0.1/database, UserName=user, MySQL-AB JDBC Driver"
  66.  
  67. 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
  68. DEBUG [DataNucleus.Datastore.Retrieve] - Execution Time = 1 ms
  69. DEBUG [DataNucleus.Datastore.Retrieve] - Retrieving PreparedStatement for connection "jdbc:mysql://127.0.0.1/database, UserName=user, MySQL-AB JDBC Driver"
  70. DEBUG [DataNucleus.Datastore.Native] - INSERT INTO `COURSE` (...,`COURSEID`) VALUES (...,<1111>)
  71. DEBUG [DataNucleus.Datastore.Persist] - Execution Time = 1 ms (number of rows = 1)
  72. DEBUG [DataNucleus.Datastore.Retrieve] - Closing PreparedStatement org.datanucleus.store.rdbms.ParamLoggingPreparedStatement@3baac1b5
  73.  
  74. DEBUG [DataNucleus.Datastore.Persist] - The requested statement "INSERT INTO `STUDYPATH` (...) VALUES (...)" has been made batchable
  75. DEBUG [DataNucleus.Datastore.Persist] - Batch has been added to statement "INSERT INTO `STUDYPATH` (...) VALUES (...)" for processing (batch size = 1)
  76. DEBUG [DataNucleus.Datastore.Persist] - Adding statement "INSERT INTO `STUDYPATH` (...) VALUES (...)" to the current batch (new batch size = 2)
  77. DEBUG [DataNucleus.Datastore.Persist] - Batch has been added to statement "INSERT INTO `STUDYPATH` (...) VALUES (...)" for processing (batch size = 2)
  78. DEBUG [DataNucleus.Datastore.Native] - BATCH [INSERT INTO `STUDYPATH` (...,`STUDYPATHID`) VALUES (...,<12345>); INSERT INTO `STUDYPATH` (...,`STUDYPATHID`) VALUES (<54321>)]
  79. ERROR [DataNucleus.Datastore] - Exception thrown
  80.  
  81. c.addStudypath(dbStudypath);
Add Comment
Please, Sign In to add comment