我有兩個實體有選擇和的毗連
@Entity
@Table(name = "users")
public class User extends Model {
public String name;
@ManyToMany
public List<Role> roles;
}
而且
@Entity
@Table(name = "roles")
public class Role extends Model {
public String name;
}
表JPA JPQL請求包含
Table Users | | Table Roles | |Table Users_roles |
id | Name | | id | Name | |users_id | roles_id |
1 | User 1 | | 1 | CEO | | 2 | 1 |
2 | User 2 | | 2 | Manager | | 2 | 2 |
3 | User 3 | | 3 | 1 |
我要讓下面的查詢(OK在PostgreSQL 9.1 .12)
SELECT u.*, array_to_string(array_agg(r.name ORDER BY r.name ASC), '/')
FROM users u
LEFT OUTER JOIN users_roles ur ON u.id=ur.users_id
FULL JOIN roles r ON r.id=ur.roles_id
GROUP BY u.id, ur.users_id
ORDER BY array_to_string ASC
我有結果我想
User name | roles
User 1 |
User 3 | CEO
User 2 | CEO/MANAGER
你能幫助我獲得此請求的JPA語法?