Search code examples
jpaeclipselinkcriteriacriteria-api

How specify a JPA criteria API with "MEMBER IN" and a many-to-many relationship in a mapped superclass?


I would like to query ConcreteEntity where AUser user IS MEMBER of ConcreteEntity.createdBy with

@Entity
public class AUser implements Serializable {
    @Id
    @GeneratedValue
    private Long id;
    private String property;

and

@MappedSuperclass
public class AbstractEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    private Long id;
    @ManyToMany
    private Set<AUser> createdBy = new HashSet<>();

and

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
ppublc class ConcreteEntity extends AbstractEntity {
    private String property1;

i.e.

EntityManager em = ...; //set to null or else
AUser user = ...; //set to null or else
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<ConcreteEntity> criteria = cb.createQuery(ConcreteEntity.class);
Root<ConcreteEntity> concreteRoot = criteria.from(ConcreteEntity.class);
criteria.select(concreteRoot);
SetAttribute<AbstractEntity, AUser> setAttribute = ConcreteEntity_.createdBy;
PluralAttribute<ConcreteEntity, Set<AUser>, AUser> pluralAttribute = setAttribute;
    //richtercloud/jpa/criteria/api/metamodel/member/of/NewMain.java:[40,77] error: incompatible types: SetAttribute<AbstractEntity,AUser> cannot be converted to PluralAttribute<ConcreteEntity,Set<AUser>,AUser>
Expression<Set<AUser>> createdByExpression = concreteRoot.get(pluralAttribute);
criteria.where(cb.isNotMember(user, createdByExpression));
TypedQuery<ConcreteEntity> query = em.createQuery(criteria);
List<ConcreteEntity> result = query.getResultList();

I don't get the transition from SetAttribute to PluralAttribute. I know that I'm not supposed to post question with code which doesn't compile, but although I'd say I have a fair understanding of generics, I don't get it here.

I'm using Eclipselink 2.5.2 and JPA 2.1.


Solution

  • look at the Path.get methods:

    <Y> Path<Y> get(SingularAttribute<? super X, Y> attribute);
    
    <E, C extends Collection<E>> Expression<C> get(PluralAttribute<X, C, E> collection);
    

    the second should be instead:

    <E, C extends Collection<E>> Expression<C> get(PluralAttribute<? super X, C, E> collection);
    

    So, IMHO, it's not your fault, the API is bugged.

    A workaround is needed. Just erase the compile-time type (it's erased on run-time, anyway).

    You have to do:

    Expression<Set<AUser>> createdByExpression = ((Root<AbstractEntity>) (Object) root).get(AbstractEntity_.createdBy);
    

    or

    Expression<Set<AUser>> createdByExpression = root.get((SetAttribute<ConcreteEntity, AUser>) (Object) AbstractEntity_.createdBy);
    

    or else

    Expression<Set<AUser>> createdByExpression = root.get("createdBy");