My parametrised JPA or Hibernate query:
SELECT entity FROM Entity entity WHERE name IN (?)
How can I pass the parameter as an ArrayList<String>? Hibernate currently returns:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String
Using pure JPA with Hibernate 5.0.2.Final as the actual provider the following seems to work with positional parameters as well:
Entity.java:
@Entity
@NamedQueries({
@NamedQuery(name = "byAttributes", query = "select e from Entity e where e.attribute in (?1)") })
public class Entity {
@Column(name = "attribute")
private String attribute;
}
Dao.java:
public class Dao {
public List<Entity> findByAttributes(Set<String> attributes) {
Query query = em.createNamedQuery("byAttributes");
query.setParameter(1, attributes);
List<Entity> entities = query.getResultList();
return entities;
}
}