查询获取未附加到特定变量的数据 - JPA
Query for getting data not attached to a particular variable - JPA
我有一个 table 的 Employee
和一个 Meeting
变量(我还有一个 table 用于会议)。 Meeting
和 Employee
之间的关系是 many-to-many
。是否可以编写一个 JPA 查询,从数据库中获取所有附加到 Meeting 变量的员工。如果是这样,查询是什么?另一种选择是获取所有 Employees 并查看哪些不在 Meeting 变量中,但可能很慢。有什么想法我该怎么做?
例如,这是您的员工实体:
@Entity
public class Employee {
@ManyToMany(mappedBy="employee")
private List<Meeting> meetings;
...
}
Query: Get all the Employees from the database which are attached to
the Meeting variable
SELECT e FROM Employee e WHERE :meeting MEMBER OF e.meetings
Query: Get all Employees and see which ones are not in the Meeting
variable
SELECT e FROM Employee e WHERE :meeting NOT MEMBER OF e.meetings
示例代码:
String query = "SELECT e FROM Employee e WHERE :meeting MEMBER OF e.meetings";
Meeting meeting = new Meeting();
meeting.setId(1);
TypedQuery<Employee> query = em.createQuery(query, Employee.class);
List<Employee> employeesInMeeting = query.setParameter("meeting", meeting).getResultList();
// this will select all employees that has a meeting with id=1 in their list
我有一个 table 的 Employee
和一个 Meeting
变量(我还有一个 table 用于会议)。 Meeting
和 Employee
之间的关系是 many-to-many
。是否可以编写一个 JPA 查询,从数据库中获取所有附加到 Meeting 变量的员工。如果是这样,查询是什么?另一种选择是获取所有 Employees 并查看哪些不在 Meeting 变量中,但可能很慢。有什么想法我该怎么做?
例如,这是您的员工实体:
@Entity
public class Employee {
@ManyToMany(mappedBy="employee")
private List<Meeting> meetings;
...
}
Query: Get all the Employees from the database which are attached to the Meeting variable
SELECT e FROM Employee e WHERE :meeting MEMBER OF e.meetings
Query: Get all Employees and see which ones are not in the Meeting variable
SELECT e FROM Employee e WHERE :meeting NOT MEMBER OF e.meetings
示例代码:
String query = "SELECT e FROM Employee e WHERE :meeting MEMBER OF e.meetings";
Meeting meeting = new Meeting();
meeting.setId(1);
TypedQuery<Employee> query = em.createQuery(query, Employee.class);
List<Employee> employeesInMeeting = query.setParameter("meeting", meeting).getResultList();
// this will select all employees that has a meeting with id=1 in their list