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.
@MappedSuperclass
public class BaseId implements Serializable {
private Long customerId;
private Long facilityId;
/* getter/setters */
}
@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:
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);
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:
|