目前我无法通过eclipselink为以下关系架构生成查询。
经理可以拥有零个或多个员工。(经理可以在没有任何员工的情况下存在)
员工将完全属于一个经理。(员工总是映射到经理)
public class Manager {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;
@OneToMany(mappedBy = "manager")
private List<Employee> employee;
private String department;
}
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private String id;
@ManyToOne
@JoinColumn(name="managerId")
private Manager manager;
private String status;
}
如何在eclipselink中形成左外连接查询,包含Manager或Employee或两者的条件?
例如:
SELECT m.id FROM Manager m LEFT OUTER JOIN Employee e ON m.id = e.managerId
WHERE e.id is null or m.department like 'Dev%' AND e.status = 'ACTIVE'
GROUP BY m.id
ORDER BY m.id
LIMIT 10
答案 0 :(得分:0)
JPQL查询几乎相同。您只是不使用表格,而是使用实体:
SELECT m FROM Manager m
LEFT JOIN m.employee e
ON条件已经在映射注释中定义,因此它被跳过。您还可以使用“LEFT JOIN FETCH”通过延迟关系获取对象。 还要检查https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#LEFT_JOIN
您可以在enity中将JPQL查询指定为命名查询:
@Entity
...
@NamedQueries({
@NamedQuery(
name="findAllEmployeesOrderById",
query="SELECT e FROM Employee e order by e.id"
),
@NamedQuery(
name="findAllEmployeesJoinAddressPhones",
query="SELECT e FROM Employee e",
hints={
@QueryHint(name=QueryHints.FETCH, value="e.address"),
@QueryHint(name=QueryHints.FETCH,
value="e.phoneNumbers")
}
)
})
...
public class MyEntity { ...