0
由於標題指示我試圖通過使用以下查詢來選擇用戶名和角色名稱。選擇用戶名和角色名稱
select u.username, r.name
from users u, role r
inner join users_roles ur
on ur.user_id = u.id
where username = ?;
但是我提示以下錯誤:
[2017-04-05 21:34:49] [42P01] ERROR: invalid reference to FROM-clause entry for table "u"
[2017-04-05 21:34:49] Hint: There is an entry for table "u", but it cannot be referenced from this part of the query.
[2017-04-05 21:34:49] Position: 79
我的用戶實體如下
@Entity(name = "users") // Postgres doesn't like the table name "user"
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String username;
...
@ManyToMany
@JoinTable(
name = "users_roles",
joinColumns = @JoinColumn(
name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(
name = "role_id", referencedColumnName = "id"))
private Collection<Role> roles;
...
而且我的角色實體如下
@Entity
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
@ManyToMany(mappedBy = "roles")
private Collection<User> users;
...
任何關於我在做什麼的線索NG?
請在此答案中添加一些上下文。這個查詢做了什麼?這是如何解決原始問題的? –
查詢將返回第一列是用戶名的元組列表,第二列是角色的名稱。我將這與彈簧安全一起使用。 – user672009