简体   繁体   English

如何使用或运算符进行HQL查询

[英]how to make a HQL Query with or operators

I have a ManyToMany relationship between Utilisateur and Projet and I want to extract all the users not existing in the project entity or the users assigned to a completed project then this is my query: 我在UtilisateurProjet之间有一个ManyToMany关系,我想提取项目实体中不存在的所有用户或分配给已完成项目的用户然后这是我的查询:

select u 
    from Utilisateur u 
    where 
        u.projets is empty or 
        u.projets.status like 'Completed'"

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) })
    List<Projet> projets;

}

and this is the Projet Entity: 这是Projet实体:

@Entity
public class Projet implements Serializable {

    @Column(name = "idprojet", nullable = false)
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)  
    Integer idprojet;

    String statut;

    @ManyToMany(
        mappedBy="projets", 
        fetch = FetchType.LAZY)
    List<Utilisateur> utilisateurs;
}

this my xhtml file that contains the method that call the hql query: 这个我的xhtml文件包含调用hql查询的方法:

<p:dataTable id="checkboxDT" var="user" value="#{UtilisateurComponent.dispo()}" selection="#{ProjetComponent.projet.utilisateurs}" rowKey="#{user.iduser}" style="margin-bottom:0">

        <f:facet name="header">
            Affectation des ressources
        </f:facet>
        <p:column selectionMode="multiple" style="width:2%;text-align:center" />

        <p:column headerText="Id">
            <h:outputText value="#{user.iduser}" />
        </p:column>
         <p:column headerText="Nom">
            <h:outputText value="#{user.nomuser}" />
        </p:column>

            </p:dataTable>
            <h:panelGroup>
                <p:commandButton image="save" ajax="false"
                    style="margin-right:20px;" value="#{projetmsgs['navigation.save']}"
                    action="#{ProjetComponent.saveProjetUtilisateurs1(ProjetComponent.projet, ProjetComponent.projet.utilisateurs)}" />
        </h:panelGroup>

this the stack trace error: 这个堆栈跟踪错误:

 org.hibernate.QueryException: illegal attempt to dereference collection [utilisateu0_.iduser.projets] with element property reference [statut] [select u from com.gestion.projet.domain.Utilisateur u where u.projets  is empty or  u.projets.statut like 'Completed' ]

generated sql query with left join: 使用左连接生成sql查询:

 select * 
 from public.utilisateur utilisateu0_ 
      left outer join projet_utilisateur projets1_ on utilisateu0_.iduser=projets1_.utilisateurs_iduser 
      left outer join public.projet projet2_ on projets1_.projets_idprojet=projet2_.idprojet 
 where  
      not (exists (select projet2_.idprojet from public.projet projet2_)) or 
      projet2_.statut like 'Completed'

检查“Projet”课程中的拼写错误 - 它表示法规,而不是状态......

Try this query: 试试这个查询:

select u 
from Utilisateur u 
left join u.projets p
where 
    p is null or 
    p.statut like 'Completed'"

See this , you need to join before use a attribute. 看到这个 ,你需要在使用属性之前加入。 The difference between join and left join is that left join get all Utilisateur , include the ones that dont have a project. joinleft join之间的区别在于left join获取所有Utilisateur ,包括没有项目的那些。

Change is empty for is null , now, in my local test works well!, see this gist for a complete example!. 更改is emptyis null ,现在,在我的本地测试中运行良好!,请参阅此要点以获取完整示例!

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

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