Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple representations of the same entity are being merged, Detached

Tags:

jpa

jpa-2.0

I have been getting an error while trying to update a list of entities containing persisted entity and detached entity both (newly created entity) into my db using jpa2.0. My entity contains internal entities which are giving an error (mentioned in the title) when merging the data:

Class superclass{
    private A a;
    private string name;

    //getter setters here...
}

Class A{
    private long id;
    @onetoone(cascade=CascadeType.All, fetch=FetchType.Eager)
     private B b;
    @onetoone(cascade=CascadeType.All, fetch=FetchType.Eager)
    private C c;

    //getter setters here...

}


Class Dao{

    daoInsert(superclass x){

    em.merge(x);

       }
}

I want any entity sent for persisting to be merged into the db.

Hibernate does provide solution for this by adding the following to the persistence.xml

Is there something I can do in jpa same as hibernate.

Please do not suggest to find the entity using em.find() and then update manually because I need both entities the persisted entity and the newly created entity too. Also I'm using spring form to persist the entire patent entity into db.

I am sorry if I'm not clear enough, this is my first question and I'm really a beginner.

Any help will be most appreciated.

like image 477
Akash Avatar asked Dec 09 '15 13:12

Akash


1 Answers

Found an answer to the question myself today.You just need to

remove CascadeType.MERGE from the entity that is not allowing you to persist the detached entity.

if you're using CascadeType.ALL then mention all cascade type other than CascadeType.MERGE.

like image 101
Akash Avatar answered Sep 23 '22 02:09

Akash