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

Hibernate Java Discussion :

Pb Requete HQL [HQL]


Sujet :

Hibernate Java

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Chargé d'affaire
    Inscrit en
    Avril 2015
    Messages
    8
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Chargé d'affaire
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2015
    Messages : 8
    Points : 5
    Points
    5
    Par défaut Pb Requete HQL
    Bonjour,
    J'ai un soucis avec mon code en Java sous eclipse et Hibernate tool.
    J'execute des requetes sous hibernate tool et la réponse est correcte. Par contre ma requete ne fonctionne pas dans Java. Ca fait 3 jours que je galère pour une requete qui je pense doit etre simple. Du coup je sais plus si mon problème vient de ma requete ou de mon code Java.
    Je vous expose mon probleme:
    J'ai 3 classes : Item, Analyse, Contact
    L'item correspond au sujet majeur. L'Analyse est le constat du problème pour lequel on a ouvert l'item. Le contact est celui qui managera l'analyse. Il peut y avoir plusieurs analyse et plusieurs contact.
    Ma config hibernate est la suivante :
    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
     
    <!-- La configuration de classe Item  -->
     
       <class name="methode.report.Item" table="ITEM"  lazy="false" >
          <meta attribute="class-description">la classe item </meta>
          <id name="idItem" type="integer">
             <generator class="native"/>
          </id>
     
          <!-- xxxxx -->
          <property name="nameItem" type="string" not-null="true"/>
          <set name="usine"  lazy="false" cascade="save-update"  table="usineItem" >
          <key column="idItem"   /> 
            <many-to-many class="metier.usine.Usine"   />
           </set>
           <set name="nameAnalyse"  lazy="false" cascade="save-update"  table="analyseItem" >
          <key column="idItem"   /> 
            <many-to-many class="methode.report.Analyse"   />
           </set>
          <property name="dateCreation" type="date" not-null="true"/>
          <property name="dateUpdate" type="date" />
          <property name="heureUpdate" type="date" />
          <property name="statusItem" type="integer" />
          <property name="dateClosed" type="date" />
          <property name="dateCanceled" type="date" />
          <property name="dateLastRelease" type="date" />
          <set name="produits"  table="produitsItem" cascade="all" lazy="false"> 
             <key column="idItem"/>
             <element column="produits" type="string"/>
          </set>
     
     
     
         </class>
     
     <!-- La configuration de classe Analyse  -->
     
       <class name="methode.report.Analyse" table="ANALYSE"  lazy="false" >
          <meta attribute="class-description">la classe analyse </meta>
          <id name="idAnalyse" type="integer">
             <generator class="native"/>
          </id>
     
          <!-- xxxxx -->
          <property name="nameAnalyse" type="string" not-null="true"/>
     
           <set name="action"  lazy="false" cascade="save-update"  table="analyseAction" >
          <key column="idAnalyse"   /> 
            <many-to-many class="methode.report.Action"   />
           </set>
           <set name="manager"  lazy="false" cascade="save-update"  table="manageAnalyse" >
          <key column="idAnalyse"   /> 
            <many-to-many class="metier.utilisateurs.Contact"   />
           </set>
     
     
         </class>
     
     <!-- La configuration de classe Contact -->
     
       <class name="metier.utilisateurs.Contact" table="CONTACT"  lazy="false">
          <meta attribute="class-description">la classe contact </meta>
          <id name="idContact" type="integer">
             <generator class="native"/>
          </id>
     
           <property name="nom" type="string" not-null="true" />
           <property name="prenom" type="string" not-null="true" />
           <property name="email" type="string" />
           <property name="telephone" type="string"/>
           <property name="adresse" type="string" />
           <property name="fonction" type="string" />
           <many-to-one name="usine"  lazy="false" class="metier.usine.Usine" /> 
         </class>
    Et voici mes classes:
    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
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
     
    package methode.report;
     
     
    import java.util.Date;
    import java.util.HashSet;
    import java.util.Set;
     
    import metier.usine.Usine;
    import metier.utilisateurs.Contact;
    /*Cette classe est une classe métier, elle représentera une table de la base de donnée !*/
    public class Item {
    	// les attribut de la classe Item
    	private Integer idItem;
    	private String nameItem= "";
    	private Set<Usine> usine = new HashSet<Usine>();
    	private Set<Analyse> nameAnalyse = new HashSet<Analyse>();
    	private Set<Contact> manager = new HashSet<Contact>();
    	private Set<Action> action= new HashSet<Action>();
    	private Date dateCreation = new Date();
    	private Date dateUpdate = new Date();
    	private Date heureUpdate = new Date();
    	private Integer statusItem;
    	private Date dateClosed= new Date();
    	private Date dateCanceled= new Date();
    	private Date dateLastRelease= new Date();
    	private Set<String> produits= new HashSet<String>();
     
     
     
    	public Item() {
    		super();
    	}
     
     
     
    	public Item(Integer idItem, String nameItem, Set<Usine> usine,
    			Set<Analyse> nameAnalyse, Set<Contact> manager, Set<Action> action,
    			Date dateCreation, Date dateUpdate, Date heureUpdate,
    			Integer statusItem, Date dateClosed, Date dateCanceled,
    			Date dateLastRelease, Set<String> produits) {
    		super();
    		this.idItem = idItem;
    		this.nameItem = nameItem;
    		this.usine = usine;
    		this.nameAnalyse = nameAnalyse;
    		this.manager = manager;
    		this.action = action;
    		this.dateCreation = dateCreation;
    		this.dateUpdate = dateUpdate;
    		this.heureUpdate = heureUpdate;
    		this.statusItem = statusItem;
    		this.dateClosed = dateClosed;
    		this.dateCanceled = dateCanceled;
    		this.dateLastRelease = dateLastRelease;
    		this.produits = produits;
    	}
     
     
     
    	public Integer getIdItem() {
    		return idItem;
    	}
     
     
     
    	public void setIdItem(Integer idItem) {
    		this.idItem = idItem;
    	}
     
     
     
    	public String getNameItem() {
    		return nameItem;
    	}
     
     
     
    	public void setNameItem(String nameItem) {
    		this.nameItem = nameItem;
    	}
     
     
     
    	public Set<Usine> getUsine() {
    		return usine;
    	}
     
     
     
    	public void setUsine(Set<Usine> usine) {
    		this.usine = usine;
    	}
     
     
     
    	public Set<Analyse> getNameAnalyse() {
    		return nameAnalyse;
    	}
     
     
     
    	public void setNameAnalyse(Set<Analyse> nameAnalyse) {
    		this.nameAnalyse = nameAnalyse;
    	}
     
     
     
    	public Set<Contact> getManager() {
    		return manager;
    	}
     
     
     
    	public void setManager(Set<Contact> manager) {
    		this.manager = manager;
    	}
     
     
     
    	public Set<Action> getAction() {
    		return action;
    	}
     
     
     
    	public void setAction(Set<Action> action) {
    		this.action = action;
    	}
     
     
     
    	public Date getDateCreation() {
    		return dateCreation;
    	}
     
     
     
    	public void setDateCreation(Date dateCreation) {
    		this.dateCreation = dateCreation;
    	}
     
     
     
    	public Date getDateUpdate() {
    		return dateUpdate;
    	}
     
     
     
    	public void setDateUpdate(Date dateUpdate) {
    		this.dateUpdate = dateUpdate;
    	}
     
     
     
    	public Date getHeureUpdate() {
    		return heureUpdate;
    	}
     
     
     
    	public void setHeureUpdate(Date heureUpdate) {
    		this.heureUpdate = heureUpdate;
    	}
     
     
     
    	public Integer getStatusItem() {
    		return statusItem;
    	}
     
     
     
    	public void setStatusItem(Integer statusItem) {
    		this.statusItem = statusItem;
    	}
     
     
     
    	public Date getDateClosed() {
    		return dateClosed;
    	}
     
     
     
    	public void setDateClosed(Date dateClosed) {
    		this.dateClosed = dateClosed;
    	}
     
     
     
    	public Date getDateCanceled() {
    		return dateCanceled;
    	}
     
     
     
    	public void setDateCanceled(Date dateCanceled) {
    		this.dateCanceled = dateCanceled;
    	}
     
     
    	public Date getDateLastRelease() {
    		return dateLastRelease;
    	}
     
     
    	public void setDateLastRelease(Date dateLastRelease) {
    		this.dateLastRelease = dateLastRelease;
    	}
     
    	public Set<String> getProduits() {
    		return produits;
    	}
     
     
     
    	public void setProduits(Set<String> produits) {
    		this.produits = produits;
    	}
     
    	@Override
    	public String toString() {
    		return "Item [nameItem=" + nameItem + "]";
    	}
     
    }
    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
     
    package methode.report;
     
    import java.util.HashSet;
    import java.util.Set;
     
    import metier.utilisateurs.Contact;
     
    public class Analyse {
     
    	private Integer idAnalyse;
    	private String nameAnalyse="";
    	private Set<Action> action = new HashSet<Action>();
    	private Set<Contact> manager=new HashSet<Contact>();
     
     
    	public Analyse() {
    		super();
     
    	}
     
     
    	public Analyse(Integer idAnalyse, String nameAnalyse, Set<Action> action,
    			Set<Contact> manager) {
    		super();
    		this.idAnalyse = idAnalyse;
    		this.nameAnalyse = nameAnalyse;
    		this.action = action;
    		this.manager = manager;
    	}
     
     
    	public Integer getIdAnalyse() {
    		return idAnalyse;
    	}
     
     
    	public void setIdAnalyse(Integer idAnalyse) {
    		this.idAnalyse = idAnalyse;
    	}
     
     
    	public String getNameAnalyse() {
    		return nameAnalyse;
    	}
     
     
    	public void setNameAnalyse(String nameAnalyse) {
    		this.nameAnalyse = nameAnalyse;
    	}
     
     
    	public Set<Action> getAction() {
    		return action;
    	}
     
     
    	public void setAction(Set<Action> action) {
    		this.action = action;
    	}
     
     
    	public Set<Contact> getManager() {
    		return manager;
    	}
     
     
    	public void setManager(Set<Contact> manager) {
    		this.manager = manager;
    	}
     
    }
    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
     
    package metier.utilisateurs;
     
    import metier.usine.Usine;
     
    public class Contact {
    	  /* Les attribut du contact */
    	private Integer idContact ; 
    	private String nom = "";
    	private String prenom = "";
    	private String email= "";
    	private String telephone = ""; 
    	private String adresse = "";
    	private String fonction ="";
    	private Usine usine;
     
    	/* Constructeurs */
    	public Contact(Integer idContact, String nom, String prenom, String email,
    			String telephone, String adresse , String fonction , Usine usine) {
    		super();
    		this.idContact = idContact;
    		this.nom = nom;
    		this.prenom = prenom;
    		this.email = email;
    		this.telephone = telephone;
    		this.adresse = adresse;
    		this.fonction = fonction;
    		this.usine= usine;
    	}
    	  /*  */
    	public Contact() {
    		super();
    	}
    	public Contact(Contact contact) {
    		super();
    		this.idContact = contact.idContact;
    		this.nom = contact.nom;
    		this.prenom = contact.prenom;
    		this.email = contact.email;
    		this.telephone = contact.telephone;
    		this.adresse = contact.adresse;
    		this.fonction = contact.fonction;
    		this.usine = contact.usine;
    	}
     
    	 /* Getter - Setter  */
    	public Integer getIdContact() {
    		return idContact;
    	}
    	public void setIdContact(Integer idContact) {
    		this.idContact = idContact;
    	}
    	public String getNom() {
    		return nom;
    	}
    	public void setNom(String nom) {
    		this.nom = nom;
    	}
    	public String getPrenom() {
    		return prenom;
    	}
    	public void setPrenom(String prenom) {
    		this.prenom = prenom;
    	}
    	public String getEmail() {
    		return email;
    	}
    	public void setEmail(String email) {
    		this.email = email;
    	}
    	public String getTelephone() {
    		return telephone;
    	}
     
    	public Usine getUsine() {
    		return usine;
    	}
    	public void setUsine(Usine usine) {
    		this.usine = usine;
    	}
    	public void setTelephone(String telephone) {
    		this.telephone = telephone;
    	}
    	public String getAdresse() {
    		return adresse;
    	}
    	public void setAdresse(String adresse) {
    		this.adresse = adresse;
    	}
    	public String getFonction() {
    		return fonction;
    	}
    	public void setFonction(String fonction) {
    		this.fonction = fonction;
    	}
    	@Override
    	public String toString() {
    		return nom + " " + prenom;
    	} 
     
    }
    voici le code qui execute la requete HQL :
    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
     
    public class AnalyseDao {
    public static String getManager(Item item){ // 
            Session session = factory.openSession();
            String manager = null ; 
            Transaction tx = null;
            try{
            tx = session.beginTransaction();
               Query query = session.createQuery("select distinct manager FROM Analyse as analyse "
                 + "where item.idItem = :m");
               query.setInteger("m", item.getIdItem());// 
               manager =  (String) query.list().get(0);
               System.out.println("Requete Manager : "+manager);
               tx.commit();
            }catch (HibernateException e) {
               if (tx!=null) tx.rollback();
               logger.error(e.getMessage());
            }finally {
               session.close(); 
            }
            return manager;
         }
    }
    Le retour de ma requete est NULL.
    Pouvais vous m'aider svp ?
    D'avance merci

  2. #2
    Futur Membre du Club
    Homme Profil pro
    Chargé d'affaire
    Inscrit en
    Avril 2015
    Messages
    8
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Chargé d'affaire
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2015
    Messages : 8
    Points : 5
    Points
    5
    Par défaut Je progresse
    Alors je progresse :
    voici une requete qui marche :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
     Query query = session.createQuery("select distinct a.nameAnalyse FROM Item as item "
    + " left join item.nameAnalyse as a "
    + " where item.idItem = :m "); 
    query.setInteger("m", item.getIdItem());
    le probléme est que cette requete m'affiche l'intitulé de l'analyse qui se trouve dans "nameAnalyse". Moi je veux afficher celui qui manage cette analyse. le champ "manager"

    Voici la méthode que j'utilise et qui s'affiche sur la console eclipse :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Item [idItem=1, nameItem=W3-W4 welding Machine, usine=[STL], nameAnalyse=[Analyse [idAnalyse=1, nameAnalyse=Difficult to weld without bend the bimetal, action=[], manager=[moi ]]], manager=[], action=[], dateCreation=2014-06-01, dateUpdate=null, heureUpdate=null, statusItem=null, dateClosed=null, dateCanceled=null, dateLastRelease=null, produits=[Domae, C60]]
    Je suis pas loin mais j'arrive pas à écrire ma requête hibernate pour mon code java.
    Help please !

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Chargé d'affaire
    Inscrit en
    Avril 2015
    Messages
    8
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Gard (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Chargé d'affaire
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2015
    Messages : 8
    Points : 5
    Points
    5
    Par défaut Pb Résolu
    Ca marche !!! Pb résolu !

    (je me suis parlait tout seul sur ce sujet ! lol )

    Voici ma requete pour quelqu'un (au cas où )
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    Query query = session.createQuery("select distinct b.prenom FROM Item as item "
    + " left join item.nameAnalyse as a "
    + " left join a.manager as b "
    + " where item.idItem = :m "); 
    query.setInteger("m", item.getIdItem());
    A une prochaine

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

Discussions similaires

  1. [Hibernate] problème requete HQL !
    Par fadjerx dans le forum Hibernate
    Réponses: 6
    Dernier message: 11/08/2007, 13h45
  2. executer une requete hql
    Par oasma dans le forum Hibernate
    Réponses: 1
    Dernier message: 13/05/2007, 09h54
  3. Réponses: 8
    Dernier message: 27/04/2007, 11h47
  4. Requete HQL avec jointure
    Par chriscoolletoubibe dans le forum Hibernate
    Réponses: 50
    Dernier message: 24/04/2007, 16h54
  5. Requete HQL hibernate
    Par DanZzz dans le forum Hibernate
    Réponses: 4
    Dernier message: 05/06/2006, 15h59

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