预期加入的休眠路径但已设置路径

hibernate path expected for join but path is set

我有一个 hql 查询:

"from User u inner join UserRole ur on ur.user_name = u.user_name and ur.user_role =ROLE_MANAGER "

虽然设置了路径,但它显示错误。我尝试了不同的 hql 变体,但错误仍然存​​在。我使用这 2 个实体从 db 进行 spring 安全登录,它工作正常。但是当我试图让用户具有指定的角色时,它不起作用。 我的实体:

@Entity
@Table(name = "USERS")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id")
    private int user_id;


    @Column(name = "username", nullable = false, unique = true)
    private String username;

    @Column(name = "passwort", nullable = false)
    private String password;

    @Column(name = "email")
    private String email = "hromnikforever@gmail.com";

    @Column(name = "enabled", nullable = false)
    private int enabled = 1;

    @Autowired
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<UserRole> userRoles = new HashSet<UserRole>(0);

UserRole 实体:

@Entity
@Table(name = "USER_ROLES")
public class UserRole {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "user_id",unique = true, nullable = false)
    private int user_role_id;

    @Column(name = "username")
    private String username;

    @Column(name = "user_role")
    private String user_role;

如果我将 HQL 查询更改为:

from User u inner join u.userRole ur on ur.user_name = u.user_name
and ur.user_role =ROLE_MANAGER "

显示错误

 could not resolve property: userRole of: com.webproject.User [from com.webproject.User u inner join u.userRole ur on ur.user_name = u.user_name and ur.user_role =ROLE_MANAGER ]

而不是显式 JOIN 尝试使用逗号表示法,并将第二个 JOIN table 更改为 UserRole,如下所示:

from User u, UserRole ur
where ur.user_name = u.user_name
and ur.user_role = ROLE_MANAGER

如果您只需要用户元素,请按如下方式完成您的查询:

select u from User u, UserRole ur
where ur.user_name = u.user_name
and ur.user_role = ROLE_MANAGER

您的查询有错字,因为您的 User 实体没有 userRole,但 userRoles

from User u 
inner join u.userRoles ur on ur.user_name = u.user_name
                          and ur.user_role = ROLE_MANAGER