Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] ParentChild Insert Problem: Duplicate Key

Hi Tim,

ahhh... Im a fool. Yes I used different EntityManagers. Ok, I made it static.

Im using a Generic DAO Base class für all my DAOs and the EntityManager wasn't static, so that each subclass gets its own.

Thank you very much!



Tim Hollosy schrieb:
Ah, I see you are maintaining both sides of the relationship.
Question: does each DAO have it's own EntityManager?

Because you need to either use the same EM for both, or merge the
nonmanaged entity into the EM you want to perform your transaction on.

Typically you wouldn't make DAO's for each entity, but rather sets of
entities that share business logic.

./tch



On Sat, Aug 29, 2009 at 1:20 PM, Martin Dames<m.dames@xxxxxx> wrote:
Hi Tim,

thanks for reply. Yes, something is wrong in my thoughts.

Your first suggestion:
Isn't this what Im doing in PersonEntity? (remindIssue.setPerson(this);)

public void addRemindIssue(RemindIssue remindIssue) {
    remindIssue.setPerson(this); // for bidirectional mapping
    if (this.remindIssueSet == null) {
       ...

If I add an existing Issue to a Person, the issue will know to what Person
it belongs to.


Your second suggestion:
Well, I do not have a Person object persisted at this time. I just have a
bunch of Issues persisted and the new Person object should get some.

You wrote: "

You don't need to break things down so much into every little
granulated persist/merge statement like you ar.e

"

Hmm.. but I need this to understand the backgrounds and why it doesn't work
as it should work.

Thank you for answer.

Martin.

Tim Hollosy schrieb:
You are thinking about this wrong, you need to tell Issue which Person
to have, then add it on to person, a proper update will occur.

OR

Obtain the issue from person, then change that issue.

You don't need to break things down so much into every little
granulated persist/merge statement like you ar.e

./tch



On Sat, Aug 29, 2009 at 6:56 AM, Martin Dames<m.dames@xxxxxx> wrote:

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


_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users



Back to the top