Uploaded image for project: 'OpenJPA'
  1. OpenJPA
  2. OPENJPA-1671

Merge Detached Entity - Attempted to attach instance * but this instance is already in the datastore

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Cannot Reproduce
    • 1.2.2
    • None
    • None
    • None
    • Windows XP, JDK 1.6.0_16, Tomcat 6, OpenJPA 1.2.2, Oracle 10g

    Description

      @Entity
      @Table(name="USERS")
      @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
      public class User implements Serializable {
      @Id
      @Column(name="user_name")
      private String userName;

      /* getter / setter */
      }

      @Entity
      public class UserExtended extends User {
      @Column (name="phone)
      private String phone;

      /* getter / setter */
      }

      // Save currently existing user (detached entity)
      UserExtended user = new UserExtended();
      user.setUserName("USER");
      user.setPhone("111-222-3333");

      entityManager.merge(user);

      Receive exception:
      Attempted to attach instance "USER" of type "class com.UserExtended", but this instance is already in the datastore as type "class com.User".

      I've narrowed it down to this:

      When the merge() is called on the UserExtended class, OpenJPA retrieves the data from the user table to perform the merge. When it retrieves this data, I believe it's retrieving it in the form of a User entity, not a UserExtended entity, causing the merge to fail.

      If I retrieve the UserExtended entity from the database, change a value, then merge() I receive no errors.

      Attachments

        1. TestDetachedEntities.java
          1 kB
          Jacob Nowosatka

        Activity

          People

            jtnowosatka Jacob Nowosatka
            seth.jackson Seth Jackson
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: