OpenJPA
  1. OpenJPA
  2. OPENJPA-792

An entity persist may fail when @MappedSupercalss is specified.

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0.0-M2
    • Fix Version/s: 1.3.0, 2.0.0-M2
    • Component/s: jpa
    • Labels:
      None

      Description

      When @MappedSuperClass annotation is specified and if we try to persist the same key of the subclasses which are entities of the Mappedsuperclass , Persist fails which is not the expected behavior.

      import java.util.Date;

      import javax.persistence.Id;
      import javax.persistence.Column;

      @javax.persistence.MappedSuperclass
      public class Party {

      protected Long PartyId;
      protected String Status;
      protected String ArchiveStatus;
      protected Date CreateDate;

      @Id
      public Long getPartyId()

      { return this.PartyId; }

      public void setPartyId(Long id)

      { this.PartyId = id; }

      public void setArchiveStatus(String s)

      { this.ArchiveStatus = s; }

      public void setStatus(String s)

      { this.Status = s; }

      @Column
      public String getStatus()

      { return this.Status; }

      @Column
      public String getArchiveStatus()

      { return this.ArchiveStatus; }

      public void setCreateDate(Date d)

      { this.CreateDate = d; }

      @Column
      public Date getCreateDate()

      { return this.CreateDate; }

      }

      import java.util.List;

      import javax.persistence.DiscriminatorValue;
      import javax.persistence.Entity;
      import javax.persistence.Inheritance;
      import javax.persistence.OneToMany;
      import javax.persistence.Table;
      import javax.persistence.FetchType;
      import javax.persistence.CascadeType;
      import javax.persistence.InheritanceType;

      import org.apache.openjpa.persistence.jdbc.DiscriminatorStrategy;

      @Entity
      @Table(name = "Site")
      public class Site extends Party implements java.io.Serializable {

      private static final long serialVersionUID = 1L;

      private String SiteName;
      private String SiteDescription;
      /* private List<Store> stores;

      @OneToMany(mappedBy="site", cascade=CascadeType.ALL, fetch=FetchType.LAZY,
      targetEntity=Store.class)
      public List<Store> getStores()

      { return stores; }

      public void setStores(List<Store> storeList)

      { this.stores = storeList; }

      */
      public void setSiteName(String s)

      { this.SiteName = s; }

      public String getSiteName()

      { return this.SiteName; }

      public void setSiteDescription(String s)

      { this.SiteDescription = s; }

      public String getSiteDescription()

      { return this.SiteDescription; }

      }

      import javax.persistence.CascadeType;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.Inheritance;
      import javax.persistence.InheritanceType;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.Table;

      @Entity
      @Table(name = "Store")
      public class Store extends Party implements java.io.Serializable {

      private static final long serialVersionUID = 1L;
      private String StoreName;
      private String StoreDescription;

      private Site site;
      private Long SiteId;

      @ManyToOne( fetch = FetchType.LAZY, cascade = CascadeType.ALL,
      targetEntity=Site.class)
      @JoinColumn(name = "Store.SiteId",
      referencedColumnName="site.PartyId", nullable = false, insertable = true,
      updatable = true)

      public Site getSite()

      { return site; }

      public void setSite(Site s)

      { this.site = s; }

      public void setStoreName(String s)

      { this.StoreName = s; }

      public String getStoreName()

      { return this.StoreName; }

      public void setStoreDescription(String s)

      { this.StoreDescription = s; }

      public String getStoreDescription()

      { return this.StoreDescription; }

      public void setSiteId(Long id)

      { this.SiteId = id; }

      public Long getSiteId()

      { return this.SiteId; }

      }

      TestCase follows:

      import java.util.ArrayList;
      import java.util.Date;
      import java.util.List;

      import javax.persistence.EntityManager;
      import javax.persistence.EntityManagerFactory;
      import javax.persistence.Persistence;

      import lazy_fetch.bean.Site;
      import lazy_fetch.bean.Store;

      public class TestLazyFetch {

      public static EntityManagerFactory emf = null;
      public static Long pkey = new Long (1502);

      public static void main(String[] args)

      { emf = Persistence.createEntityManagerFactory("LazyFetch"); createSite(); System.out.println("Done creating Site"); createStore(); System.out.println("Done creating Store"); // getStoreWithSite(); }

      public static void getStoreWithSite()

      { EntityManager em = emf.createEntityManager(); Store store = em.find(Store.class, pkey); System.out.println("store =" + store); // Site site = store.getSite(); // System.out.println("site =" + site); }

      public static void createSite()

      { EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); Site s = new Site(); s.setPartyId(pkey); s.setSiteName("San Jose"); s.setSiteDescription("San Jose site"); s.setStatus("2"); s.setArchiveStatus("2"); s.setCreateDate(new Date()); em.persist(s); em.getTransaction().commit(); em.close(); }

      public static void createStore()

      { EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); Site site = em.find(Site.class, pkey); Store store = new Store(); store.setPartyId(pkey); store.setStoreDescription("storeDescription"); store.setStoreName("storeName"); store.setStatus("1"); store.setArchiveStatus("1"); store.setCreateDate(new Date()); store.setSiteId(site.getPartyId()); store.setSite(site); // List<Store> stores = new ArrayList<Store>(); // stores.add(store); // site.setStores(stores); em.persist(store); em.getTransaction().commit(); }

      }

      47 LazyFetch INFO [main] openjpa.Runtime - Starting OpenJPA 2.0.0-SNAPSHOT
      125 LazyFetch INFO [main] openjpa.jdbc.JDBC - Using dictionary class "org.apache.openjpa.jdbc.sql.DB2Dictionary".
      Exception in thread "main" <openjpa-2.0.0-SNAPSHOT-runknown nonfatal store error> org.apache.openjpa.persistence.EntityExistsException: An object of type "lazy_fetch.bean.Store" with oid "lazy_fetch.bean.Party-1502" already exists in this context; another cannot be persisted.
      FailedObject: lazy_fetch.bean.Store@9c609c6
      at org.apache.openjpa.kernel.BrokerImpl.checkForDuplicateId(BrokerImpl.java:4756)
      at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2445)
      at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2281)
      at org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java:1021)
      at org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:645)
      at lazy_fetch.tests.TestLazyFetch.createStore(TestLazyFetch.java:77)
      at lazy_fetch.tests.TestLazyFetch.main(TestLazyFetch.java:25)

        Activity

        Hide
        Sandhya added a comment -

        Hi all,

        I am working on this issue and I will update patch soon. (I will try to upload this today if not I will upload the patch the after the thanksgiving)

        Thanks
        Sandhya

        Show
        Sandhya added a comment - Hi all, I am working on this issue and I will update patch soon. (I will try to upload this today if not I will upload the patch the after the thanksgiving) Thanks Sandhya
        Hide
        Sandhya added a comment -

        Hi All,

        Please find the attached patch that has a small modification in PCEnhancer.java and test cases. When @MappedSuperClass annotation is specified, the bytecode for the AddNewobjectIdInstanceMethod() in the enhancer should be looking for This.getClass() instead of the classname.
        I changed that part. This change resolves the issue of not able to persist same keys of the subclasses when MappedSuperClass annotation is specified.

        Thanks
        Sandhya Turaga

        Show
        Sandhya added a comment - Hi All, Please find the attached patch that has a small modification in PCEnhancer.java and test cases. When @MappedSuperClass annotation is specified, the bytecode for the AddNewobjectIdInstanceMethod() in the enhancer should be looking for This.getClass() instead of the classname. I changed that part. This change resolves the issue of not able to persist same keys of the subclasses when MappedSuperClass annotation is specified. Thanks Sandhya Turaga
        Hide
        Fay Wang added a comment -

        patch is committed to trunk r724490, and openjpa 1.3.x r724491.

        Show
        Fay Wang added a comment - patch is committed to trunk r724490, and openjpa 1.3.x r724491.
        Hide
        Ben added a comment -

        I need the fix as I am hitting it in my project.

        Is there a version 1.3.0 build I can download ? Could not find it in the download page.

        Thanks

        Show
        Ben added a comment - I need the fix as I am hitting it in my project. Is there a version 1.3.0 build I can download ? Could not find it in the download page. Thanks
        Hide
        Donald Woods added a comment -

        Neither 1.3.0 nor 2.0.0 have been released yet.
        You can download the assembly from the latest nightly build of 1.3.0-SNAPSHOT from -
        http://people.apache.org/repo/m2-snapshot-repository/org/apache/openjpa/apache-openjpa/1.3.0-SNAPSHOT/
        Or the latest 2.0.0-SNAPSHOT build from -
        http://people.apache.org/repo/m2-snapshot-repository/org/apache/openjpa/apache-openjpa/2.0.0-SNAPSHOT/

        Show
        Donald Woods added a comment - Neither 1.3.0 nor 2.0.0 have been released yet. You can download the assembly from the latest nightly build of 1.3.0-SNAPSHOT from - http://people.apache.org/repo/m2-snapshot-repository/org/apache/openjpa/apache-openjpa/1.3.0-SNAPSHOT/ Or the latest 2.0.0-SNAPSHOT build from - http://people.apache.org/repo/m2-snapshot-repository/org/apache/openjpa/apache-openjpa/2.0.0-SNAPSHOT/
        Hide
        Donald Woods added a comment -

        Give it about an hour and the published build content will be updated under -
        http://openjpa.apache.org/builds/1.3.0-SNAPSHOT/

        Show
        Donald Woods added a comment - Give it about an hour and the published build content will be updated under - http://openjpa.apache.org/builds/1.3.0-SNAPSHOT/
        Hide
        Michael Dick added a comment -

        Hi Ben,

        The information was there (on the downloads page) but it wasn't terribly intuitive. I've added direct links to the snapshot repository on people.apache.org for all the versions that I publish (same links that Donald posted).

        Thanks for bringing it up, and thanks Donald for posting it in a more intuitive place.

        Show
        Michael Dick added a comment - Hi Ben, The information was there (on the downloads page) but it wasn't terribly intuitive. I've added direct links to the snapshot repository on people.apache.org for all the versions that I publish (same links that Donald posted). Thanks for bringing it up, and thanks Donald for posting it in a more intuitive place.
        Hide
        Roger Keays added a comment -

        I've hit this bug in OpenJPA 2.0.0 doing a merge operation, similar to what is described in https://issues.apache.org/jira/browse/OPENJPA-1671

        Trying to reproduce it now.

        Show
        Roger Keays added a comment - I've hit this bug in OpenJPA 2.0.0 doing a merge operation, similar to what is described in https://issues.apache.org/jira/browse/OPENJPA-1671 Trying to reproduce it now.
        Hide
        Roger Keays added a comment -

        I can reproduce my problem and opened a separate bug for it since it seems to be related to serialization.

        https://issues.apache.org/jira/browse/OPENJPA-2262

        Show
        Roger Keays added a comment - I can reproduce my problem and opened a separate bug for it since it seems to be related to serialization. https://issues.apache.org/jira/browse/OPENJPA-2262

          People

          • Assignee:
            Unassigned
            Reporter:
            Sandhya
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development