I have a table named group_table
in MySQL with only two columns user_group_id
and group_id
(both of them are of type VARCHAR
). Both of these columns together form a composite primary key.
I need to execute a statement using a sub-select IN()
to select rows based on a list of values passed to the query.
@Override
@SuppressWarnings("unchecked")
public List<GroupTable> getList(List<GroupTable> list)
{
CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder();
CriteriaQuery<GroupTable> criteriaQuery=criteriaBuilder.createQuery(GroupTable.class);
Root<GroupTable> root = criteriaQuery.from(entityManager.getMetamodel().entity(GroupTable.class));
criteriaQuery.where(root.in(list));
return entityManager.createQuery(criteriaQuery).getResultList();
}
The implementation produces the following query.
SELECT group_id,
user_group_id
FROM projectdb.group_table
WHERE ((?, ?) IN ((?, ?), (?, ?)))
/*Binding parameters.*/
bind => [null, null, ROLE_AAA, aaa, ROLE_BBB, aaa]
Please notice that the first two parameters which are about the composite key itself are null
. They should be user_group_id
and group_id
respectively.
Why are they not substituted in the parameter list?
While I'm not interested in forming a composite primary key in a table, this is (likely) mandatory for JAAS I'm using for authentication.
In this scenario, the query returns the same list as it is supplied from the database which is needless in reality. I actually need this query for deletion of multiple rows.