Hi,
i have an issue with understanding EclipseLinks persist technique:
If I have an entity and persisting it twice with JPAs
getEntityManager().persist(entity); method. I get a duplicate key value in a
unique or primary key constraint violation. So, this is fine since the
entity is persisted before.
What about a ManyToOne Relation in this case? Imagine I have two entities
(Person and RemindIssue). I have already a list of RemindIssues persisted in
the DB through JPA. So, I find an object Remindssue and add this to my NEW
Person entity (Person does not exist in db before). Then I persist my Person
Object (I have CASCADE.ALL activated) and I get the same duplicate key
violation since JPA/EclipseLink wants to persist the RemindIssue again. This
should not happen... and I though that the purpose of an ORM is to figure
this out and do an update instead of a perstist of RemindIssue?!?!
So what is wrong in my thoughts? Are ORMs that stupid?
(Using EclipseLink 1.1.2 with JPA and Derby Embedded, Java 6)
Here are some code snippets:
Person:
@Entity
public class Person extends BaseEntity<Long> {
private String salutation;
....
@OneToMany (mappedBy = "person", cascade = {CascadeType.ALL})
private Set<RemindIssue> remindIssueSet;
public void addRemindIssue(RemindIssue remindIssue) {
remindIssue.setPerson(this); // for bidirectional mapping
if (this.remindIssueSet == null) {
this.remindIssueSet = new HashSet<RemindIssue>();
}
this.remindIssueSet.add(remindIssue);
}
RemindIssue:
@Entity
public class RemindIssue extends BaseEntity<Long> {
// Attributes
...
@ManyToOne(cascade = {CascadeType.ALL}) private Person
person;
Test class:
PersonDAO pDAO = new JpaPersonDAO();
RemindIssueDAO rDAO = new JpaRemindIssueDAO();
RemindIssue issue = new RemindIssue();
issue.setComment("AmazingIssue");
rDAO.persist(issue);
Person p = new Person();
p.setSalutation("Mr");
p.addRemindIssue(issue);
// for updating the issue
issue.setComment("StupidIssue");
// persist the issue as well... no should do an update!
pDAO.persist(p);
Exception Stacktrace:
...
EL Fine]: 2009-08-29 12:53:13.703--Connection(14823211)--INSERT INTO
REMINDISSUE (OBJECTID, DATEOFACTION, COMMENT, PERSON_OBJECTID,
KIND_OBJECTID) VALUES (?, ?, ?, ?, ?)
[EL Fine]: 2009-08-29 12:53:13.703--Connection(14823211)-- bind => [1,
2009-08-29 12:53:08.625, AmazingIssue, null, null]
[EL Fine]: 2009-08-29 12:53:13.718--Connection(19488744)--INSERT INTO PERSON
(OBJECTID, TELEPHONNO2, ADR_HOUSENO, ADR_COUNTRY, ADR_STREET, ADR_CITY,
LASTNAME, FIRSTNAME, FAXNO, EMAIL, SALUTATION, TELEPHONNO1, ADR_ZIPCODE)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
[EL Fine]: 2009-08-29 12:53:13.718--Connection(19488744)-- bind => [2,
null, null, null, null, null, Dames, Martin, null, null, Herr, 02208/910561,
null]
[EL Fine]: 2009-08-29 12:53:13.718--Connection(19488744)--INSERT INTO
REMINDISSUE (OBJECTID, DATEOFACTION, COMMENT, PERSON_OBJECTID,
KIND_OBJECTID) VALUES (?, ?, ?, ?, ?)
[EL Fine]: 2009-08-29 12:53:13.718--Connection(19488744)-- bind => [1,
2009-08-29 12:53:08.625, StupidIssue, 2, null]
[EL Fine]: 2009-08-29 12:53:13.718--VALUES(1)
[EL Warning]: 2009-08-29 12:53:13.718--Exception [EclipseLink-4002] (Eclipse
Persistence Services - 1.1.2.v20090612-r4475):
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.BatchUpdateException: The statement was aborted
because it would have caused a duplicate key value in a unique or primary
key constraint or unique index identified by 'SQL090829125312890' defined on
'REMINDISSUE'.
Error Code: 20000
Exception in thread "main" javax.persistence.RollbackException: Exception
[EclipseLink-4002] (Eclipse Persistence Services - 1.1.2.v20090612-r4475):
org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.BatchUpdateException: The statement was aborted
because it would have caused a duplicate key value in a unique or primary
key constraint or unique index identified by 'SQL090829125312890' defined on
'REMINDISSUE'.
Error Code: 20000
at
org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:102)
at
org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
at birthday.dao.jpa.JpaDAO.commit(JpaDAO.java:99)
at birthday.dao.jpa.JpaDAO.persist(JpaDAO.java:41)
at birthday.dao.jpa.JpaDAO.persist(JpaDAO.java:1)
at birthday.test.TestDAO.main(TestDAO.java:44)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services -
1.1.2.v20090612-r4475): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.BatchUpdateException: The statement was aborted
because it would have caused a duplicate key value in a unique or primary
key constraint or unique index identified by 'SQL090829125312890' defined on
'REMINDISSUE'.
Error Code: 20000
_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users