Search code examples
javahibernatejpanamed-query

How to create this entity named query


I have a notification service.Notification show to users depends on their permission.Notification is implemeted by the help of three tables they are Notification,Notification type(its Define notification type and its belogs to which permission users is defined), and permission table(id,and permission name) Notification and notification type tables are added below.

notification

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 Entity

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;

}

in above two enties i want find list of notification by specific permissions such that Select nt from notification nt where nt.notificationType.permission in(permissionList) How can i solve this in named queries.


Solution

  • Try to get to the id of Permission using joins and then pass in the list of ids:

    Select nt 
    from notification n 
       inner join n.notificationType nt
       inner join nt.permissions p 
    where p.id in :permissionIds
    

    in the transactional method:

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