我有以下实体:
1.User entity。
@Entity
@Table(name = "USER")
public class User {
@Id
@GeneratedValue
private int userId;
@Column
private String username;
@Column
private String password;
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name = "USER_ROLE", joinColumns={@JoinColumn(name="userId")}, inverseJoinColumns={@JoinColumn(name="roleId")})
private List<Role> roles;
//+setters and getters}
角色实体。
@Entity
@Table(name="ROLE")
public class Role {
@Id
@GeneratedValue
private long roleId;
@Column
private String name;
@ManyToMany(mappedBy="roles", fetch=FetchType.EAGER)
private List<User> users;
//+setters and getters
}
现在,我正在尝试在存储库中提取具有其角色(role_name)的用户(用户名)。我希望为Spring Security Auth做这件事。 这是我的HQL代码:
@Query("select u.username, r.name from User u"+
" join User_Role ur on u.userId = ur.userId"+
" join Role r on ur.roleId = r.roleId"+
" where u.username =:username")
......我收到了这个错误:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [select u.username, r.name from ro.app.mmapp.persistence.entity.User u join User_Role ur on u.userId = ur.userId join Role r on ur.roleId = r.roleId where u.username =:username]
我尝试了多种类型的连接,但得到了相同的结果。 谁可以帮我提一些建议?
谢谢!
答案 0 :(得分:1)
任何基本的JPQL
教程都会告诉您加入一个关系。
select u.username, r.name from User u JOIN u.roles r WHERE u.username =:username