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 :

failed to lazily initialize a collection of role


Sujet :

Hibernate Java

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Développeur Java
    Inscrit en
    Avril 2016
    Messages
    65
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Avril 2016
    Messages : 65
    Points : 36
    Points
    36
    Par défaut failed to lazily initialize a collection of role
    Bonjour,
    Comme j'ai mentionné dans le titre j'ai eu cette erreur par ce que j'ai essayé d'afficher la liste des tests avec leurs questions je pense que c'est une erreur de Mapping.
    Classe question
    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
    package tn.esen.entities;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
     
    import javax.persistence.ManyToMany;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
     
    import java.io.Serializable;
    import java.util.Collection;
    import java.util.Date;
    @Entity
    public class Question implements Serializable {
    	@Override
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result + ((contenu == null) ? 0 : contenu.hashCode());
    		result = prime * result + ((dateCreation == null) ? 0 : dateCreation.hashCode());
    		result = prime * result + ((niveauDeDifficulte == null) ? 0 : niveauDeDifficulte.hashCode());
    		return result;
    	}
     
    	@Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (getClass() != obj.getClass())
    			return false;
    		Question other = (Question) obj;
    		if (contenu == null) {
    			if (other.contenu != null)
    				return false;
    		} else if (!contenu.equals(other.contenu))
    			return false;
    		if (dateCreation == null) {
    			if (other.dateCreation != null)
    				return false;
    		} else if (!dateCreation.equals(other.dateCreation))
    			return false;
    		if (niveauDeDifficulte == null) {
    			if (other.niveauDeDifficulte != null)
    				return false;
    		} else if (!niveauDeDifficulte.equals(other.niveauDeDifficulte))
    			return false;
    		return true;
    	}
     
    	/**
             * 
             */
    	private static final long serialVersionUID = 1L;
    	@Id
    	@GeneratedValue(strategy=GenerationType.IDENTITY)
    	@Column(name="id_question")		
    	private int id;
        private String contenu;    
    	private String niveauDeDifficulte;
            private Date dateCreation;
            @OneToMany(mappedBy="question",fetch = FetchType.EAGER)
            private Collection <Reponse> reponses; 
            @ManyToOne
             private Categorie categorie;
            @ManyToOne
             private Administrateur administrateur;
            @ManyToMany(mappedBy = "questions",fetch = FetchType.EAGER)
            private Collection<Test> tests;
    public Question(){
    	super();
    }
     
    @Override
    public String toString() {
    	return "Question [id=" + id + ", contenu=" + contenu + ", niveauDeDifficulte=" + niveauDeDifficulte + "]";
    }
     
    public int getId() {
    	return id;
    }
     
    public void setId(int id) {
    	this.id = id;
    }
     
     
    public String getContenu() {
    	return contenu;
    }
     
    public void setContenu(String contenu) {
    	this.contenu = contenu;
    }
     
    public String getNiveauDeDifficulte() {
    	return niveauDeDifficulte;
    }
     
    public void setNiveauDeDifficulte(String niveauDeDifficulte) {
    	this.niveauDeDifficulte = niveauDeDifficulte;
    }
     
     
    public Date getDateCreation() {
    	return dateCreation;
    }
     
    public void setDateCreation(Date dateCreation) {
    	this.dateCreation = dateCreation;
    }
     
     
    public Collection<Reponse> getReponses() {
    	return reponses;
    }
     
    public void setReponses(Collection<Reponse> reponses) {
    	this.reponses = reponses;
    }
     
    public Categorie getCategorie() {
    	return categorie;
    }
     
    public void setCategorie(Categorie categorie) {
    	this.categorie = categorie;
    }
     
    public Administrateur getAdministrateur() {
    	return administrateur;
    }
     
    public void setAdministrateur(Administrateur administrateur) {
    	this.administrateur = administrateur;
    }
     
    public Collection<Test> getTest() {
    	return tests;
    }
     
    public void setTest(Collection<Test> tests) {
    	this.tests = tests;
    }
     
    public Question(String contenu, String niveauDeDifficulte, Date dateCreation) {
    	super();
    	this.contenu = contenu;
    	this.niveauDeDifficulte = niveauDeDifficulte;
    	this.dateCreation = dateCreation;
    }
     
     
     
     
     
     
    }
    classe test
    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
    package tn.esen.entities;
     
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
     
    import javax.persistence.ManyToMany;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
     
    import java.io.Serializable;
    import java.util.Collection;
     
    @Entity
    public class Test implements Serializable {
    	/**
             * 
             */
    	private static final long serialVersionUID = 1L;
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	private int id;
    	private String duree;
    	private String typeDePreparation;
    	private String lieu;
    	private int nbrQuestionFacile;
    	private int nbrQuestionMoyen;
    	private int nbrQuestionDifficle;
    	@OneToMany(mappedBy = "test")
    	private Collection<Resultat> resultats;
    	@ManyToMany
    	private Collection<Question> questions;
     
    	@ManyToOne
    	private ResponsableTechnique responsableTechnique;
     
    	public Test() {
    		super();
    	}
     
    	public int getId() {
    		return id;
    	}
     
    	public void setId(int id) {
    		this.id = id;
    	}
     
     
     
    	public String getDuree() {
    		return duree;
    	}
     
    	public void setDuree(String duree) {
    		this.duree = duree;
    	}
     
    	public Collection<Question> getQuestions() {
    		return questions;
    	}
     
    	public void setQuestions(Collection<Question> questions) {
    		this.questions = questions;
    	}
     
    	public Test(String duree, String typeDePreparation, String lieu, int nbrQuestionFacile, int nbrQuestionMoyen,
    			int nbrQuestionDifficle) {
    		super();
    		this.duree = duree;
    		this.typeDePreparation = typeDePreparation;
    		this.lieu = lieu;
    		this.nbrQuestionFacile = nbrQuestionFacile;
    		this.nbrQuestionMoyen = nbrQuestionMoyen;
    		this.nbrQuestionDifficle = nbrQuestionDifficle;
    	}
     
    	public ResponsableTechnique getRésponsableTechnique() {
    		return responsableTechnique;
    	}
     
    	public void setRésponsableTechnique(ResponsableTechnique résponsableTechnique) {
    		this.responsableTechnique = résponsableTechnique;
    	}
     
    	public String getTypeDePreparation() {
    		return typeDePreparation;
    	}
     
    	public void setTypeDePreparation(String typeDePreparation) {
    		this.typeDePreparation = typeDePreparation;
    	}
     
    	public String getLieu() {
    		return lieu;
    	}
     
    	public void setLieu(String lieu) {
    		this.lieu = lieu;
    	}
     
    	public int getNbrQuestionFacile() {
    		return nbrQuestionFacile;
    	}
     
    	public void setNbrQuestionFacile(int nbrQuestionFacile) {
    		this.nbrQuestionFacile = nbrQuestionFacile;
    	}
     
    	public int getNbrQuestionMoyen() {
    		return nbrQuestionMoyen;
    	}
     
    	public void setNbrQuestionMoyen(int nbrQuestionMoyen) {
    		this.nbrQuestionMoyen = nbrQuestionMoyen;
    	}
     
    	public int getNbrQuestionDifficle() {
    		return nbrQuestionDifficle;
    	}
     
    	public void setNbrQuestionDifficle(int nbrQuestionDifficle) {
    		this.nbrQuestionDifficle = nbrQuestionDifficle;
    	}
     
    	public Collection<Resultat> getResultats() {
    		return resultats;
    	}
     
    	public void setRésultats(Collection<Resultat> resultats) {
    		this.resultats = resultats;
    	}
     
    }
    et voilà l’erreur
    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
    mai 11, 2016 2:57:08 PM org.xnio.Xnio <clinit>
    INFO: XNIO version 3.3.1.Final
    mai 11, 2016 2:57:14 PM org.xnio.nio.NioXnio <clinit>
    INFO: XNIO NIO Implementation Version 3.3.1.Final
    mai 11, 2016 2:57:15 PM org.jboss.remoting3.EndpointImpl <clinit>
    INFO: JBoss Remoting version 4.0.9.Final
    mai 11, 2016 2:57:20 PM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
    INFO: EJBCLIENT000017: Received server version 2 and marshalling strategies [river]
    mai 11, 2016 2:57:21 PM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
    INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@4a0ef907, receiver=Remoting connection EJB receiver [connection=Remoting connection <5d15fce5>,channel=jboss.ejb,nodename=daly-pc]} on channel Channel ID c53cfcba (outbound) of Remoting connection 3584f732 to localhost/127.0.0.1:8383
    mai 11, 2016 2:57:22 PM org.jboss.ejb.client.EJBClient <clinit>
    INFO: JBoss EJB Client version 2.1.1.Final
    Exception in thread "AWT-EventQueue-0" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: tn.esen.entities.Test.questions, could not initialize proxy - no Session
    	at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575)
    	at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214)
    	at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
    	at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142)
    	at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:526)
    	at javax.swing.table.DefaultTableCellRenderer.setValue(Unknown Source)
    	at javax.swing.table.DefaultTableCellRenderer.getTableCellRendererComponent(Unknown Source)
    	at javax.swing.JTable.prepareRenderer(Unknown Source)
    	at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
    	at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
    	at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
    	at javax.swing.plaf.ComponentUI.update(Unknown Source)
    	at javax.swing.JComponent.paintComponent(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JViewport.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JViewport.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JLayeredPane.paint(Unknown Source)
    	at javax.swing.JComponent.paintChildren(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    	at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
    	at javax.swing.RepaintManager.paint(Unknown Source)
    	at javax.swing.JComponent._paintImmediately(Unknown Source)
    	at javax.swing.JComponent.paintImmediately(Unknown Source)
    	at javax.swing.RepaintManager$4.run(Unknown Source)
    	at javax.swing.RepaintManager$4.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.access$1200(Unknown Source)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    	at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$500(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

  2. #2
    Expert confirmé
    Homme Profil pro
    Inscrit en
    Septembre 2006
    Messages
    2 936
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations forums :
    Inscription : Septembre 2006
    Messages : 2 936
    Points : 4 356
    Points
    4 356
    Par défaut
    Citation Envoyé par dalitobs Voir le message
    Bonjour,
    Comme j'ai mentionné dans le titre j'ai eu cette erreur par ce que j'ai essayé d'afficher la liste des tests avec leurs questions je pense que c'est une erreur de Mapping.
    et voilà l’erreur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Exception in thread "AWT-EventQueue-0" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: tn.esen.entities.Test.questions, could not initialize proxy - no Session
        at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575)
    LazyInitializationException == vous essayez de charger des données en dehors d'un contexte transactionnel ou d'une Session Hibernate.

    Par ailleurs déclarer vos @Entity implements Serializable alors que des champs sont des interfaces (Collection) ne sert à rien : le contrat n'est pas respecté.
    Et de toute façon avec un ORM les collections seront des proxies qui sont rarement serializable.

Discussions similaires

  1. Réponses: 2
    Dernier message: 18/01/2015, 19h05
  2. Réponses: 10
    Dernier message: 07/03/2014, 16h18
  3. Réponses: 5
    Dernier message: 23/10/2009, 14h02
  4. failed to lazily initialize a collection of role ?
    Par zuzuu dans le forum Hibernate
    Réponses: 3
    Dernier message: 25/02/2009, 16h02
  5. Réponses: 1
    Dernier message: 04/04/2007, 09h17

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