Mapping NHibernate probleme <composite-id>

Mapping NHibernate probleme <composite-id> - C#/.NET managed - Programmation

Marsh Posté le 18-04-2008 à 17:10:03    

Bonjour,
je suis en train d'effectuer le mapping de ma base de données avec le framework NHibernate sous Visual Studio 2008.
Je posséde une table "Contrat" qui est associé à une table "Client" et une table "Interimaire".  
La table "Contrat" possède donc une clef primaire composé de :  
          - numContrat
          - numClient
          - matricule (interimaire)
 
mon fichier de mapping est le suivant :  
 


<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <!--Built with MyGeneration/Template/NHibernate (c) by OHM (alvy77@hotmail.com)
based on NHibernate lujan99 0.9.20 (c) by lujan99@usa.net-->
  <class name="adecco.Entities.Contrat,adecco" table="contrat" lazy="false">
 
 
    <composite-id name="Id" class="adecco.Entities.ContratId,adecco">
      <key-property name="NumContrat" column="num_contrat" type="int" />
      <key-property name="ClientNumClient" column="client_num_client" type="int" />
      <key-property name="InterimaireMatricule" column="interimaire_matricule" type="int" />
    </composite-id>
 
    <property type="int" not-null="true" name="TypeAvenantId" column="[type_avenant_id]" />
    <property type="int" not-null="true" name="QualificationId" column="[qualification_id]" />
    <property type="int" not-null="true" name="StatutContratId" column="[statut_contrat_id]" />
    <property type="DateTime" not-null="true" name="DateAvenant" column="[date_avenant]" />
    <property type="bool" not-null="true" name="TacheRisque" column="[tache_risque]" />
    <property type="bool" not-null="true" name="Intemperie" column="[intemperie]" />
    <property type="bool" not-null="true" name="Risque" column="[risque]" />
    <property type="bool" not-null="true" name="SurveillanceMedical" column="[surveillance_medical]" />
 
  </class>
</hibernate-mapping>


 
Je vais maintenant vous présenter la classe Entity "ContratID.cs" :  
 

Code :
  1. /*
  2. using MyGeneration/Template/NHibernate (c) by OHM (alvy77@hotmail.com)
  3. based on NHibernate lujan99 0.9.20 (c) by lujan99@usa.net
  4. */
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. namespace adecco.Entities
  9. {
  10. /// <summary>
  11. /// IContratId interface for NHibernate mapped table 'contrat'.
  12. /// </summary>
  13. public interface IContratId
  14. {
  15.  #region Public Properties
  16.  int NumContrat
  17.  {
  18.   get ;
  19.   set ;
  20.  }
  21.  int ClientNumClient
  22.  {
  23.   get ;
  24.   set ;
  25.  }
  26.  int InterimaireMatricule
  27.  {
  28.   get ;
  29.   set ;
  30.  }
  31.  bool IsDeleted { get; set; }
  32.  bool IsChanged { get; set; }
  33.  #endregion
  34. }
  35. /// <summary>
  36. /// ContratId object for NHibernate mapped table 'contrat'.
  37. /// </summary>
  38. [Serializable]
  39. public class ContratId : IContratId
  40. {
  41.  #region Member Variables
  42.  protected int numcontrat;
  43.  protected int clientnumclient;
  44.  protected int interimairematricule;
  45.  protected bool _bIsDeleted;
  46.  protected bool _bIsChanged;
  47.  #endregion
  48.  #region Constructors
  49.  public ContratId() {}
  50.  public ContratId(int pClientNumClient, int pInterimaireMatricule)
  51.  {
  52.   this.clientnumclient = pClientNumClient;
  53.   this.interimairematricule = pInterimaireMatricule;
  54.  }
  55.  #endregion
  56.  #region Public Properties
  57.  public int NumContrat
  58.  {
  59.   get { return numcontrat; }
  60.   set { _bIsChanged |= (numcontrat != value); numcontrat = value; }
  61.  }
  62.  public int ClientNumClient
  63.  {
  64.   get { return clientnumclient; }
  65.   set { _bIsChanged |= (clientnumclient != value); clientnumclient = value; }
  66.  }
  67.  public int InterimaireMatricule
  68.  {
  69.   get { return interimairematricule; }
  70.   set { _bIsChanged |= (interimairematricule != value); interimairematricule = value; }
  71.  }
  72.  public bool IsDeleted
  73.  {
  74.   get
  75.   {
  76.    return _bIsDeleted;
  77.   }
  78.   set
  79.   {
  80.    _bIsDeleted = value;
  81.   }
  82.  }
  83.  public bool IsChanged
  84.  {
  85.   get
  86.   {
  87.    return _bIsChanged;
  88.   }
  89.   set
  90.   {
  91.    _bIsChanged = value;
  92.   }
  93.  }
  94.  #endregion
  95. }
  96. #region Custom ICollection interface for ContratId
  97. public interface IContratIdCollection : ICollection
  98. {
  99.  ContratId this[int index]{ get; set; }
  100.  void Add(ContratId pContratId);
  101.  void Clear();
  102. }
  103. [Serializable]
  104. public class ContratIdCollection : IContratIdCollection
  105. {
  106.  private IList<ContratId> _arrayInternal;
  107.  public ContratIdCollection()
  108.  {
  109.   _arrayInternal = new List<ContratId>();
  110.  }
  111.  public ContratIdCollection( IList<ContratId> pSource )
  112.  {
  113.   _arrayInternal = pSource;
  114.   if(_arrayInternal == null)
  115.   {
  116.    _arrayInternal = new List<ContratId>();
  117.   }
  118.  }
  119.  public ContratId this[int index]
  120.  {
  121.   get
  122.   {
  123.    return _arrayInternal[index];
  124.   }
  125.   set
  126.   {
  127.    _arrayInternal[index] = value;
  128.   }
  129.  }
  130.  public int Count { get { return _arrayInternal.Count; } }
  131.  public bool IsSynchronized { get { return false; } }
  132.  public object SyncRoot { get { return _arrayInternal; } }
  133.  public void CopyTo(Array array, int index){ _arrayInternal.CopyTo((ContratId[])array, index); }
  134.  public IEnumerator GetEnumerator() { return _arrayInternal.GetEnumerator(); }
  135.  public void Add(ContratId pContratId) { _arrayInternal.Add(pContratId); }
  136.  public void Clear() { _arrayInternal.Clear(); }
  137.  public IList<ContratId> GetList() { return _arrayInternal; }
  138.  }
  139. #endregion
  140. }


 
et bien sur la classe Entity "contrat.cs" :  
 

Code :
  1. /*
  2. using MyGeneration/Template/NHibernate (c) by OHM (alvy77@hotmail.com)
  3. based on NHibernate lujan99 0.9.20 (c) by lujan99@usa.net
  4. */
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using adecco.Entities;
  9. namespace adecco.Entities
  10. {
  11.     /// <summary>
  12.     /// IContrat interface for NHibernate mapped table 'contrat'.
  13.     /// </summary>
  14.     public interface IContrat
  15.     {
  16.         #region Public Properties
  17.         ContratId Id
  18.         {
  19.             get;
  20.             set;
  21.         }
  22.         int TypeAvenantId
  23.         {
  24.             get;
  25.             set;
  26.         }
  27.         int QualificationId
  28.         {
  29.             get;
  30.             set;
  31.         }
  32.         int StatutContratId
  33.         {
  34.             get;
  35.             set;
  36.         }
  37.         DateTime DateAvenant
  38.         {
  39.             get;
  40.             set;
  41.         }
  42.         bool TacheRisque
  43.         {
  44.             get;
  45.             set;
  46.         }
  47.         bool Intemperie
  48.         {
  49.             get;
  50.             set;
  51.         }
  52.         bool Risque
  53.         {
  54.             get;
  55.             set;
  56.         }
  57.         bool SurveillanceMedical
  58.         {
  59.             get;
  60.             set;
  61.         }
  62.         bool IsDeleted { get; set; }
  63.         bool IsChanged { get; set; }
  64.         #endregion
  65.     }
  66.     /// <summary>
  67.     /// Contrat object for NHibernate mapped table 'contrat'.
  68.     /// </summary>
  69.     [Serializable]
  70.     public class Contrat : IContrat
  71.     {
  72.         #region Member Variables
  73.         protected ContratId id;
  74.         protected int typeavenantid;
  75.         protected int qualificationid;
  76.         protected int statutcontratid;
  77.         protected DateTime dateavenant;
  78.         protected bool tacherisque;
  79.         protected bool intemperie;
  80.         protected bool risque;
  81.         protected bool surveillancemedical;
  82.         protected bool _bIsDeleted;
  83.         protected bool _bIsChanged;
  84.         #endregion
  85.         #region Constructors
  86.         public Contrat() { }
  87.         public Contrat(ContratId pId, int pTypeAvenantId, int pQualificationId, int pStatutContratId, DateTime pDateAvenant, bool pTacheRisque, bool pIntemperie, bool pRisque, bool pSurveillanceMedical)
  88.         {
  89.             this.id = pId;
  90.             this.typeavenantid = pTypeAvenantId;
  91.             this.qualificationid = pQualificationId;
  92.             this.statutcontratid = pStatutContratId;
  93.             this.dateavenant = pDateAvenant;
  94.             this.tacherisque = pTacheRisque;
  95.             this.intemperie = pIntemperie;
  96.             this.risque = pRisque;
  97.             this.surveillancemedical = pSurveillanceMedical;
  98.         }
  99.         #endregion
  100.         #region Public Properties
  101.         public ContratId Id
  102.         {
  103.             get { return id; }
  104.             set { _bIsChanged |= (id != value); id = value; }
  105.         }
  106.         public int TypeAvenantId
  107.         {
  108.             get { return typeavenantid; }
  109.             set { _bIsChanged |= (typeavenantid != value); typeavenantid = value; }
  110.         }
  111.         public int QualificationId
  112.         {
  113.             get { return qualificationid; }
  114.             set { _bIsChanged |= (qualificationid != value); qualificationid = value; }
  115.         }
  116.         public int StatutContratId
  117.         {
  118.             get { return statutcontratid; }
  119.             set { _bIsChanged |= (statutcontratid != value); statutcontratid = value; }
  120.         }
  121.         public DateTime DateAvenant
  122.         {
  123.             get { return dateavenant; }
  124.             set { _bIsChanged |= (dateavenant != value); dateavenant = value; }
  125.         }
  126.         public bool TacheRisque
  127.         {
  128.             get { return tacherisque; }
  129.             set { _bIsChanged |= (tacherisque != value); tacherisque = value; }
  130.         }
  131.         public bool Intemperie
  132.         {
  133.             get { return intemperie; }
  134.             set { _bIsChanged |= (intemperie != value); intemperie = value; }
  135.         }
  136.         public bool Risque
  137.         {
  138.             get { return risque; }
  139.             set { _bIsChanged |= (risque != value); risque = value; }
  140.         }
  141.         public bool SurveillanceMedical
  142.         {
  143.             get { return surveillancemedical; }
  144.             set { _bIsChanged |= (surveillancemedical != value); surveillancemedical = value; }
  145.         }
  146.         public bool IsDeleted
  147.         {
  148.             get
  149.             {
  150.                 return _bIsDeleted;
  151.             }
  152.             set
  153.             {
  154.                 _bIsDeleted = value;
  155.             }
  156.         }
  157.         public bool IsChanged
  158.         {
  159.             get
  160.             {
  161.                 return _bIsChanged;
  162.             }
  163.             set
  164.             {
  165.                 _bIsChanged = value;
  166.             }
  167.         }
  168.         #endregion
  169.     }
  170.     #region Custom ICollection interface for Contrat
  171.     public interface IContratCollection : ICollection
  172.     {
  173.         Contrat this[int index] { get; set; }
  174.         void Add(Contrat pContrat);
  175.         void Clear();
  176.     }
  177.     [Serializable]
  178.     public class ContratCollection : IContratCollection
  179.     {
  180.         private IList<Contrat> _arrayInternal;
  181.         public ContratCollection()
  182.         {
  183.             _arrayInternal = new List<Contrat>();
  184.         }
  185.         public ContratCollection(IList<Contrat> pSource)
  186.         {
  187.             _arrayInternal = pSource;
  188.             if (_arrayInternal == null)
  189.             {
  190.                 _arrayInternal = new List<Contrat>();
  191.             }
  192.         }
  193.         public Contrat this[int index]
  194.         {
  195.             get
  196.             {
  197.                 return _arrayInternal[index];
  198.             }
  199.             set
  200.             {
  201.                 _arrayInternal[index] = value;
  202.             }
  203.         }
  204.         public int Count { get { return _arrayInternal.Count; } }
  205.         public bool IsSynchronized { get { return false; } }
  206.         public object SyncRoot { get { return _arrayInternal; } }
  207.         public void CopyTo(Array array, int index) { _arrayInternal.CopyTo((Contrat[])array, index); }
  208.         public IEnumerator GetEnumerator() { return _arrayInternal.GetEnumerator(); }
  209.         public void Add(Contrat pContrat) { _arrayInternal.Add(pContrat); }
  210.         public void Clear() { _arrayInternal.Clear(); }
  211.         public IList<Contrat> GetList() { return _arrayInternal; }
  212.     }
  213.     #endregion
  214. }


 
Lorsque que je compile via mon "Main" : j'obtient l'erreur suivante qui apparemment et normal :  
 


L'application démarre...
 
System.TypeInitializationException: Une exception a été levée par l'initialiseur
 de type pour 'adecco.NHibernateHelper'. ---> NHibernate.MappingException: Could
 not compile the mapping document: adecco.Mapping.Contrat.hbm.xml ---> NHibernat
e.MappingException: composite-id class must override Equals(): adecco.Entities.C
ontratId
   à NHibernate.Cfg.HbmBinder.BindRootClass(XmlNode node, RootClass model, Mappi
ngs mappings)
   à NHibernate.Cfg.HbmBinder.BindRoot(XmlDocument doc, Mappings mappings)
   à NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
   --- Fin de la trace de la pile d'exception interne ---
   à NHibernate.Cfg.Configuration.LogAndThrow(MappingException me)
   à NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
   à NHibernate.Cfg.Configuration.ProcessMappingsQueue()
   à NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument docum
ent)
   à NHibernate.Cfg.Configuration.AddXmlReader(XmlTextReader hbmReader, String n
ame)
   à NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String n
ame)
   à NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly)
   à NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly)
   à NHibernate.Cfg.Configuration.AddAssembly(String assemblyName)
   à NHibernate.Cfg.Configuration.DoConfigure(XmlDocument doc)
   à NHibernate.Cfg.Configuration.Configure(XmlTextReader reader)
   à NHibernate.Cfg.Configuration.Configure(String fileName)
   à NHibernate.Cfg.Configuration.Configure()
   à adecco.NHibernateHelper..cctor() dans D:\Mes Documents\Visual Studio 2008\P
rojects\adecco\adecco\NHibernateHelper.cs:ligne 22
   --- Fin de la trace de la pile d'exception interne ---
   à adecco.NHibernateHelper.GetCurrentSession()
   à adecco.Manager.TypeAvenantManager..ctor() dans D:\Mes Documents\Visual Stud
io 2008\Projects\adecco\adecco\Manager\TypeAvenantManager.cs:ligne 20
   à adecco.Program.Main(String[] args) dans D:\Mes Documents\Visual Studio 2008
\Projects\adecco\adecco\Program.cs:ligne 22
 
L'application est fermée!


 
La ligne que je retient est : composite-id class must override Equals(): adecco.Entities.C
ontratId
Cependant, je ne comprend pas où est ce que je dois implémenter la méthode Equals ?  
 
Est ce que qu'elqu'un pourrait m'aider ?  
SVP, c la galère ...
Cordialement,
Olive

Reply

Marsh Posté le 18-04-2008 à 17:10:03   

Reply

Marsh Posté le 21-04-2011 à 15:29:48    

:hello:
 
Le temps a passé et tu as sans doute réglé ce problème.  
Je répond donc pour les lecteurs suivants éventuels.
 
Il faut implémenté Equals et GetHashCode dans la classe qui map la clé composite  : dans ton cas adecco.Entities.ContratId.
Ces méthodes existe déjà (hérité de object) elles doivent donc être override.
 
(N)Hibernate à besoin d'un hash code (GetHashCode) pour ordonner les entities et il doit aussi pouvoir comparer ce qu'il fait avec Equals.
Comme c'est un objet complexe (composé de plusieurs objets simples), la comparaison doit être faite explicitement.
 
Voilou
 
@+

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed