Uploaded image for project: 'Hibernate ORM'
  1. HHH-9259

NullPointerException querying against inherited properties of a MappedSuperclass.

    Details

    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

    • Last commented by a user?:
      true
    • Sprint:

      Description

      I have created an Embeddable class that serves as a table key that extends an already defined MappedSuperclass which results in a composite-key for the table.

      BaseId.java
      @MappedSuperclass
      public class BaseId implements Serializable {
        private Long customerId;
        private Long facilityId;
        /* getter/setters */
      }
      
      MyEntityId.java
      @Embeddable
      public class MyEntityId extends BaseId {
        private Long type;
        /* getter/setters */
      }
      

      Now using the JPA Criteria API shown below, the code throws a NullPointerException when attempting to find the records:

      TestCase.java
      public List<MyEntity> runTestCase(List<Long> customerIds) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<MyEntity> cq = cb.createQuery(MyEntity.class);
        Root<MyEntity> root = cq.from(MyEntity.class);
      
        // this next statement is where the NullPointerException is thrown.
        Path<Long> customerIdPath = root.get(MyEntity_.id).get(MyEntityId_.customerId);
        cq.select(root);
        cq.where(customerIdPath.in(customerIds));
        return em.createQuery(cq).getResultList();
      }
      

      After fiddling with this, I have determined that if the BaseId class is associated to another class designated as an Entity, then the NullPointerException isn't thrown an the above query works. However, if the BaseId isn't associated to any entity and merely serves as a common class across a series of derived classes, Hibernate will throw the following error:

      java.lang.NullPointerException at
      org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:140)
      

        Attachments

          Activity

            People

            • Votes:
              3 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated: