Ich habe eine Tabelle mit dem Namen group_table
in MySQL mit nur zwei Spalten user_group_id
und group_id
(beide vom Typ VARCHAR
). Diese beiden Spalten bilden zusammen einen zusammengesetzten Primärschlüssel.IN-Klausel mit einem zusammengesetzten Primärschlüssel in JPA-Kriterien
Ich muss eine Anweisung ausführen, die eine Unterauswahl IN()
verwendet, um Zeilen basierend auf einer Liste der an die Abfrage übergebenen Werte auszuwählen.
@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();
}
Die Implementierung erzeugt die folgende Abfrage.
SELECT group_id,
user_group_id
FROM projectdb.group_table
WHERE ((?, ?) IN ((?, ?), (?, ?)))
/*Binding parameters.*/
bind => [null, null, ROLE_AAA, aaa, ROLE_BBB, aaa]
Bitte beachten Sie, dass die ersten beiden Parameter, die über den zusammengesetzten Schlüssel selbst sind null
sind. Sie sollten user_group_id
bzw. group_id
sein.
Warum werden sie nicht in der Parameterliste ersetzt?
Während ich einen zusammengesetzten Primärschlüssel in einer Tabelle in Bildung interessiert mich nicht, dies ist (wahrscheinlich) obligatorisch für JAAS Ich verwende für die Authentifizierung.
In diesem Szenario gibt die Abfrage dieselbe Liste zurück, wie sie aus der Datenbank geliefert wird, was in der Realität unnötig ist. Ich brauche diese Abfrage tatsächlich zum Löschen mehrerer Zeilen.
Ist es nicht möglich, eine Abfrage wie 'WHERE ((user_group_id, group_id) IN (?,?), (?,?)))? MySQL 5.x unterstützt dies. Es wäre viel besser, wenn möglich :) Vielen Dank trotzdem. – Tiny
Sie haben die Abfrage in Hibernate getestet, der Fehler liegt jedoch in Eclipselink. – zbig