【问题标题】:How to create this entity named query如何创建此实体命名查询
【发布时间】:2017-11-14 09:36:25
【问题描述】:

我有一个通知服务。向用户显示的通知取决于他们的权限。通知是通过三个表实现的,它们是通知、通知类型(其定义通知类型及其定义用户权限的日志),以及权限表(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) 如何在命名查询中解决这个问题。

【问题讨论】:

    标签: java hibernate jpa named-query


    【解决方案1】:

    尝试使用joins获取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();
    

    【讨论】:

    • @marciej 没有该查询的 ON 条件?
    猜你喜欢
    • 1970-01-01
    • 2011-05-27
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多