Monday 17 September 2012

A bug in EclipseLink

I recently found a bug in EclipseLink JPA provider.

I posted the question and answer at Stackoverflow.

Let me summarize the problem here.

First, retrieve an entity from the database, call entityManager.merge() on this entity, change the entity's Id, call entityManager.merge() on this entity again. An exception will be thrown.

Note the above operations are not executed in the same transaction context. Rather, each merge() is executed within its own transaction.

Here is the pseudocode to illustrate the idea.


User user = userManager.find(1); 
userManager.merge(user); 
System.out.println("User is managed? "+userManager.contains(user);
user.setId(2);
userManager.merge(user);


The console outputs:

User is managed? false

Exception [EclipseLink-7251] (Eclipse Persistence Services - 2.1.3.v20110304-r9073): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [id] of class [demo.model.User] is mapped to a primary key column in the database. Updates are not allowed.


userManager.contains() methods invokes the entityManager.contains() method, the fact that it returns false indicates the entity is not being managed. When switching the JPA provider from EclipseLink to Hibernate, it works fine. And according to the JPA specification, it shouldn't throw any exception in this scenario. I believe this bug has manifested itself in other forms. I have found several articles discussing this exception. If you want to reproduce this error, this blog presents one of the simplest approaches.

4 comments:

  1. These properties in persistence.xml solve the problem:

    eclipselink.weaving.changetracking true
    eclipselink.weaving.lazy true
    eclipselink.weaving.eager true
    eclipselink.weaving.fetchgroups true
    eclipselink.weaving.internal false

    ReplyDelete
  2. @Pablo Thank you a lot.

    Those properties solve my problem.

    Greetings ..

    Erva.

    ReplyDelete
  3. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
    python training Course in chennai
    python training in Bangalore
    Python training institute in bangalore

    ReplyDelete
  4. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog.
    rpa training in bangalore
    best rpa training in bangalore
    rpa training in pune | rpa course in bangalore
    rpa training in chennai

    ReplyDelete