Cast in typed query

#1

I have the following property in my class "OtherType" with "MySuperType" (an interface) as the generic list type:

@ElementCollection(fetch = FetchType.EAGER)
private List<MySuperType> types = null;

Now I can use "instanceof" within a query in order to select only elements of the specific type:

SELECT o FROM OtherType o JOIN o.types t WHERE t instanceof MySpecificType

But how can I cast to the specific type in order to use properties of the specific type ("t.value")?

SELECT o FROM OtherType o JOIN o.types t WHERE t instanceof MySpecificType AND t.value = :value

#2

The solution is a Java-like cast:

SELECT o FROM OtherType o JOIN o.types t WHERE t instanceof MySpecificType AND ((MySpecificType)t).value = :value

Reply