To a lot of problems with @MappedSuperclass with generics.
We have: {code:java} @MappedSuperclass public class AbstractEntity<ID extends Serializable> { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) protected ID id; ... } {code}
And we have:
{code:java} @Entity @Table(name = "SomeEntity") public class SomeEntity extends AbstractEntity<Integer> { } {code}
Next, jpa criteria: {code:java} public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) { //Here we have - Serializable, but expected Integer. root.get("id").getJavaType(); return ...; } {code}
If the generic parameter is just type, we have Object at getJavaType().
TEST CASE: I'm try to execute criteria query and have this test case:
{code:java} CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<SomeEntity> criteria = builder.createQuery(SomeEntity.class);
Root<SomeEntity> root = criteria.from(SomeEntity.class);
Specification<SomeEntity> specification = (roott, criteriaQuery, criteriaBuilder) -> { Path<Integer> idField = root.get("id"); assertEquals(idField.getJavaType(), Integer.class); return criteriaBuilder.equal(idField, 1); };
criteria.where(specification.toPredicate(root, criteria, builder)); criteria.select(root);
assertEquals(entityManager.createQuery(criteria).getResultList().size(), 1); {code}
And i have assertion exception - getJavaType() returns Serializable. In my project requirements i need to know type of fields in MappedSuperclass. |
|