如何创建这个名为query的实体

时间:2017-11-14 09:36:25

标签: java hibernate jpa named-query

我有一个通知服务。用户的通知显示取决于他们的许可。通知是通过三个表的帮助实现的,它们是通知,通知类型(其定义通知类型和定义权限用户的belogs),以及权限表(id和权限名称) 下面添加了通知和通知类型表。

通知

public class Notifications  {

@Id 
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String title;
private String deatils;
private String link;
@OneToOne(fetch=FetchType.EAGER)
@PrimaryKeyJoinColumn(name="NotificationTypeId",referencedColumnName="Id")
private NotificationTypes notificationTypes;
private Boolean tenantSpecific;
private Boolean userSpecific;
private Long recieverUserId;
private Boolean activeFlag;
private Long tenantId;
@Temporal(TemporalType.TIMESTAMP)
private Date insertedDttm;
private Long insertedBy;
@Temporal(TemporalType.TIMESTAMP)
private Date updatedDttm;
private Long updatedBy;
}

NotifiactionType实体

public class NotificationTypes implements Serializable {
private static final long serialVersionUID = 1L;

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

private String typeName;
@Temporal(TemporalType.TIMESTAMP)
private Date insertedDttm;
private Long insertedBy;
@Temporal(TemporalType.TIMESTAMP)
private Date updatedDttm;
private Long updatedBy;

@OneToMany(fetch = FetchType.EAGER)
private List<RLPermissions> permissions;

}

在以上两个内容中,我希望按特定权限查找通知列表 这样的 Select nt from notification nt where nt.notificationType.permission in(permissionList) 我如何在命名查询中解决这个问题。

1 个答案:

答案 0 :(得分:1)

尝试使用连接获取Permission的id,然后传入id列表:

Select nt 
from notification n 
   inner join n.notificationType nt
   inner join nt.permissions p 
where p.id in :permissionIds
交易方法中的

session.createNamedQuery("query", Notifications.class)
            .setParameterList("permissionIds", permissionIds)
            .list();