IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Persistance des données Java Discussion :

critical query where avec plusieurs argument


Sujet :

Persistance des données Java

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Octobre 2009
    Messages
    226
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 226
    Points : 72
    Points
    72
    Par défaut critical query where avec plusieurs argument
    bonjour pouvez vous m'aider avec le code suivant :
    les condition du where ne sont pas prise en compte pouvez vous me dire pourquoi ?

    merci d'avance .

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
     
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
          CriteriaQuery<Tuple> cq= cb.createTupleQuery();
          Predicate predicate = cb.and();
          Root<SessionPersonne> root = cq.from(SessionPersonne.class);
            Join <ReponseClient,SessionPersonne> reponseClient = null;
            Join <Categorie,SessionPersonne> categorie = null;
            Join<Reponse,ReponseClient> reponse = null;
            Join<Question,Reponse> question = null;
            Join <Personne,SessionPersonne> personne = null;
            Join <Session,SessionPersonne> Session = null;
            Join <Entreprise,Personne> entreprise = null;
            Join <Adresse,Entreprise>  adresse = null;
            Join <Localite,Adresse>  localite = null;
            Join <Departement,Localite>  departement = null;
            Join <Pays,Localite>  pays = null;
            Join <Civilite,Personne> civilite = null ;
            Join <AutreInformation,Personne> autreInformation = null;
            Expression<String> champsCrm =null;
     
             personne =  root.join("personne");
             Session = root.join("session");
     
             if("Personne".equals(crm.getNomTable())){
                 champsCrm = personne.get(crm.getNomChamps());
             }
             // ==========================jointure =======================================
             if("Entreprise".equals(crm.getNomTable()) && entreprise== null){
                 entreprise = personne.join("entreprise");
                 champsCrm = entreprise.get(crm.getNomChamps());
               //   System.out.println("jointure Entreprise");
             }
            if("Adresse".equals(crm.getNomTable()) && adresse== null){
                if(entreprise== null)  entreprise = personne.join("entreprise");
                if(adresse== null) adresse = entreprise.join("Adresse");
               // System.out.println("jointure adresse"); 
                champsCrm = adresse.get(crm.getNomChamps());
             } 
             if("Localite".equals(crm.getNomTable()) && localite== null){
               if(entreprise== null)  entreprise = personne.join("entreprise");
               if(adresse== null) adresse = entreprise.join("adresse");
               if(localite == null) localite = adresse.join("localite");
               champsCrm = localite.get(crm.getNomChamps());
              // System.out.println("jointure Localite"); 
             } 
             if("Departement".equals(crm.getNomTable()) && departement== null){
               if(entreprise== null)  entreprise = personne.join("entreprise");
               if(adresse== null) adresse = entreprise.join("adresse");
               if(localite == null) localite = adresse.join("localite");
               if(departement== null)pays = localite.join("departement");
               champsCrm = departement.get(crm.getNomChamps());
               //System.out.println("jointure Pays"); 
             }   
             if("Pays".equals(crm.getNomTable()) && pays== null){
               if(entreprise== null)  entreprise = personne.join("entreprise");
               if(adresse== null) adresse = entreprise.join("adresse");
               if(localite == null) localite = adresse.join("localite");
               if(pays== null)pays = localite.join("pays");
               champsCrm = pays.get(crm.getNomChamps());
               //System.out.println("jointure Pays"); 
             }   
             if("Civilite".equals(crm.getNomTable()) && civilite== null){ 
               civilite = personne.join("civilite");
               champsCrm = civilite.get(crm.getNomChamps());
               //System.out.println("jointure Civilite");
             }
     
            if("AutreInformation".equals(crm.getNomTable()) && autreInformation== null){ 
                 autreInformation = personne.join("AutreInformation");
                 champsCrm = autreInformation.get(crm.getNomChamps());
                 //System.out.println("jointure autre inforation ");
             }
             if("Categorie".equals(crm.getNomTable()) && categorie== null){ 
                 categorie = root.join("categorie");
                 champsCrm = categorie.get(crm.getNomChamps());
                 //System.out.println("jointure autre inforation ");
             }
             if("Question".equals(crm.getNomTable()) && reponseClient== null){ 
                 reponseClient = root.join("reponseClientCollection");
                 reponse = reponseClient.join("reponse");
                 question = reponse.join("question");
                 champsCrm = reponse.get("nomReponse");
     
     
                 //System.out.println("reponseClient");
             }
            // ========================== recherche =======================================
     
            Expression<String> userId = root.get("keySessionPersonne");
            Expression<Long> count = cb.count(userId);
            cq.multiselect(champsCrm.alias("USERID"), count.alias("CNT"));
     
     
             Expression<String> sessionType = Session.get("keySession");
             Expression<Date> date = root.get("dateCreation");
             cb.and(cb.equal(sessionType,session));
             cb.and(cb.between(date, dateDebut, dateFin));
     
     
           //cq.where(cb.between(root.get("dateCreation"),dateDebut,dateFin));
             if("Question".equals(crm.getNomTable())){
                Expression<String> ExpressionQuestion = question.get("nomQuestion");
                cb.and(cb.like(ExpressionQuestion,crm.getNomChamps()));
            }
     
           if("PE".equals(typeChoisi)){
               Expression<String> pe = root.get("typeEnregistrement");
               cb.and(cb.equal(pe,"PE"));
     
           }
           if("SS".equals(typeChoisi)){
              Expression<String> ss = root.get("typeEnregistrement");
              cb.and(cb.equal(ss,"SS")); 
           }
            if("PAV".equals(typeChoisi)){
                Expression<Collection<String>> passageCollection = root.get("passageCollection");
                Expression<Integer> size = cb.size(passageCollection);
                cb.and(cb.equal(size,0)); 
            }
            if("PEV".equals(typeChoisi)){
                Expression<Collection<String>> passageCollection = root.get("passageCollection");
                Expression<Integer> size = cb.size(passageCollection);
                cb.and(cb.equal(size,1)); 
            }
            if("R".equals(typeChoisi)){
                 Expression<Collection<String>> passageCollection = root.get("passageCollection");
                 Expression<Integer> size = cb.size(passageCollection);
                 cb.and(cb.greaterThan(size,1)); 
     
            }
            cq.where(cb.and());
            cq.groupBy(champsCrm);
            List<Reporting> lResporting = new ArrayList<>();
            TypedQuery<Tuple> tq = getEntityManager().createQuery(cq);
            for (Tuple t : tq.getResultList()) {
              //System.out.println(t.get("USERID")+" "+t.get("CNT"));
              lResporting.add(new Reporting((Long)t.get("CNT"),(String)t.get("USERID")));
     
        }
            return lResporting ;

  2. #2
    Membre chevronné
    Homme Profil pro
    Directeur technique
    Inscrit en
    Janvier 2007
    Messages
    1 348
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Directeur technique

    Informations forums :
    Inscription : Janvier 2007
    Messages : 1 348
    Points : 1 787
    Points
    1 787
    Par défaut
    Salut !

    Beaucoup de choses qui piquent un peu les yeux dans ton code (conventions de nommage, lisibilité, ...).
    La façon dont tu construis ton where ne va pas du tout. Tu utilises un criteria builder, et notamment la méthode and, mais tu n'utilises pas le résultat
    Il faut que tu fasses qqchose comme
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    Predicate wherePredicate = cb.and();
    ...
    wherePredicate = cb.and(wherePredicate , cb.equal(sessionType,session));
    wherePredicate = cb.and(wherePredicate , cb.between(date, dateDebut, dateFin));
    ...

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Octobre 2009
    Messages
    226
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 226
    Points : 72
    Points
    72
    Par défaut
    un grand merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2003] Fonction 'like' avec plusieurs arguments
    Par bdoux33 dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 14/05/2009, 14h44
  2. Réponses: 1
    Dernier message: 03/06/2008, 10h21
  3. somme avec plusieurs arguments
    Par new_wave dans le forum Langage SQL
    Réponses: 1
    Dernier message: 27/05/2007, 19h12
  4. callback avec plusieurs arguments
    Par Claiw dans le forum GTK+ avec C & C++
    Réponses: 2
    Dernier message: 23/12/2006, 18h46
  5. switch avec plusieurs arguments
    Par lazzeroni dans le forum Langage
    Réponses: 4
    Dernier message: 18/07/2006, 13h34

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo