6

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.

Akash
  • 387
  • 1
  • 5
  • 19
  • We need more information, as you haven't described what exactly is causing the issue. It shouldn't be left to us to guess if and what you are calling daoInsert on, and if the entity causing the issue is an A, B or C instance. What exactly is the situation you get the exception under, and why do you have multiple detached instances of the same entity in your model or list? – Chris Dec 09 '15 at 17:15
  • @Chris the entity causing the issue is b and c, I get the error only when value of an attribute inside C is repeated in the next entry for A... This link I've mentioned for hibernate is exactly the problem I have in jpa... HTTPS://hibernate.atlassian.net/browse/HHH-9106 – Akash Dec 09 '15 at 17:27
  • Also I am calling DaoInsert to merge my entities list, ie if the entity with matching id exists then it should be updated and if no matching id is found then a new entry has to be created in the db. Would this much information do? – Akash Dec 09 '15 at 17:30
  • Is there anything I'm missing... Can someone help me here??? – Akash Dec 10 '15 at 05:06
  • This still did not describe how you are hitting the issue in HHH-9106. Your model shows an A->B and A->C. HHH-9106 would require something like A1->B1->A1'. While B and C are likely involved in your issue, you did not show enough of your model or the situation to infer you are hitting HHH-9106, and you haven't described why you are getting multiple different instance of the same entity. You might be better looking at why you have multiple detached instances of one of your entities around, as this seems inefficient, and it is unclear which might contain changes that should be captured. – Chris Dec 10 '15 at 18:46
  • @Chris... I'm not really sure if i had any A1->B1->A1' relationship because all I had was A1->B1->C1&D1 there's only a three level hierarchy which is not actually cyclic. It's linear in nature. – Akash Dec 10 '15 at 18:51
  • Possible duplicate of [java.lang.IllegalStateException: Multiple representations of the same entity with @ManyToMany 3 entities](https://stackoverflow.com/questions/26591521/java-lang-illegalstateexception-multiple-representations-of-the-same-entity-wit) – Jens Schauder Aug 27 '18 at 05:03

3 Answers3

8

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.

Community
  • 1
  • 1
Akash
  • 387
  • 1
  • 5
  • 19
  • 1
    I love such esoterics. Thank for answering your question. (yeah, i was sarcastic about the esoterics part). – hrs Oct 02 '18 at 17:28
  • Can you please give an example of what you mean? Let's say that you are using CascadeType.ALL. How woud you "mention all cascade type other than CascadeType.Merge" Do you mean specifically list out every other one besides merge? – Jolley71717 Jan 22 '19 at 20:24
  • @Jolley71717 Yes that's exactly what I meant. List it all the other cascade type in an array. – Akash Jan 30 '19 at 09:01
2

Now removing CascadeType.MERGE from cascade is one solution but not a best solution because after removing MERGE from Cascade you won't be able to update the mapped object ever.

If you want to merge the Detached entity with Hibernate then clear the entity manager before you merge the entity

entityManager.clear();
//perform modification on object
entityManager.merge(object);
abhinav kumar
  • 1,487
  • 1
  • 12
  • 20
  • 1
    +1, we had mappings set up in a way that we did not need updates to the objects then. But for anyone coming across this, this is indeed one problem with removing merge, the entity will not be updated as mentioned. – Akash Oct 28 '21 at 12:53
0

To solve this problem make sure to specify that the identifiers of your objects are automatically generated by adding @GeneratedValue(strategy = GenerationType.IDENTITY) on the identifaint such as id.

In this way when the merge will be carried out, the identifier of the elements to merge will be automatically incremented, compared to the other object already recorded in the database to avoid primary key conflicts