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 :

Problème d'initialisation des relations LAZY


Sujet :

Hibernate Java

  1. #1
    Invité
    Invité(e)
    Par défaut Problème d'initialisation des relations LAZY
    Bonjour à tous


    Je viens vers vous pour un problème qui a apparu "soudainement", dans le sens ou j'ai dû trifouiller des fichiers de conf pour régler un soucis. Et depuis j'ai une exception qui se lève :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Caused by: org.hibernate.HibernateException: collection is not associated with any session

    Voici le code qui lève l'exception :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    public List<Personne> getCurrentPersonne(Date start, Date end) {
    		List<Personne> list = personneRepo.findByStartDateAndEnDate(start, end);
    		for (Personne unit: list){
    			Hibernate.initialize(unit.getImpactedIntervention());
    		}
    		return list;
    	}


    Ce code a fonctionné donc le soucis ne vient sûrement pas de là. Auriez vous des pistes de recherche pour rectifier ce soucis ?

    Je suis dispo pour tout complément d'info !

    Bonne journée à tous !

  2. #2
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Tu pourrais montrer la stacktrace complète de ton erreur. Quel est le mapping de la classe Personne. En gros le message te dit qu'hibernate tente d'initializer une collection qui n'est pas gérée par hibernate. Est-ce que impactedintervention ne serait pas une collection et que celle ci aurait été marquée comme transient?

  3. #3
    Invité
    Invité(e)
    Par défaut
    Bonjour à toi, alors tout d'abord la trace de l'exception.

    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
    Caused by: org.hibernate.HibernateException: collection is not associated with any session
    	at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:474)
    	at org.hibernate.Hibernate.initialize(Hibernate.java:417)
    	at com.org.test.service.PersonneServiceImpl.getCurrentPersonne(PersonneServiceImpl.java:522)
    	at com.org.test.controller.PersonneController.getNextPersonne(PersonneController.java:527)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:606)
    	at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
    	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100)
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604)
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565)
    	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    	... 38 more

    Et le mapping de cette relation dans la classe personne

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @ManyToMany(fetch=FetchType.LAZY)
    	@JoinTable(
    		name="\"PersonneIntervention\""
    		, joinColumns={
    			@JoinColumn(name="\"personneID\"")
    			}
    		, inverseJoinColumns={
    			@JoinColumn(name="\"interventionID\"")
    			}
    		)
    	@NotFound(action=NotFoundAction.IGNORE)
    	private List<Intervention> impactedIntervention;

  4. #4
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Est-ce que la session utilisée par personneRepo.findByStartDateAndEnDate est bien toujours ouverte. Ce message semble dire le contraire.

  5. #5
    Invité
    Invité(e)
    Par défaut
    C'est pas moi qui gère cette méthode mais le repository JPA

  6. #6
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    Je doute fort que JPA aie créé une méthode appelée findByStartDateAndEnDate ainsi que la classe personneRepo

    et ça ne réponds toujours pas à la question de savoir comment est gérée la session.

  7. #7
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 313
    Billets dans le blog
    1
    Par défaut
    Peux-tu montrer le code de com.org.test.service.PersonneServiceImpl ?
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  8. #8
    Invité
    Invité(e)
    Par défaut
    Bonjour,

    Citation Envoyé par tchize_ Voir le message
    Je doute fort que JPA aie créé une méthode appelée findByStartDateAndEnDate ainsi que la classe personneRepo

    et ça ne réponds toujours pas à la question de savoir comment est gérée la session.
    le repository c'est moi qui l'ai créé oui, mais c'est qu'une interface. Fin c'est un repo JPA quoi... Concernant la session je ne sais quoi te répondre :/


    @OButterlin

    Bonjour à toi
    la méthode qui pose problème suffit ou le source entier?

  9. #9
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 313
    Billets dans le blog
    1
    Par défaut
    le source entier serait mieux
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  10. #10
    Invité
    Invité(e)
    Par défaut
    Je me suis permis de cleaner un peu le code (notamment pas mal de méthode inutilisé et/ou commentés) ce qui explique la diff au niveau des numéros de lignes. J'ai mis en gras l'instruction levant l'exception. (ligne 156 ici, le gras est pas assez visible)


    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
                                     package com.org.test.service;
    
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.HashSet;
    import java.util.List;
    
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    
    import org.apache.commons.collections.ListUtils;
    import org.apache.log4j.Logger;
    import org.hibernate.Hibernate;
    import org.joda.time.DateTime;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    
    import com.org.test.model.Personne;
    import com.org.test.model.SupportGroup;
    import com.org.test.model.TargetSite;
    import com.org.test.model.Task;
    
    
    @Service
    @Transactional
    public class PersonneServiceImpl implements PersonneService {
    
    	@Autowired
    	PersonneRepository personneRepo;
    	
    
    
    	
    
    
    	
    
    	
    	public List<Personne> findAllPersonne() {
    		return personneRepo.findAll();
    	}
    
    
    
    	public boolean isCIROK(String idPersonne) {
    		return (personneRepo.findById(idPersonne)).getRisk().equals("Low");
    	}
    	
    	public List<Object[]> countPersonneByCategory() {
    		return personneRepo.countAllPersonneByCategory();
    	}
    
    	
    	public List<Object[]> countPersonneByStatus(String category) {
    		return personneRepo.countAllPersonneByStatus(category);
    	}
    
    	/*
    	 * Get Personne list for the Personne calendar
    	 */
    	public List<Personne> getPersonneCalendar(DateTime s, DateTime e) {
    		List<Personne> listPersonne = personneRepo.findByPlannedStartDateBetweenOrderByPlannedStartDateAsc(s.toDate(), e.toDate());
    		for (Personne unit : listPersonne){
    			try {
    				Hibernate.initialize(unit.getTasks());
    				Hibernate.initialize(unit.getSupportGroups());
    			}
    			catch (Exception ex){
    				System.err.println(ex.getMessage());
    			}
    		}
    		return listPersonne;
    	}
    	
    	/*
    	 * Update the alerts of all Personne over 6 months base on the implemented rules.
    	 */
    	public void updateAlertsOnIntervention() {
    
    			
    	}
    	
    	
    	/*
    	 * Update the comment done during the intervention
    	 * @param id
    	 * 			the id of the Personne
    	 * @param comment
    	 * 			comment to add
    	 * 
    	 * @return the complete comment
    	 */
    	public String updateInterventionComment(String id, String comment) {
    		Personne unit = personneRepo.findById(id);
    		Date today = new Date();
    		SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    		if (unit.getInterventionComment()!=null)
    			unit.setInterventionComment(unit.getInterventionComment()+"<br/>"+df.format(today)+" : "+comment);
    		else
    			unit.setInterventionComment(df.format(today)+" : "+comment);
    		return unit.getInterventionComment();
    	}
    
    
    	
    
    	
    	/*
    	 * Get all Personne between dates.
    	 * @param start
    	 * 			planned start date boundary
    	 * @param end
    	 * 			planned end date bounday
    	 * 
    	 * @return a list of Personne
    	 */
    	public List<Personne> getPersonneBetween(Date start, Date end) {
    		List<Personne> list = personneRepo.findByStartDateAndEndDate(start, end);
    		for (Personne c:list){
    			Hibernate.initialize(c.getImpactedIntervention());   
    			c.setNbTasks();
    		}
    		return list;
    	}
    
    			
    
    
    
    
    
    
    	public Personne save(Personne unit) {
    		Personne unitSaved = personneRepo.findById(unit.getId());
    		if (unitSaved!=null){
    			return unitSaved;
    		}
    		else {
    			personneRepo.save(unit);
    			return unit;
    		}
    	}
    	
    	
    
     
     
    	
    
    	public List<Personne> getCurrentPersonne(Date start, Date end) {
    		List<Personne> list = personneRepo.findByStartDateAndEndDate (start, end, start, end);
    		for (Personne unit : list){
    			Hibernate.initialize(unit.getImpactedIntervention());
    		}
    		return list;
    	}
    
    
    
    
    
    
    
    	@Override
    	public List<Object[]> getPersonneOnTargetSiteByStatus(List<String> status) {
    		return personneRepo.currentPersonneTargetSites();
    	}
    	
    
    	
    }

  11. #11
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 313
    Billets dans le blog
    1
    Par défaut
    Il semble que "personneRepo.findByStartDateAndEndDate(...)" ouvre la session au début de la méthode et la ferme à la sortie...
    Du coup, comme ta collection n'est pas initialisée, son usage dans la boucle plante... normal...

    Tu peux mettre le code de personneRepo ?
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

Discussions similaires

  1. Réponses: 2
    Dernier message: 30/06/2015, 15h23
  2. [AC-2007] Problème au niveau DES RELATIONS
    Par adama13 dans le forum Access
    Réponses: 38
    Dernier message: 27/12/2014, 18h15
  3. Deadlock à l'initialisation des relations
    Par djarBoy dans le forum QxOrm
    Réponses: 8
    Dernier message: 16/03/2012, 09h02
  4. Réponses: 5
    Dernier message: 14/08/2008, 10h13
  5. Problème de suppression des relations de table
    Par mouche dans le forum Access
    Réponses: 2
    Dernier message: 10/05/2007, 13h50

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