2

How can I get a typecast into "Specification" "toPredicate" method?

Ex:

@Entity
public class Empresa{...}

@Entity
public class Cliente extends Empresa{ 
    private Integer cod;
    ...
}
@Entity
public class Vinculo{
    private Empresa emp1;
    private Empresa emp2;
}


public static Specification<Vinculo> serieEq(final Integer cod) {
    return new Specification<Vinculo>() {
    @Override
        public Predicate toPredicate(Root<Vinculo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            return cb.equal(root.join("emp2").get("cod"), cod);
    };
}

Note that "cod" is only present in Cliente.

Thanks a lot,

Sandro
  • 63
  • 5

1 Answers1

0

You can use the as() API. This will work on some JPA providers.

James
  • 17,965
  • 11
  • 91
  • 146
  • It works with EclipseLink, but not with Hibernate. Here is a more detailled answer: http://stackoverflow.com/questions/6427126/polymorphic-criteriaquery-without-inverse-relationship – Romain F. Jun 10 '15 at 12:49