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

Java EE Discussion :

Problème dao, liste avec doublons


Sujet :

Java EE

  1. #1
    Membre habitué
    Homme Profil pro
    Développeur Java
    Inscrit en
    Novembre 2008
    Messages
    88
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2008
    Messages : 88
    Points : 154
    Points
    154
    Par défaut Problème dao, liste avec doublons
    Bonjour ,

    Je developpe un projet de gestion de tests et j'ai 4 questions dans la base de donnée. Lorsque je veut extraire les questions je reçoit une liste avec 15 éléments avec des doublons.
    voici la liste reçus :

    1, Java est un language
    1, Java est un language
    1, Java est un language
    2, Java est un langage développé par
    2, Java est un langage développé par
    2, Java est un langage développé par
    2, Java est un langage développé par
    3, Choisir la bonne réponse
    3, Choisir la bonne réponse
    3, Choisir la bonne réponse
    3, Choisir la bonne réponse
    4, La liaison tardive est essentielle pour assurer
    4, La liaison tardive est essentielle pour assurer
    4, La liaison tardive est essentielle pour assurer
    4, La liaison tardive est essentielle pour assurer


    J'ai le mapping suivant entre un test et les questions
    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
    public class Test{
           	private List<Question> questions;
    ....
    	@OneToMany(mappedBy="test",fetch=FetchType.EAGER)
    	@Cascade( { org.hibernate.annotations.CascadeType.ALL })
    	public Collection<Question> getQuestions() {
    		return questions;
    	}
    	public void setQuestions(List<Question> questions) {
    		this.questions = questions;
            }
     
    public class Question{
    	private Test test;
    ....
    	@ManyToOne
    	@JoinColumn(name="test_id")
    	public Test getTest() {
    		return test;
    	}
     
    	public void setTest(Test test) {
    		this.test = test;
    	}
    }
    J'utilise la fonction dao suivante que j'hérite avec TestDao:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    @Transactional(propagation = Propagation.SUPPORTS)
    public class BaseDaoHibernate<T> extends HibernateDAO implements IDao<T> {
    	private Class<T> type;
    .....
    	@SuppressWarnings("unchecked")
    	public T findById(Serializable _id) {
     
    		T o = (T) this.getHibernateTemplate().get(type, _id);
    		return o;
    	}
    }
    Dans le service de test j'ai la classe qui génère l'output:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    public Test getById(int id) {
    		// TODO Auto-generated method stub
    		Test test = testDao.findById(id);
    		System.out.println("TestServiceImpl >> questions.size: "+test.getQuestions().size());
    		for(Question q: test.getQuestions())
    			System.out.println(q.getId()+", "+q.getLibelle());
    		return test;
    	}
    Voici le log généré par log4j:
    support.AbstractBeanFactory>> 13:54:50,546 DEBUG DefaultListableBeanFactory:245 - Returning cached instance of singleton bean 'transactionManager'
    support.AbstractPlatformTransactionManager>> 13:54:50,546 DEBUG HibernateTransactionManager:365 - Creating new transaction with name [com.businessdecision.tact.bdtechtest.services.Impl.TestServiceImpl.getById]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
    hibernate3.HibernateTransactionManager>> 13:54:50,546 DEBUG HibernateTransactionManager:493 - Opened new Session [org.hibernate.impl.SessionImpl@24389c] for Hibernate transaction
    hibernate3.HibernateTransactionManager>> 13:54:50,546 DEBUG HibernateTransactionManager:504 - Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@24389c]
    datasource.DataSourceUtils>> 13:54:50,546 DEBUG DataSourceUtils:110 - Fetching JDBC Connection from DataSource
    hibernate3.HibernateTransactionManager>> 13:54:50,562 DEBUG HibernateTransactionManager:569 - Exposing Hibernate transaction as JDBC transaction [Transaction-aware proxy for target Connection [com.mysql.jdbc.JDBC4Connection@189b9bd]]
    support.TransactionSynchronizationManager>> 13:54:50,562 DEBUG TransactionSynchronizationManager:193 - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@12f0ce9] for key [org.springframework.jdbc.datasource.SingleConnectionDataSource@f8f104] to thread [http-8080-1]
    support.TransactionSynchronizationManager>> 13:54:50,562 DEBUG TransactionSynchronizationManager:193 - Bound value [org.springframework.orm.hibernate3.SessionHolder@1f8a4fd] for key [org.hibernate.impl.SessionFactoryImpl@87286] to thread [http-8080-1]
    support.TransactionSynchronizationManager>> 13:54:50,562 DEBUG TransactionSynchronizationManager:272 - Initializing transaction synchronization
    interceptor.TransactionAspectSupport>> 13:54:50,562 DEBUG TransactionInterceptor:362 - Getting transaction for [com.businessdecision.tact.bdtechtest.services.Impl.TestServiceImpl.getById]
    interceptor.AbstractFallbackTransactionAttributeSource>> 13:54:50,578 DEBUG AnnotationTransactionAttributeSource:106 - Adding transactional method 'getById' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
    interceptor.TransactionAspectSupport>> 13:54:50,578 DEBUG AnnotationTransactionAspect:339 - Skipping transactional joinpoint [com.businessdecision.tact.bdtechtest.services.Impl.TestServiceImpl.getById] because no transaction manager has been configured
    interceptor.TransactionAspectSupport>> 13:54:50,578 DEBUG AnnotationTransactionAspect:362 - Getting transaction for [com.businessdecision.tact.bdtechtest.services.Impl.TestServiceImpl.getById]
    support.AbstractBeanFactory>> 13:54:50,578 DEBUG DefaultListableBeanFactory:245 - Returning cached instance of singleton bean 'transactionManager'
    support.TransactionSynchronizationManager>> 13:54:50,578 DEBUG TransactionSynchronizationManager:140 - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@1f8a4fd] for key [org.hibernate.impl.SessionFactoryImpl@87286] bound to thread [http-8080-1]
    hibernate3.HibernateTransactionManager>> 13:54:50,578 DEBUG HibernateTransactionManager:437 - Found thread-bound Session [org.hibernate.impl.SessionImpl@24389c] for Hibernate transaction
    support.TransactionSynchronizationManager>> 13:54:50,578 DEBUG TransactionSynchronizationManager:140 - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@12f0ce9] for key [org.springframework.jdbc.datasource.SingleConnectionDataSource@f8f104] bound to thread [http-8080-1]
    support.AbstractPlatformTransactionManager>> 13:54:50,578 DEBUG HibernateTransactionManager:470 - Participating in existing transaction
    interceptor.TransactionAspectSupport>> 13:54:50,578 DEBUG TransactionInterceptor:362 - Getting transaction for [com.businessdecision.tact.bdtechtest.dao.Utils.BaseDaoHibernate.findById]
    interceptor.AbstractFallbackTransactionAttributeSource>> 13:54:50,593 DEBUG AnnotationTransactionAttributeSource:106 - Adding transactional method 'findById' with attribute: PROPAGATION_SUPPORTS,ISOLATION_DEFAULT; ''
    interceptor.TransactionAspectSupport>> 13:54:50,593 DEBUG AnnotationTransactionAspect:339 - Skipping transactional joinpoint [com.businessdecision.tact.bdtechtest.dao.Utils.BaseDaoHibernate.findById] because no transaction manager has been configured
    interceptor.TransactionAspectSupport>> 13:54:50,593 DEBUG AnnotationTransactionAspect:362 - Getting transaction for [com.businessdecision.tact.bdtechtest.dao.Utils.BaseDaoHibernate.findById]
    support.TransactionSynchronizationManager>> 13:54:50,593 DEBUG TransactionSynchronizationManager:140 - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@1f8a4fd] for key [org.hibernate.impl.SessionFactoryImpl@87286] bound to thread [http-8080-1]
    support.TransactionSynchronizationManager>> 13:54:50,593 DEBUG TransactionSynchronizationManager:140 - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@1f8a4fd] for key [org.hibernate.impl.SessionFactoryImpl@87286] bound to thread [http-8080-1]
    hibernate3.HibernateTemplate>> 13:54:50,593 DEBUG HibernateTemplate:397 - Found thread-bound Session for HibernateTemplate
    Hibernate: select test0_.test_id as test1_1_4_, test0_.duree as duree1_4_, test0_.niveau as niveau1_4_, test0_.nombre as nombre1_4_, test0_.id_technologie as id5_1_4_, test0_.utilisateur_id as utilisat6_1_4_, questions1_.test_id as test4_1_6_, questions1_.id as id6_, questions1_.id as id2_0_, questions1_.libelle as libelle2_0_, questions1_.niveau as niveau2_0_, questions1_.test_id as test4_2_0_, reponses2_.id_Question as id4_2_7_, reponses2_.id as id7_, reponses2_.id as id5_1_, reponses2_.etat as etat5_1_, reponses2_.libelle as libelle5_1_, reponses2_.id_Question as id4_5_1_, technologi3_.id as id3_2_, technologi3_.libelle as libelle3_2_, utilisateu4_.id as id6_3_, utilisateu4_.email as email6_3_, utilisateu4_.hashed as hashed6_3_, utilisateu4_.login as login6_3_, utilisateu4_.mot_de_passe as mot5_6_3_, utilisateu4_.nom as nom6_3_, utilisateu4_.prenom as prenom6_3_, utilisateu4_.salt as salt6_3_, utilisateu4_.type as type6_3_ from Test test0_ left outer join Question questions1_ on test0_.test_id=questions1_.test_id left outer join Reponse reponses2_ on questions1_.id=reponses2_.id_Question left outer join Technologie technologi3_ on test0_.id_technologie=technologi3_.id left outer join Utilisateur utilisateu4_ on test0_.utilisateur_id=utilisateu4_.id where test0_.test_id=?
    support.TransactionSynchronizationManager>> 13:54:50,593 DEBUG TransactionSynchronizationManager:140 - Retrieved value [org.springframework.jdbc.datasource.ConnectionHolder@12f0ce9] for key [org.springframework.jdbc.datasource.SingleConnectionDataSource@f8f104] bound to thread [http-8080-1]
    hibernate3.HibernateTemplate>> 13:54:50,609 DEBUG HibernateTemplate:422 - Not closing pre-bound Hibernate Session after HibernateTemplate
    interceptor.TransactionAspectSupport>> 13:54:50,609 DEBUG TransactionInterceptor:391 - Completing transaction for [com.businessdecision.tact.bdtechtest.dao.Utils.BaseDaoHibernate.findById]
    TestServiceImpl >> questions.size: 15
    Je ne comprend pas d'ou vient le problème,

    Merci d'avance pour votre aide.

  2. #2
    Membre habitué
    Homme Profil pro
    Développeur Java
    Inscrit en
    Novembre 2008
    Messages
    88
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Novembre 2008
    Messages : 88
    Points : 154
    Points
    154
    Par défaut Problem resolved using a Hashset
    Il suffit de créer un HashSet à partir de la liste pour supprimer les doublons.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    test = testService.getById(test.getId());
     
    //convert to Hashset to eliminate duplicates
    Set<Question>  questionsList = new HashSet<Question>(test.getQuestions());
     
    questions = new ArrayList<Question>(questionsList);
    pour plus d'information:
    http://stackoverflow.com/questions/1...cates-elements

Discussions similaires

  1. problème de liste, avec des dates ?
    Par pierre3401 dans le forum Général Python
    Réponses: 5
    Dernier message: 12/08/2013, 12h59
  2. Réponses: 2
    Dernier message: 26/01/2013, 01h00
  3. retirer des éléments d'une liste avec doublons
    Par tanguy.L dans le forum Prolog
    Réponses: 7
    Dernier message: 28/07/2010, 15h44
  4. [SP-2007] Problème Affichage Liste avec nouveau thème
    Par genzo93 dans le forum SharePoint
    Réponses: 1
    Dernier message: 19/02/2010, 11h02
  5. [MySQL] Problème de listes déroulantes liées avec requêtes sql
    Par richton95 dans le forum PHP & Base de données
    Réponses: 5
    Dernier message: 21/12/2005, 16h04

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