简体   繁体   中英

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:

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

and this the Utilisateur Entity:

@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:

@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:

<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:

 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.

Change is empty for is null , now, in my local test works well!, see this gist for a complete example!.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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