OpenJPA
  1. OpenJPA
  2. OPENJPA-602

NullPointerException at org.apache.openjpa.jdbc.meta.strats.RelationToManyInverseKeyFieldStrategy.map

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.1.0
    • Fix Version/s: 1.2.0
    • Component/s: jdbc
    • Labels:
      None

      Description

      The example I'll attach crashes with an NPE when running the mappingtool.

      Caused by: java.lang.NullPointerException
      at org.apache.openjpa.jdbc.meta.strats.RelationToManyInverseKeyFieldStrategy.map(RelationToManyInverseKeyFieldStrategy.java:136)
      at org.apache.openjpa.jdbc.meta.strats.RelationCollectionInverseKeyFieldStrategy.map(RelationCollectionInverseKeyFieldStrategy.java:95)
      at org.apache.openjpa.jdbc.meta.FieldMapping.setStrategy(FieldMapping.java:120)
      at org.apache.openjpa.jdbc.meta.RuntimeStrategyInstaller.installStrategy(RuntimeStrategyInstaller.java:80)
      at org.apache.openjpa.jdbc.meta.FieldMapping.resolveMapping(FieldMapping.java:438)
      at org.apache.openjpa.jdbc.meta.FieldMapping.resolve(FieldMapping.java:403)
      at org.apache.openjpa.jdbc.meta.ClassMapping.resolveMapping(ClassMapping.java:812)

      The goal of this JIRA is not (neccessarily) to support the kind of mapping that would be required for this (which may be hard), but to at least print a clear error and not an NPE...

      1. OneToMany-test.zip
        109 kB
        Michael Vorburger
      2. FullStackTrace-OPENJPA-602.txt
        3 kB
        Michael Vorburger

        Activity

        Hide
        Michael Vorburger added a comment -

        ----Original Message----
        From: Michael Vorburger mvorburger@odyssey-group.com
        Sent: mercredi, 30. avril 2008 17:07
        To: users@openjpa.apache.org; dev@openjpa.apache.org
        Subject: NPE at RelationToManyInverseKeyFieldStrategy when using mappedBy inverse

        Hello,

        I'm running into an NPE in RelationToManyInverseKeyFieldStrategy
        (copy/pasted below) when using an OneToMany inverse relation with mappedBy and InheritanceType.TABLE_PER_CLASS in 1.1.0-SNAPSHOT...

        Test summary: A common abstract superclass Translatable which has a @OneToManySet<Translation>, and many subclasses of that like e.g. a SomethingTranslatable, and many more. The idea is that mapping should lead to a Translation table holding the items for SomethingTranslatable and all other superclasses. Then SomethingTranslatable and many other tables (no physical Translatable table). There shouldn't be a join table for each SomethingTranslatable, as it's a OneToManySet, it doesn't make sense, so the owning side is a @ManyToOne in Translation back to Translatable. Granted a physical FK will not be possible in such a schema - accepted, but it should still be possible to map this? It works until I use @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) on Translatable, with it - boum, NPE.

        Show
        Michael Vorburger added a comment - ---- Original Message ---- From: Michael Vorburger mvorburger@odyssey-group.com Sent: mercredi, 30. avril 2008 17:07 To: users@openjpa.apache.org; dev@openjpa.apache.org Subject: NPE at RelationToManyInverseKeyFieldStrategy when using mappedBy inverse Hello, I'm running into an NPE in RelationToManyInverseKeyFieldStrategy (copy/pasted below) when using an OneToMany inverse relation with mappedBy and InheritanceType.TABLE_PER_CLASS in 1.1.0-SNAPSHOT... Test summary: A common abstract superclass Translatable which has a @OneToManySet<Translation>, and many subclasses of that like e.g. a SomethingTranslatable, and many more. The idea is that mapping should lead to a Translation table holding the items for SomethingTranslatable and all other superclasses. Then SomethingTranslatable and many other tables (no physical Translatable table). There shouldn't be a join table for each SomethingTranslatable, as it's a OneToManySet, it doesn't make sense, so the owning side is a @ManyToOne in Translation back to Translatable. Granted a physical FK will not be possible in such a schema - accepted, but it should still be possible to map this? It works until I use @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) on Translatable, with it - boum, NPE.
        Hide
        Michael Vorburger added a comment -

        ----Original Message----
        From: Fay Wang fyw300@yahoo.com
        Sent: jeudi, 15. mai 2008 00:52
        To: users@openjpa.apache.org; dev@openjpa.apache.org
        Subject: RE: NPE at RelationToManyInverseKeyFieldStrategy when using mappedBy inverse

        Here is my finding: The NPE problem is the combination of (1) abstract class, (2) inheritance strategy of TABLE_PER_CLASS, and (3) toMany relationship. When you have (1) and (2), or (1) and (3), or (2) and (3), you will be fine. However, when the three things are put together, the class strategy of this entity class becomes NoneClassStrategy (since abstract class with inheritance strategy of TABLE_PER_CLASS will not have a corresponding database table). With NoneClassStrategy, the foreign key required for the toMany relationship will not be constructed, resulting in NPE. I am not clear whether JPA spec ever mentioned that no relation fields in the abstract class when inheritance strategy is table per concrete class. If not, this seems to be a bug in OpenJPA.

        @Entity
        @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class EntityA {

        @Id
        @GeneratedValue
        private Long id;

        @Version
        private Long version;

        @OneToMany(cascade=CascadeType.PERSIST,
        mappedBy="entityA")
        private java.util.Set<EntityB> entityBs;

        public java.util.Set<EntiytB> getEntityBs()

        { return entityBs; }

        public void setEntityBs(java.util.Set<EntityB> entityBs)

        { this.entityBs = entityBs; }


        }
        }

        If you only have (1) and (2), you will be fine because

        ----Original Message----
        From: Fay Wang fyw300@yahoo.com
        Sent: mercredi, 14. mai 2008 18:45
        To: users@openjpa.apache.org; dev@openjpa.apache.org
        Subject: RE: NPE at RelationToManyInverseKeyFieldStrategy when using mappedBy inverse

        I take it back. I found that when I put the fields and methods in EntityBase class directly in the Translatable class instead of having Translatable inherit from EntityBase class, I still get NPE. This problem seems having nothing to do with MappedSuperclass annotation.

        -Fay

        ----Original Message----
        From: Fay Wang fyw300@yahoo.com
        Sent: mercredi, 14. mai 2008 18:23
        To: users@openjpa.apache.org; dev@openjpa.apache.org
        Subject: RE: NPE at RelationToManyInverseKeyFieldStrategy when using mappedBy inverse

        Hi Mike,
        Your super class EntityBase is annotated as @MappedSuperclass. Your Translatable class which inherits from EntityBase has inheritance strategy of TABLE_PER_CLASS. Since MappedSuperclass itself is not a persistence class and can not act in the capacity of an entity, it does not have a corresponding table in the database. I think this is why openjpa got confused and could not find foreign key between the parent class and the child class. When I removed Inheritance strategy annotation from Translatable class, your test case went through. Having said that, I think the NPE should be fixed and proper error message should be thrown.

        -Fay

        Show
        Michael Vorburger added a comment - ---- Original Message ---- From: Fay Wang fyw300@yahoo.com Sent: jeudi, 15. mai 2008 00:52 To: users@openjpa.apache.org; dev@openjpa.apache.org Subject: RE: NPE at RelationToManyInverseKeyFieldStrategy when using mappedBy inverse Here is my finding: The NPE problem is the combination of (1) abstract class, (2) inheritance strategy of TABLE_PER_CLASS, and (3) toMany relationship. When you have (1) and (2), or (1) and (3), or (2) and (3), you will be fine. However, when the three things are put together, the class strategy of this entity class becomes NoneClassStrategy (since abstract class with inheritance strategy of TABLE_PER_CLASS will not have a corresponding database table). With NoneClassStrategy, the foreign key required for the toMany relationship will not be constructed, resulting in NPE. I am not clear whether JPA spec ever mentioned that no relation fields in the abstract class when inheritance strategy is table per concrete class. If not, this seems to be a bug in OpenJPA. @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class EntityA { @Id @GeneratedValue private Long id; @Version private Long version; @OneToMany(cascade=CascadeType.PERSIST, mappedBy="entityA") private java.util.Set<EntityB> entityBs; public java.util.Set<EntiytB> getEntityBs() { return entityBs; } public void setEntityBs(java.util.Set<EntityB> entityBs) { this.entityBs = entityBs; } } } If you only have (1) and (2), you will be fine because ---- Original Message ---- From: Fay Wang fyw300@yahoo.com Sent: mercredi, 14. mai 2008 18:45 To: users@openjpa.apache.org; dev@openjpa.apache.org Subject: RE: NPE at RelationToManyInverseKeyFieldStrategy when using mappedBy inverse I take it back. I found that when I put the fields and methods in EntityBase class directly in the Translatable class instead of having Translatable inherit from EntityBase class, I still get NPE. This problem seems having nothing to do with MappedSuperclass annotation. -Fay ---- Original Message ---- From: Fay Wang fyw300@yahoo.com Sent: mercredi, 14. mai 2008 18:23 To: users@openjpa.apache.org; dev@openjpa.apache.org Subject: RE: NPE at RelationToManyInverseKeyFieldStrategy when using mappedBy inverse Hi Mike, Your super class EntityBase is annotated as @MappedSuperclass. Your Translatable class which inherits from EntityBase has inheritance strategy of TABLE_PER_CLASS. Since MappedSuperclass itself is not a persistence class and can not act in the capacity of an entity, it does not have a corresponding table in the database. I think this is why openjpa got confused and could not find foreign key between the parent class and the child class. When I removed Inheritance strategy annotation from Translatable class, your test case went through. Having said that, I think the NPE should be fixed and proper error message should be thrown. -Fay
        Hide
        Tim Kaltenbrunner added a comment -

        Hi,

        I have the same problem at my current project. I have an existing database structure created with Kodo JDO. Now I want to use OpenJPA but keep the old database structure. Will someone fix this problem or will you only throw an exception. I am also not sure if the specification specifies that this case should be possible but JDO implementation do support it and therefore it would be nice to have it in OpenJPA .

        Cheers Tim

        Show
        Tim Kaltenbrunner added a comment - Hi, I have the same problem at my current project. I have an existing database structure created with Kodo JDO. Now I want to use OpenJPA but keep the old database structure. Will someone fix this problem or will you only throw an exception. I am also not sure if the specification specifies that this case should be possible but JDO implementation do support it and therefore it would be nice to have it in OpenJPA . Cheers Tim
        Hide
        Pinaki Poddar added a comment -

        SVN Revision 666314 [1] addresses this issue. Used a modified version the attached patch as the domain model in a test case.
        It will be helpful if the reporter can verify
        a) whether the newly added test case reflects the intended domain model
        and b) whether the change resolves the original issue.

        Known limitation:
        As the root of persistent hierarchy is abstract and table-per-class strategy is being used,
        aggregate JPQL query e.g. "SELECT COUNT(p) FROM Translatable p" will not be possible
        because the concrete subclasses such as SomethingTranslatable or SomeOtherTranslatable are unjoined.

        [1] http://svn.apache.org/viewvc?view=rev&revision=666314

        Show
        Pinaki Poddar added a comment - SVN Revision 666314 [1] addresses this issue. Used a modified version the attached patch as the domain model in a test case. It will be helpful if the reporter can verify a) whether the newly added test case reflects the intended domain model and b) whether the change resolves the original issue. Known limitation: As the root of persistent hierarchy is abstract and table-per-class strategy is being used, aggregate JPQL query e.g. "SELECT COUNT(p) FROM Translatable p" will not be possible because the concrete subclasses such as SomethingTranslatable or SomeOtherTranslatable are unjoined. [1] http://svn.apache.org/viewvc?view=rev&revision=666314
        Hide
        Pinaki Poddar added a comment -

        SVN revision 666314.

        Show
        Pinaki Poddar added a comment - SVN revision 666314.

          People

          • Assignee:
            Pinaki Poddar
            Reporter:
            Michael Vorburger
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development