Page 1 of 1

About Question enthuware.oce-jpad.v6.2.514 :

Posted: Sat Sep 22, 2018 5:19 pm
by __JJ__
For some unknown reason this (in particular the line at //1) doesn't throw EntityAlreadyExistsException:

Code: Select all

    public static void main(String[] args) {
        setupY1();
        setupY1A();
        closeEM();        
    }
    
    private static void setupY1() {           
        Y y1= new Y("foo");        
        em.getTransaction().begin();
        em.persist(y1);    
        em.getTransaction().commit();      
    }
    
    private static void setupY1A() {                  
        em.getTransaction().begin();
        Y y1 = em.find(Y.class, 1);
        em.persist(y1);					//1
        em.getTransaction().commit();
    }

Re: About Question enthuware.oce-jpad.v6.2.514 :

Posted: Sat Sep 22, 2018 9:56 pm
by admin
As per Section 3.2.2 of JPA 2.0 spec, it should not throw an exception in your case but just ignore the call because y1 points to an entity that is already managed by the entity manager.
Exception is thrown if you try to persist a new entity with the same PK as an entity that already exists in the DB.

Re: About Question enthuware.oce-jpad.v6.2.514 :

Posted: Sun Sep 23, 2018 9:14 pm
by __JJ__
OK I see, thanks very much.