gpt4 book ai didi

java - 用于获取未附加到特定变量的数据的查询 - JPA

转载 作者:行者123 更新时间:2023-12-01 11:04:06 25 4
gpt4 key购买 nike

我有一个Employee表和一个Meeting变量(我还有一个 session 表)。 MeetingEmployee 之间的关系是多对多。是否可以编写一个 JPA 查询来从数据库中获取附加到 session 变量的所有员工。如果是的话,查询是什么?另一种选择是获取所有员工并查看哪些不在 session 变量中,但可能会很慢。有什么想法我应该做什么吗?

最佳答案

例如,这是您的 Employee 实体:

@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

关于java - 用于获取未附加到特定变量的数据的查询 - JPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33123378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com