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 :

chargement retardé ==> LazyInitializationException


Sujet :

Hibernate Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Étudiant
    Inscrit en
    Avril 2009
    Messages
    171
    Détails du profil
    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2009
    Messages : 171
    Par défaut chargement retardé ==> LazyInitializationException
    Bonjour à tous,

    Je développe un intranet et j'utilise Hibernate.
    Le problème c'est que j'utilise pour la première fois le lasy loading et je n'arrive pas à le faire fonctionner.

    J'ai un Contact qui un objet client Client :
    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
     
    @Entity
    @Table(name = "contact", schema = "public")
    public class Contact implements java.io.Serializable {
     
    	private static final long serialVersionUID = 922930571126812834L;
    	private int nocontact;
    	private Clients clients;
    	private String nom;
    	private String prenom;
    	private String adresse;
    	private String fonction;
    	private String service;
    	private String telephones;
    	private String email1;
    	private String email2;
    	private String email3;
     
    	public Contact() {
    	}
     
    	public Contact(int nocontact, Clients clients) {
    		this.nocontact = nocontact;
    		this.clients = clients;
    	}
     
    	public Contact(int nocontact, Clients clients, String nom, String prenom,
    			String adresse, String fonction, String service, String telephones,
    			String email1, String email2, String email3) {
    		this.nocontact = nocontact;
    		this.clients = clients;
    		this.nom = nom;
    		this.prenom = prenom;
    		this.adresse = adresse;
    		this.fonction = fonction;
    		this.service = service;
    		this.telephones = telephones;
    		this.email1 = email1;
    		this.email2 = email2;
    		this.email3 = email3;
    	}
     
    	@Id
    	@SequenceGenerator(sequenceName = "contact_nocontact_seq", name = "contact_nocontact_seq")
    	@GeneratedValue(strategy = GenerationType.AUTO, generator="contact_nocontact_seq")
    	@Column(name = "nocontact", unique = true, nullable = false)
    	public int getNocontact() {
    		return this.nocontact;
    	}
     
    	public void setNocontact(int nocontact) {
    		this.nocontact = nocontact;
    	}
     
    	@ManyToOne(fetch = FetchType.LAZY)
    	@JoinColumn(name = "identrepriseclient", nullable = false)
    	public Clients getClients() {
    		return this.clients;
    	}
     
    	public void setClients(Clients clients) {
    		this.clients = clients;
    	}
     
    	@Column(name = "nom", length = 32)
    	public String getNom() {
    		return this.nom;
    	}
     
    	public void setNom(String nom) {
    		this.nom = nom;
    	}
     
    	@Column(name = "prenom", length = 32)
    	public String getPrenom() {
    		return this.prenom;
    	}
     
    	public void setPrenom(String prenom) {
    		this.prenom = prenom;
    	}
     
    	@Column(name = "adresse", length = 256)
    	public String getAdresse() {
    		return this.adresse;
    	}
     
    	public void setAdresse(String adresse) {
    		this.adresse = adresse;
    	}
     
    	@Column(name = "fonction", length = 32)
    	public String getFonction() {
    		return this.fonction;
    	}
     
    	public void setFonction(String fonction) {
    		this.fonction = fonction;
    	}
     
    	@Column(name = "service", length = 32)
    	public String getService() {
    		return this.service;
    	}
     
    	public void setService(String service) {
    		this.service = service;
    	}
     
    	@Column(name = "telephones", length = 64)
    	public String getTelephones() {
    		return this.telephones;
    	}
     
    	public void setTelephones(String telephones) {
    		this.telephones = telephones;
    	}
     
    	@Column(name = "email1", length = 256)
    	public String getEmail1() {
    		return this.email1;
    	}
     
    	public void setEmail1(String email1) {
    		this.email1 = email1;
    	}
     
    	@Column(name = "email2", length = 256)
    	public String getEmail2() {
    		return this.email2;
    	}
     
    	public void setEmail2(String email2) {
    		this.email2 = email2;
    	}
     
    	@Column(name = "email3", length = 256)
    	public String getEmail3() {
    		return this.email3;
    	}
     
    	public void setEmail3(String email3) {
    		this.email3 = email3;
    	}
     
    	/**
             * teste l'égalité entre deux objet contact
             * @param contact le contact à comparer 
             */
    	@Override
    	public boolean equals(Object contact) {
            if ( (this == contact ) ){
            	return true;
            }
            if ( !(contact instanceof Contact) ){
            	return false;
            }
            Contact contactACompare = (Contact) contact;
    		return this.nom.equals(contactACompare.nom)
    				&& this.prenom.equals(contactACompare.prenom)
    				&& this.clients.equals(contactACompare.clients);
    	}
     
    	/**
             * 
             */
    	@Override
    	public int hashCode() {
    		return new HashCodeBuilder().append(
    				this.nom + this.prenom + this.clients.getNom()).toHashCode();
    	}
     
    	/**
             * compare deux objets dans le cas 'un tri. La comparation se fait sur le
             * nom, le prénom et le nom de la société du contact
             * 
             * @param contact
             *            le contact à comparer
             * @return
             */
    	public int compareTo(Object contact) {
    		Contact contactACompare = (Contact) contact;
    		String chaineContact = this.nom + this.prenom + this.clients.getNom();
    		String chaineContactAComparer = contactACompare.nom
    				+ contactACompare.prenom + contactACompare.clients.getNom();
    		return chaineContact.compareTo(chaineContactAComparer);
    	}
    }
    dans ma couche logique DAO, j'ai une méthode qui me retourne tous les contacts en BD. Je fais un traitement tout con, j'affaiche le nom, le prénom et le nom du client de tous les contacts de cette liste.

    SAUF QUE 'ai une exception quand je veux afficher le nom du client :
    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
     
    org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    	at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
    	at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
    	at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
    	at domain.model.Clients$$EnhancerByCGLIB$$fba890bb.getNom(<generated>)
    	at tests.service.ContactServiceTest.testGetContact(ContactServiceTest.java:63)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at junit.framework.TestCase.runTest(TestCase.java:168)
    	at junit.framework.TestCase.runBare(TestCase.java:134)
    	at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:79)
    	at junit.framework.TestResult$1.protect(TestResult.java:110)
    	at junit.framework.TestResult.runProtected(TestResult.java:128)
    	at junit.framework.TestResult.run(TestResult.java:113)
    	at junit.framework.TestCase.run(TestCase.java:124)
    	at junit.framework.TestSuite.runTest(TestSuite.java:232)
    	at junit.framework.TestSuite.run(TestSuite.java:227)
    	at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
    	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    Voilà le code qui liste tous mes contacts :
    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 ContactDaoImpl extends HibernateDaoSupport implements IContactDao {
     
    	public List<Contact> listerContacts() {
    		try {
    			// récupération de la session de l'utilisateur ou création d'une
    			// nouvelle
    			logger.info("récupération de la liste des contacts.");
    			Session session = getHibernateTemplate().getSessionFactory()
    					.getCurrentSession();
     
    			session.beginTransaction();
    			Query query = session.createQuery("from Contact order by nom");
    			List<Contact> tousLesContacts = query.list();
     
    			return tousLesContacts;
     
    			// TODO: clore la session et la flusher
    		} catch (DataAccessException e) {
    			logger.error("Exception - DataAccesException : " + e.getMessage()
    					+ " sur la méthode listerContacts");
    			return null;
    		}
    	}
    }
    Quelqu'un peut me dire mon erreur ?

    Nicolas

  2. #2
    Membre expérimenté Avatar de Shinzul
    Homme Profil pro
    Lecteur assidu de code source
    Inscrit en
    Janvier 2008
    Messages
    174
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Lecteur assidu de code source
    Secteur : Finance

    Informations forums :
    Inscription : Janvier 2008
    Messages : 174
    Par défaut
    Si j'ai bien compris ton erreur est quand tu chercher a afficher des attributs de l'objet Clients qui est contenu dans contact.

    Si c'est bien cela change le fetch du @ManyToOne de client en FetchType.EAGER dans Contact

    Le lazy informe qu'on ne doit pas charger et objet quand on recupère un Contact et donc quand tu cherche a le récupérer Hibernate génère une erreur informant que la session est fermé

Discussions similaires

  1. Application des css au chargement (retard)
    Par krunch dans le forum Mise en page CSS
    Réponses: 7
    Dernier message: 26/02/2015, 00h40
  2. Chargement fichier JS retardé
    Par Romalafrite dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 23/08/2007, 09h53
  3. action APRES chargement complet ...
    Par PinGuy dans le forum Delphi
    Réponses: 7
    Dernier message: 06/07/2006, 17h16
  4. [DLL] -> Retarder le chargement ?
    Par MaTHieU_ dans le forum C++Builder
    Réponses: 3
    Dernier message: 18/08/2003, 20h32
  5. DirectX 6, un peu en retard ... :\
    Par multani dans le forum DirectX
    Réponses: 3
    Dernier message: 28/05/2002, 19h19

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