我只是试图获得一个包含所有属性的整个实体,包括空集合,如果它们是空的。
public class Users extends BaseEntity {
...
@Column(name = "Prename")
private String prename;
@Column(name = "session")
private String session;
@JsonView({ View.Users.class })
@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
@JoinTable(
name="USERS_PROJECTS",
joinColumns={@JoinColumn(name="USERS_ID", referencedColumnName="id")},
inverseJoinColumns={@JoinColumn(name="PROJECT_ID", referencedColumnName="id")})
private Set<Project> projects = new HashSet<>();
@JsonView({ View.Users.class })
@ManyToMany(mappedBy = "projectManager",
cascade = { CascadeType.PERSIST, CascadeType.MERGE },
fetch = FetchType.LAZY)
private Set<Project> managingProjects = new HashSet<>();
@JsonView({ View.Users.class })
@ManyToMany(mappedBy = "projectManager",
cascade = { CascadeType.PERSIST, CascadeType.MERGE },
fetch = FetchType.LAZY)
private Set<Project> watchingProjects = new HashSet<>();
...
}
相应的命名查询如下所示:
@NamedQueries({
@NamedQuery(
name = Users.QUERY_GET_ALL_USERS_ASC,
query = "SELECT u " +
"FROM Users u " +
"LEFT JOIN FETCH u.projects proj " +
"LEFT JOIN FETCH u.managingProjects manProj " +
"LEFT JOIN FETCH u.watchingProjects watProj " +
"ORDER BY u.email ASC")
})
此查询返回大约350个对象,但它应该只返回17,可能是因为它不是&#34; GROUPED&#34;。
@NamedQueries({
@NamedQuery(
name = Users.QUERY_GET_ALL_USERS_ASC,
query = "SELECT u " +
"FROM Users u " +
"JOIN FETCH u.projects proj " +
"JOIN FETCH u.managingProjects manProj " +
"JOIN FETCH u.watchingProjects watProj " +
"ORDER BY u.email ASC")
})
此查询返回0个对象,因为并非每个对象都与非空的watchProjects或managingProjects集合相关。
@NamedQuery(
name = Users.QUERY_GET_ALL_USERS_ASC,
query = "SELECT u " +
"FROM Users u " +
"LEFT JOIN FETCH u.projects proj " +
"LEFT JOIN FETCH u.managingProjects manProj " +
"LEFT JOIN FETCH u.watchingProjects watProj " +
"GROUP BY u " // + ", manProj, watProj " + // does not change anything
"ORDER BY u.email ASC")
此查询引发以下异常:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: column "t1.id" must appear in the GROUP BY clause or be used in an aggregate function
Position: 99
Error Code: 0
Call: SELECT t0.ID, t0.CreateDate, t0.EMail, t0.Lastname, t0.Password, t0.Prename, t0.Role, t0.session, t1.ID, t1.Name, t1.Scheme, t2.ID, t2.Name, t2.Scheme, t3.ID, t3.Name, t3.Scheme FROM Users t0 LEFT OUTER JOIN (USERS_PROJECTS t6 JOIN PROJECT t1 ON (t1.ID = t6.PROJECT_ID)) ON (t6.USERS_ID = t0.ID) LEFT OUTER JOIN (PROJECTS_MANAGER t7 JOIN PROJECT t2 ON (t2.ID = t7.PROJECT_ID)) ON (t7.MANAGER_ID = t0.ID) LEFT OUTER JOIN (PROJECTS_MANAGER t8 JOIN PROJECT t3 ON (t3.ID = t8.PROJECT_ID)) ON (t8.MANAGER_ID = t0.ID) LEFT OUTER JOIN (PROJECTS_MANAGER t9 JOIN PROJECT t4 ON (t4.ID = t9.PROJECT_ID)) ON (t9.MANAGER_ID = t0.ID) LEFT OUTER JOIN (PROJECTS_MANAGER t10 JOIN PROJECT t5 ON (t5.ID = t10.PROJECT_ID)) ON (t10.MANAGER_ID = t0.ID) GROUP BY t0.ID, t0.CreateDate, t0.EMail, t0.Lastname, t0.Password, t0.Prename, t0.Role, t0.session, t4.ID, t4.Name, t4.Scheme, t5.ID, t5.Name, t5.Scheme ORDER BY t0.EMail ASC
将FetchType设置为LAZY不是一个选项,因为还有其他查询不应该获取这些属性。所以问题是如何用可能的空集合返回带有JPQL查询的所有实体? 我正在使用EclipseLink 2.6.2和JPA 2.1。