List<Users> temp2;
temp2 = em.createQuery("SELECT u FROM Users u "
        + "NATURAL LEFT JOIN CourseMembers b "
        + "WHERE b.courseMembersPK.userID IS NULL").getResultList();


嗨!我试图获取所有用户对象,如果在另一个表(CourseMembers)中没有它们的记录。

但我从我的EJB收到此错误:

Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.6.2.qualifier): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Syntax error parsing [SELECT u FROM Users u NATURAL LEFT JOIN CourseMembers b WHERE b.courseMembersPK.userID IS NULL].
[21, 22] The FROM clause has 'Users u' and 'NATURAL LEFT JOIN CourseMembers b' that are not separated by a comma.
[30, 30] An identification variable must be provided for a range variable declaration.


我用逗号尝试过,但这给了我另一个错误。我该如何解决?

最佳答案

我猜您有一种情况想让所有不是成员/未订阅任何课程的用户获得。

首先,您应该在OneToManyUser实体之间创建一个CourseMembers关系:

User实体中,您应该具有以下内容:

...
private Set<CourseMembers> courseMembers;
...
@OneToMany(fetch = FetchType.EAGER, mappedBy="user")
public Set<CourseMembers> getCourseMembers() {
    return this.courseMembers;
}
...


然后,您需要ManyToOneCourseMembers之间的User关系:

CourseMembers实体中,您应该具有以下内容:

...
private User user;
...
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="USER_ID", nullable=false)
public User getUser() {
    return this.user;
}
...


然后,在查询中可以使用:

FROM User u WHERE u.courseMembers IS EMPTY


希望这可以帮助。

10-08 19:23