-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Problème de requête DISTINCT
PostPosted: Fri Jan 04, 2008 9:45 am 
Newbie

Joined: Fri Jan 04, 2008 4:23 am
Posts: 7
Bonjour,

Je cherche à effectuer un DISTINCT en hibernate pour enlever les doublons :

Code:
public List findCodeEmetteurById(Long id) {
          List result = null;
          Session session = sessionFactory.getCurrentSession();
          try
          {           
            session.beginTransaction();
            Criteria critere = session.createCriteria(ClientSi.class)
            .add(Restrictions.eq("client.CIdClient",id))           
            .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);                       
            result = critere.list();         
          }catch (Exception e){
               e.printStackTrace();
          }finally{
               session.getTransaction().commit();
               HibernateUtil.closeSession(session);
          }
          return result;
     }


Or ce code me renvoie des doublons, par exemple :

123
123
147
123
147
154

Comment n'avoir que :
123
147
154

En n'utilisant que les Critères ?

Merci d'avance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 1:21 pm 
Beginner
Beginner

Joined: Thu Nov 15, 2007 11:27 am
Posts: 34
Le setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); fonctionne bien pour les jointures. faite a base de criteria.createCriteria("monchamp").add(Restriction.eq("blabla",blablabla));

Peux tu poster la commande SQL générée?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 04, 2008 2:33 pm 
Newbie

Joined: Fri Jan 04, 2008 4:23 am
Posts: 7
OK merci comment je fais pour poster la commande SQL générée ?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 06, 2008 7:06 pm 
Beginner
Beginner

Joined: Thu Nov 15, 2007 11:27 am
Posts: 34
check http://www.hibernate.org/hib_docs/v3/re ... figuration
et
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

ou ajoute
hibernate.show_sql = true
hibernate.format_sql = true
dans ton hibernate.properties
(en fonction de ce que tu utilises comme config)


Last edited by Yoann56 on Mon Jan 07, 2008 4:23 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 06, 2008 7:34 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
gandalf,

Si tu veux obtenir un "distinct", tu n'as pas besoin d'une "transformation", mais d'une "projection", Utiliser Criteria.DISTINCT_ROOT_ENTITY seulement garantie que, si le resultset obtenu a des JOINS père-enfant, le père ne sera répété qu'une fois, mais ça n'est pas le même que écrire un "distinct" dans ton code SQL.

Remarque que, si on demande un distinct dans un SQL query normal pour obtenir des parent uniques, on détruit quelques enfants. Voilà la raison por laquelle on utilise la transformation Criteria.DISTINCT_ROOT_ENTITY: elle rend l'"arbre" complet, sans détruire ni les parents (tout eux différents) ni les enfants.

Pourtant, ça n'est pas ton cas, je crois. Tu veux vraiment apliquer un "distinct" sur tes résultats. Ce genre d'operation on l'accomplit avec des "projections", par example:

Code:
   tonCritere.setProjection(   
  Projections.distinct(Projections.projectionList().add(
     Projections.property("taPropriete"),

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 16, 2008 5:40 am 
Newbie

Joined: Fri Jan 04, 2008 4:23 am
Posts: 7
Ok je vais tester, merci.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.