Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » [SOLVED] integrity constraint violation after mergin list of unmanaged entities
[SOLVED] integrity constraint violation after mergin list of unmanaged entities [message #957722] Thu, 25 October 2012 08:47 Go to next message
Andre Albert is currently offline Andre Albert
Messages: 25
Registered: July 2009
Junior Member
Hello,

I have this weird situation that after merging a list of unmanaged and managed entities, I get PK constraint errors while committing changes to DB.
The entities are transmitted between Client/Server and are unmanaged before each step in the procedure.
It happens when first persisting an entity containing a list of entities and afterwards removing one entity from this list and finally adding a new Entity to this list.

I am using eclipselink 4.2.0

I have a stripped down example:

@Entity
public class Parent implements Serializable{
    @Id @GeneratedValue
    public Long id;
    @OneToMany(cascade=CascadeType.ALL, orphanRemoval=true)
    public List<Children> childs = new ArrayList<Children>();
}

and
@Entity
public class Children implements Serializable {
    @Id @GeneratedValue
    public Long id;
}


The following Main Method for example crashes all the time (stacktrace.log attached).

public static void main(final String[] args) {
        EntityManager em = createEntityManager();

        Parent p = new Parent();
        Children c1 = new Children();
        Children c2 = new Children();
        p.childs.add(c1);
        p.childs.add(c2);
        
        EntityTransaction transaction = em.getTransaction();
        transaction.begin();
        em.persist(p);
        transaction.commit();
        em.close();
        
        // simulate a new Client Request which always get a new EntityManager
        
        em = createEntityManager();
        Parent pNeu = new Parent();
        Children c1Neu = new Children();
        pNeu.id = p.id;
        c1Neu.id= c1.id;
        pNeu.childs.add(c1Neu);
        transaction = em.getTransaction();
        transaction.begin();
        em.merge(p);
        transaction.commit();
        em.close();
        
        // simulate a new Client Request which always get a new EntityManager
        
        em = createEntityManager();
        pNeu = new Parent();
        pNeu.id = p.id;
        c1Neu = new Children();
        c1Neu.id= c1.id;
        Children c2Neu = new Children();
        p.childs.add(c1Neu);
        p.childs.add(c2Neu);
        transaction = em.getTransaction();
        transaction.begin();
        em.merge(p);
        transaction.commit();
        em.close();
    }


Can someone please tell me what I am doing wrong?

Thanks in advance

Regards

---
Solution:
It turns out that the (real) entities I am using failing the hash/equals contract

[Updated on: Thu, 25 October 2012 10:24]

Report message to a moderator

Re: integrity constraint violation after mergin list of unmanaged entities [message #957749 is a reply to message #957722] Thu, 25 October 2012 09:12 Go to previous messageGo to next message
Andre Albert is currently offline Andre Albert
Messages: 25
Registered: July 2009
Junior Member
sorry,
I made an mistake while creating this stripped-down example. If I am using pNeu instead of p in the last step, it works. However, in my environment I still have this issue but maybe the cause is similar to my typing error here. I am investigating it.
Re: integrity constraint violation after mergin list of unmanaged entities [message #957838 is a reply to message #957749] Thu, 25 October 2012 10:39 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 995
Registered: July 2009
Senior Member
Turning the EclipseLink logging level up might help track inserts and statements while debugging as described here:
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging

Having the paramaters in the statement might help track down which object is involved in the problem.

Best Regards,
Chris
Re: integrity constraint violation after mergin list of unmanaged entities [message #957857 is a reply to message #957838] Thu, 25 October 2012 10:53 Go to previous message
Andre Albert is currently offline Andre Albert
Messages: 25
Registered: July 2009
Junior Member
Solution:
It turns out that the (real) entities I am using failing the hash/equals contract.
It was realy hard to debug, but in the end the failure was part of the entity implementation.
Maybe this helps when someone experiences similar issue where 'merge' triggers an INSERT INTO but the object is already managed with an @Id value set.
Previous Topic:Exception when using transformer on a accessor
Next Topic:Config Migratin Using Moxy
Goto Forum:
  


Current Time: Mon Apr 21 03:16:58 EDT 2014

Powered by FUDForum. Page generated in 0.02971 seconds