-->
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.  [ 2 posts ] 
Author Message
 Post subject: Lazy-loading avec hibernate3, annotation et Spring
PostPosted: Thu Nov 17, 2005 11:03 am 
Newbie

Joined: Thu Nov 17, 2005 10:41 am
Posts: 1
Location: Lyon
Bonjour,

je suis en train de tester Hibernate3 avec les annotation en intégrant le tout à Spring ...
Or lors lors du chargement d'un objet, tout le graph est chargé et non juste l'entité demandée.

Voici le fichier de mapping :
Code:
package com.itnsa.gestion.hibernate;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratorType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
* Client generated by hbm2java
*/
@Entity
@Table(name = "F_P_CLIENT")
public class Client implements java.io.Serializable {

   // Fields
   private Long ident;
   private Rib ribPrincipal;
   private Personne personne;
   private java.util.Set<com.itnsa.gestion.hibernate.ClientZoneLibre> zonesLibres;

   // Constructors

   /** default constructor */
   public Client() {
   }

   /** constructor with id */
   public Client(Long cliIdent) {
      this.ident = cliIdent;
   }

   // Property accessors

   /**
    *
    */
   @Id(generate = GeneratorType.AUTO)
   @Column(name = "CLI_IDENT")
   public Long getIdent() {
      return this.ident;
   }

   public void setIdent(Long cliIdent) {
      this.ident = cliIdent;
   }

   /**
    *
    */
   @ManyToOne
   @JoinColumn(name = "CLI_BPR_IDENT")
   public Rib getRibPrincipal() {
      return this.ribPrincipal;
   }

   public void setRibPrincipal(Rib PRib) {
      this.ribPrincipal = PRib;
   }

   /**
    *
    */
   @ManyToOne
   @JoinColumn(name = "CLI_BPP_IDENT")
   public Personne getPersonne() {
      return this.personne;
   }

   public void setPersonne(Personne PPersonne) {
      this.personne = PPersonne;
   }

   /**
    *
    */
   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   @JoinColumn(name = "CLZL_CLI_IDENT")
   public java.util.Set<com.itnsa.gestion.hibernate.ClientZoneLibre> getZonesLibres() {
      return this.zonesLibres;
   }

   public void setZonesLibres(
         java.util.Set<com.itnsa.gestion.hibernate.ClientZoneLibre> FPClientZls) {
      this.zonesLibres = FPClientZls;
   }

}


Le chargement est effectué de la manière suivante dans un DAO qui hérite de HibernateDaoSupport :

Code:
public Collection findClientByNomAppel(String nomAppel) {
   return getHibernateTemplate().find("from Client as client left join fetch client.personne where client.personne.nomAppel like 'TEST%'");
}


L'appel au DAO se fait dans une methode de service :

Code:
public void rechercheClient(String nomAppel) {
   Iterator iter = this.clientDAO.findClientByNomAppel(nomAppel).iterator();
   while (iter.hasNext()) {
      Client client = (Client) iter.next();
      System.out.println(client.getPersonne().getNomAppel());
   }
}


Or quand je regarde les requêtes générées par hibernate (je m'attends à n'en avoir qu'une seule initialisant les clients et leurs personne), j'obtiens ceci :

Code:
Hibernate: select client0_. ..... personne0_. ...
Hibernate: select rib0_. ....
Hibernate: select rib0_. ....
Hibernate: select zoneslibre0_. ...
Hibernate: select zoneslibre0_. ...


J'ai du laisser passer quelque chose sur le fonctionnement du lazy-loading ... je ne comprends pas !


Merci de m'ouvrir les yeux sur ma boulette ;-)
Marco.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 7:43 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
fetch=FetchType.LAZY sur le @ManyToOne et le @OneToMany

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.