OpenJPA
  1. OpenJPA
  2. OPENJPA-1797

Can not persist subclass entity with its full data, if this subclass entity has ManyToOne to another entity

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.0.1
    • Fix Version/s: None
    • Component/s: jpa
    • Labels:
      None

      Description

      A Person entity class, which has such emails field:

      @OneToMany(targetEntity = EmailDb.class, mappedBy = "person", cascade = CascadeType.ALL)
      protected List<ListField> emails = new ArrayList<ListField>();

      While Email entity is subclass of @MappedSuperclass ListFieldDb.

      Then I create email and add to person:

      EmailDb email = new EmailDb();
      email.setValue(targetAddress);
      email.setPrimary(false);
      email.setType("emailType");

      person.addEmail(emial);
      em.persist(person);

      But what I see from console about email table changes is: INSERT INTO email (oid, person_id) VALUES (?, ?), and other data lost, especially the map key 'person_id' to person table.

      Not sure if I am using something wrong? or this is a known issue?

      I pasted source code files for entity relationship reference. Thanks.

      1. EmailDb.java
        2 kB
        Kai Feng Zhang
      2. ListFieldDb.java
        4 kB
        Kai Feng Zhang
      3. PersonDb.java
        33 kB
        Kai Feng Zhang

        Activity

        Hide
        Kai Feng Zhang added a comment -

        This only happens when I want to persist the subclass entity. If it's a no-superclass entity and has manytoone relation to person, then I can store all its data into table.

        Show
        Kai Feng Zhang added a comment - This only happens when I want to persist the subclass entity. If it's a no-superclass entity and has manytoone relation to person, then I can store all its data into table.
        Hide
        Pinaki Poddar added a comment -

        > protected List<ListField> emails = new ArrayList<ListField>();

        ListField is an interface and ListFieldDB is the implementation. Try

        protected List<ListFieldDB> emails = new ArrayList<ListFieldDB>();

        Show
        Pinaki Poddar added a comment - > protected List<ListField> emails = new ArrayList<ListField>(); ListField is an interface and ListFieldDB is the implementation. Try protected List<ListFieldDB> emails = new ArrayList<ListFieldDB>();
        Hide
        Kai Feng Zhang added a comment -

        Thank you Pinaki.

        I made a workaround to solve this issue, that is to remove @MappedSuperclass from ListFieldDb, and use @Inheritance(strategy=InheritanceType.JOINED) on ListFieldDb. Then I could store data of email into EmailDb and ListFieldDb.

        But anyway this is a problem of @MappedSuperclass, which is subclass entity data can not be stored with integrity.

        Show
        Kai Feng Zhang added a comment - Thank you Pinaki. I made a workaround to solve this issue, that is to remove @MappedSuperclass from ListFieldDb, and use @Inheritance(strategy=InheritanceType.JOINED) on ListFieldDb. Then I could store data of email into EmailDb and ListFieldDb. But anyway this is a problem of @MappedSuperclass, which is subclass entity data can not be stored with integrity.

          People

          • Assignee:
            Unassigned
            Reporter:
            Kai Feng Zhang
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:

              Development