-->
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.  [ 3 posts ] 
Author Message
 Post subject: trouble loading many to many
PostPosted: Thu Feb 26, 2009 4:08 pm 
Newbie

Joined: Fri Sep 26, 2008 4:45 am
Posts: 17
Hi,

I can't figure out why once I'm been trying to load a many to many relation (the datas are correctly stored into the database) but when I access the collection the set is null

here is how I try to load the data

Code:
public Set<Personne> getBeneficaireEnfants() {
      DossierBeneficiaire dossierBeneficiaire = dossierBeneficiaireService.findByCriteriaMainBeneficiaire(dossier);
      if (dossierBeneficiaire != null) {
         try {
            LOG.debug("DOSSIER BENEFICIAIRE NOT NULL");
            LOG.debug("ENFANT SIZE : " + dossierBeneficiaire.getBeneficiairesEnfant().size());
            return dossierBeneficiaire.getBeneficiairesEnfant();
         }
         catch (Exception ex) {
            LOG.debug("Exception while attempting to retrieve enfant " + ex);
            return null;
         }
      }else {
         return null;
      }



dossier beneficiare is not null but ENFANT SIZE get 0

here is my entity

Code:
@Entity
public class DossierBeneficiaire implements Serializable{

   /** The serial version id. */
   private static final long serialVersionUID = 0;
   
   /** The dossier beneficiaire id. */
   @Id
   @Column(name="id")
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long id;
   
   /** The dossier id. */
   @ManyToOne(fetch = FetchType.EAGER, optional = true)
   @Cascade((org.hibernate.annotations.CascadeType.SAVE_UPDATE))
   private Dossier dossier;

   /** The date debut. */
   @Column(name="dateDebut", nullable=true)
   private Date dateDebut;

   /** The date fin. */
   @Column(name="dateFin", nullable=true)
   private Date dateFin;

   /** The beneficiaire adulte. */
   @ManyToMany(fetch = FetchType.EAGER)
   @Cascade((org.hibernate.annotations.CascadeType.SAVE_UPDATE))
    @JoinTable(name = "DossierBeneficiaire_BeneficiaireAdulte",
        joinColumns = @JoinColumn(name="dossierBeneficiaire_fk"),
        inverseJoinColumns = @JoinColumn(name="personne_fk")
    )      
   private Set<Beneficiaire> beneficiairesAdulte;

   /** The beneficiaire enfant . */
   @ManyToMany(fetch = FetchType.EAGER)
   @Cascade((org.hibernate.annotations.CascadeType.SAVE_UPDATE))
    @JoinTable(name = "DossierBeneficiaire_BeneficiaireEnfant",
        joinColumns = @JoinColumn(name="dossierBeneficiaire_fk"),
        inverseJoinColumns = @JoinColumn(name="personne_fk")
    )      
   private Set<Personne> beneficiairesEnfant;

   /** The beneficiaire tiers . */
   @ManyToMany(fetch = FetchType.EAGER)
   @Cascade((org.hibernate.annotations.CascadeType.SAVE_UPDATE))
    @JoinTable(name = "DossierBeneficiaire_BeneficiaireTiers",
        joinColumns = @JoinColumn(name="dossierBeneficiaire_fk"),
        inverseJoinColumns = @JoinColumn(name="personne_fk")
    )      
   private Set<Personne> beneficiairesTiers;

   /**
    * Constructor
    */
   public DossierBeneficiaire() {
      beneficiairesAdulte = new HashSet<Beneficiaire>();
      beneficiairesEnfant = new HashSet<Personne>();
      beneficiairesTiers = new HashSet<Personne>();
   }

   /**
    * Gets the BeneficiaireEnfants
    *
    * @return Set<Beneficiaire> returns the BeneficairesEnfant
    */
   public Set<Personne> getBeneficiairesEnfant() {
      return beneficiairesEnfant;
   }

   /**
    * Sets the BeneficiaireEnfants
    *
    * @param beneficiairesEnfant
    *          The set of beneficiaire enfants to set
    */
   public void setBeneficiairesEnfant(Set<Personne> beneficiairesEnfant) {
      this.beneficiairesEnfant = beneficiairesEnfant;
   }

   /**
    * Gets the BeneficaireTiers
    *
    * @return   Set<Beneficiaire> returns the set of beneficiaire
    *       
    */
   public Set<Personne> getBeneficiairesTiers() {
      return beneficiairesTiers;
   }

   /**
    * Sets the BeneficaireTiers
    *
    * @param beneficiairesTiers
    *          The beneficiaire tiers to set
    */
   public void setBeneficiairesTiers(Set<Personne> beneficiairesTiers) {
      this.beneficiairesTiers = beneficiairesTiers;
   }

   /**
    * Gets the id
    *
    * @return long returns the id
    */
   public long getId() {
      return id;
   }

   /**
    * Sets the id
    *
    * @param id
    *          The id to set
    */
   public void setId(long id) {
      this.id = id;
   }

   /**
    * Gets the Dossier
    *
    * @return Dossier returns the DossierId
    */
   public Dossier getDossier() {
      return dossier;
   }

   /**
    * Sets the Dossier
    *
    * @param dossier
    *          The Dossier to set
    */
   public void setDossier(Dossier dossier) {
      this.dossier = dossier;
   }

   /**
    * Gets the date debut
    *
    * @return Date returns the date debut
    */
   public Date getDateDebut() {
      return dateDebut;
   }

   /**
    * Sets the date debut
    *
    * @param dateDebut
    *          The date debut to set
    */
   public void setDateDebut(Date dateDebut) {
      this.dateDebut = dateDebut;
   }

   /**
    * Gets the date fin
    *
    * @return Date returns the date fin
    */
   public Date getDateFin() {
      return dateFin;
   }

   /**
    * Sets the date fin
    *
    * @param dateFin
    *          The date fin to set
    */
   public void setDateFin(Date dateFin) {
      this.dateFin = dateFin;
   }

   /**
    * Gets the Beneficiaire
    *
    * @return Beneficiaire returns the Beneficiaire
    */
   public Set<Beneficiaire> getBeneficiairesAdulte() {
      return beneficiairesAdulte;
   }

   /**
    * Sets the Beneficiaire
    *
    * @param beneficiaire
    *          The Benficiaire to set
    */
   public void setBeneficiairesAdulte(Set<Beneficiaire> beneficiairesAdulte) {
      this.beneficiairesAdulte = beneficiairesAdulte;
   }
   
   /**
    * Does our two object are equals ?
    *
    * @param obj
    *          The object to compare to
    * @return boolean returns true if the gived paramater is equal to our current object
    *
    */
   @Override
   public boolean equals(Object obj) {
      return EqualsBuilder.reflectionEquals(this, obj);
   }

   /**
    * Gets the hash code
    *
    * @return int return the hash code of the current adresse
    */
   @Override
   public int hashCode() {
      return HashCodeBuilder.reflectionHashCode(this);
   }

   /**
    * Make a String representation of the current adresse
    */
   @Override
   public String toString() {
      return ToStringBuilder.reflectionToString(this);
   }
}




Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2009 1:01 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Can you post the SQL which is generated by dossierBeneficiaireService.findByCriteriaMainBeneficiaire(dossier); and dossierBeneficiaire.getBeneficiairesEnfant()

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2009 4:11 am 
Newbie

Joined: Fri Sep 26, 2008 4:45 am
Posts: 17
here is the code inside the dao

Code:
/**
    * Find a main beneficiaire by using a gived Dossier
    *
    * @param  dossier
    *          The entity dossier
    * @return Beneficiaire returns a gived Beneficiaire
    */
   public DossierBeneficiaire findByCriteriaMainBeneficiaire(Dossier dossier) {
      long start = System.currentTimeMillis();
      LOG.debug("findByCriteria(dossier " + ReflectionToStringBuilder.toString(dossier) + ")");
      Session session = getSession();
      session.getSessionFactory().openSession();
      Criteria criteria = session.createCriteria(DossierBeneficiaire.class);
      Criteria manyToOne = criteria.createCriteria("dossier");
      if (dossier.getDossierId() != null) {
         LOG.debug("DOSSIER BENEFICIAIRE DOSSIER ID NOT NULL");
         manyToOne.add(org.hibernate.criterion.Expression.eq("dossierId", dossier.getDossierId()));
      }
      DossierBeneficiaire result = new DossierBeneficiaire();
      if (criteria.list().size() > 0) {
         LOG.debug("DOSSIER BENEFICIAIRE LIST > 0");
         result = (DossierBeneficiaire) criteria.list().get(0);
      }else {
         LOG.debug("DOSSIER BENEFICIAIRE LIST == 0");
      }
      session.close();
      return result;      
   }


this code work loading another many to many relation named beneficiairesAdulte :

Code:
   /** The beneficiaire adulte. */
   @ManyToMany(fetch = FetchType.EAGER)
   @Cascade((org.hibernate.annotations.CascadeType.SAVE_UPDATE))
    @JoinTable(name = "DossierBeneficiaire_BeneficiaireAdulte",
        joinColumns = @JoinColumn(name="dossierBeneficiaire_fk"),
        inverseJoinColumns = @JoinColumn(name="personne_fk")
    )      
   private Set<Beneficiaire> beneficiairesAdulte;


And the generated sql is a bit to complicated to be posted here


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