简体   繁体   English

HQL:如何使“不存在”查询?

[英]HQL:How to make Not Exists Query?

I have a ManyToMany relationship between "utilisateur" and "projet" and I want to extract all the users not existing in the project entity then this is my query: 我在“ utilisateur”和“ projet”之间有一个ManyToMany关系,我想提取项目实体中不存在的所有用户,这是我的查询:

Query req=utilisateurDAO.createQuery("select u from utilisateur u where not in(select p from projet p where p.utilisateurs.iduser=u.iduser) ");

and this is "Projet" entity: 这是“ Projet”实体:

        @Entity
        public class Projet implements Serializable {
             @Column(name = "idprojet", nullable = false)
             @Id
             @GeneratedValue(strategy=GenerationType.IDENTITY)  
             Integer idprojet;
             @ManyToMany(mappedBy="projets", fetch = FetchType.LAZY)
             java.util.List<com.gestion.projet.domain.Utilisateur> utilisateurs;
    }

and this the "Utilisateur" Entity 这是“ Utilisateur”实体

 @Entity
        public class Utilisateur implements Serializable {
             @Column(name = "iduser", nullable = false)
             @Id
             @GeneratedValue(strategy=GenerationType.IDENTITY)  
             Integer iduser;

@ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(schema = "public", name = "join_membre_projet", joinColumns = { @JoinColumn(name = "iduser", referencedColumnName = "iduser", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "idprojet", referencedColumnName = "idprojet", nullable = false, updatable = false) })
    java.util.List<com.gestion.projet.domain.Projet> projets;
}

and i dont know why does not work? 而且我不知道为什么不起作用?

HQL support both sub selects as well as collection exepressions. HQL支持子选择以及集合表达式。

All the users not existing in the project entity means all users which have no project assigned, or users with an empty project list: 项目实体中不存在的所有用户表示没有分配项目的所有用户,或项目列表为空的用户:

select u from Utilisateur where u.projets is empty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM