命名查询连接选择

时间:2016-07-07 07:27:12

标签: mysql jpa select

我需要获取在RoleGroupEntity表中分配了特定角色的GroupEntity对象。

我尝试这样做:

@NamedQuery(name = "GroupEntity.getGIDs", query = "SELECT o FROM RoleGroupEntity u JOIN FETCH u.GroupId o WHERE u.role_id LIKE :role_id")

但我得到了:

org.hibernate.QueryException: could not resolve property: role_id of: RoleGroupEntity

参与样本:

@Entity
@Table(name = "group")
public class GroupEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "gid_number")
    private Long gid_number;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "GroupId")
    private List<RoleGroupEntity> GroupId;

RoleGroupEntity具有复合键:

@Entity
@Table(name = "role_group")
public class RoleGroupEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private RoleGroupCompositeKey posRGKey;

    @MapsId("role_id")
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "role_id")
    private RoleEntity roles;

    @MapsId("group_id")
    @ManyToOne
    @JoinColumn(name = "group_id")
    private GroupEntity GroupId;

您知道如何使我的选择查询起作用吗?

1 个答案:

答案 0 :(得分:1)

您无法使用必须使用的连接列:u.roles.id LIKE :role_id")

@NamedQuery(name = "GroupEntity.getGIDs", query = "SELECT o FROM RoleGroupEntity u JOIN FETCH u.GroupId o WHERE u.roles.id LIKE :role_id")