Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA ambiguous method call isMember

CriteriaBuilder has overloaded method isMember(...)

Create a predicate that tests whether an element is a member of a collection.

<E,C extends java.util.Collection<E>> 
Predicate isMember(E elem, Expression<C> collection) 

<E,C extends java.util.Collection<E>> 
Predicate isMember(Expression<E> elem, Expression<C> collection) 

I got ambiguous compilation error for the following call:

CriteriaBuilder.isMember((Expression<Object>)a, (Expression<Collection<Object>>)b);

The member type can be any, so it is Object type. How to fix it? Thanks.

like image 285
Dave Avatar asked Feb 19 '26 16:02

Dave


1 Answers

You can declare an (unchecked) generic type to link the casts, perhaps in an internal private method to avoid it being visible to other callers. This even compiles:

CriteriaBuilder cb;
Object a;
Object b;

@SuppressWarnings("unchecked")
private <E> void isMember() {

    cb.isMember((Expression<E>) a, (Expression<? extends Collection<E>>) b);
}

Though a and b would be better as type Expression, and/or as local variables or parameters in the method for more local scoping.

like image 138
df778899 Avatar answered Feb 22 '26 07:02

df778899



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!