| Hibernate version: 
NHibernate-1.2.0.Alpha1
 
 Mapping documents:
 <?xml version="1.0" encoding="utf-8" ?>
 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
 auto-import="false"
 default-access="nosetter.camelcase-underscore"
 default-lazy="true"
 assembly="AtivosONLINE.Entidades"
 namespace="AtivosONLINE.Entidades">
 
 <!-- AtivosONLINE.Entidades.Derivativos -->
 <class name="AtivosONLINE.Entidades.Derivativos.Derivativo"
 table="ATV_DerivativosLiquidacoes"
 lazy="true"
 discriminator-value="?">
 <cache usage="read-only" />
 <id name="Liquidacao" column="DataLiquidacao" unsaved-value="null">
 <generator class="assigned" />
 </id>
 <discriminator column="IdDerivativo" force="true" not-null="true" />
 <bag name="Vencimentos"
 table="ATV_DerivativosTaxas"
 lazy="true"
 inverse="true"
 order-by="DataVencimento desc">
 <cache usage="read-only" />
 <key column="DataLiquidacao" />
 <one-to-many class="AtivosONLINE.Entidades.Derivativos.Vencimento" />
 </bag>
 <subclass name="AtivosONLINE.Entidades.Derivativos.DI1Futuro" discriminator-value="1" />
 </class>
 
 <class name="AtivosONLINE.Entidades.Derivativos.Vencimento"
 table="ATV_DerivativosTaxas"
 lazy="true">
 <cache usage="read-only" />
 <composite-id>
 <key-many-to-one name="Derivativo"
 class="AtivosONLINE.Entidades.Derivativos.Derivativo"
 column="DataLiquidacao" />
 <key-property name="Data" column="DataVencimento" />
 </composite-id>
 <property name="Taxa" column="Taxa" not-null="true" />
 </class>
 </hibernate-mapping>
 
 Classes
 
 using System;
 using System.Collections.Generic;
 using System.Text;
 using NHibernate;
 using NHibernate.Expression;
 
 namespace AtivosONLINE.Entidades.Derivativos
 {
 public abstract class Derivativo
 {
 #region Privados
 
 private DateTime _liquidacao;
 private IList<Vencimento> _vencimentos;
 
 #endregion
 
 #region Públicos
 
 public DateTime Liquidacao {
 get { return _liquidacao; }
 set { _liquidacao = value; }
 }
 
 public IList<Vencimento> Vencimentos {
 get { return _vencimentos; }
 }
 
 #endregion
 
 #region Sobrecargas
 
 /// <returns>A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.</returns>
 public override string ToString() {
 return _liquidacao.ToShortDateString();
 }
 
 #endregion
 
 #region CRUD
 
 public void Load(DateTime liquidacao) {
 NHibernate.GetSession.Load(this, liquidacao);
 }
 
 public static IList<Derivativo> ListLiquidacoes<derivativo>()
 where derivativo : Derivativo {
 return NHibernate.GetSession.CreateCriteria(typeof(derivativo))
 .List<Derivativo>();
 }
 
 #endregion
 
 }
 
 }
 
 using System;
 using System.Collections.Generic;
 using System.Text;
 
 namespace AtivosONLINE.Entidades.Derivativos {
 public class Vencimento {
 
 #region Privados
 
 private DateTime _data;
 private Derivativo _derivativo;
 private double _taxa;
 
 #endregion
 
 #region Públicos
 
 public DateTime Data
 {
 get { return _data; }
 set { _data = value; }
 }
 
 public double Taxa {
 get { return _taxa; }
 set { _taxa = value; }
 }
 
 public Derivativo Derivativo {
 get { return _derivativo; }
 }
 
 #endregion
 
 #region Sobrecarregados
 
 /// <returns>A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.</returns>
 public override string ToString()
 {
 return string.Format("{0} : {1}", _data.ToShortDateString(), _taxa);
 }
 
 /// <param name="obj">The <see cref="T:System.Object"></see> to compare with the current <see cref="T:System.Object"></see>.</param>
 /// <returns>true if the specified <see cref="T:System.Object"></see> is equal to the current <see cref="T:System.Object"></see>; otherwise, false.</returns>
 public override bool Equals(object obj) {
 if (this == obj) return true;
 
 Vencimento ve = obj as Vencimento;
 if (ve == null) return false; // null or not Derivativo
 
 if (!_derivativo.Equals(ve.Derivativo)) return false;
 if (_data != ve.Data) return false;
 
 return true;
 
 }
 
 /// <returns>A hash code for the current <see cref="T:System.Object"></see>.</returns>
 public override int GetHashCode() {
 unchecked {
 int result = 17;
 result = 37 * result + _derivativo.Liquidacao.GetHashCode();
 result = 37 * result + _data.GetHashCode();
 return result;
 }
 }
 
 #endregion
 
 #region CRUD
 
 public void Load<derivativo>(DateTime liquidacao, DateTime vencimento)
 where derivativo : Derivativo, new() {
 derivativo de = new derivativo();
 de.Load(liquidacao);
 _derivativo = de;
 _data = vencimento;
 NHibernate.GetSession.Load(this, this);
 }
 
 //public void Load(Type obj, DateTime liquidacao, DateTime vencimento) {
 //    obj i = new obj();
 //    Derivativo de = new obj();
 //    de.Load(liquidacao);
 //    _derivativo = de;
 //    _data = vencimento;
 //    NHibernate.GetSession.Load(this, this);
 //}
 //public void Load(Derivativo derivativo, DateTime vencimento) {
 //    _derivativo = derivativo;
 //    _data = vencimento;
 //    NHibernate.GetSession.Load(this, this);
 //}
 
 #endregion
 }
 }
 
 Teste:
 
 using System;
 using System.Collections.Generic;
 using System.Text;
 using AtivosONLINE.Entidades.Derivativos;
 
 namespace Teste
 {
 class Program
 {
 static void Main(string[] args) {
 print(); // read DB
 print(); // read cache
 print(); // read cache
 }
 
 static void print()
 {
 DI1Futuro di = new DI1Futuro();
 di.Load(new DateTime(2006, 7, 14));
 Console.WriteLine(di.Liquidacao);
 Console.ReadKey();
 
 foreach (Vencimento ve in di.Vencimentos)
 {
 Console.WriteLine(string.Format("{0} : {1}", ve.Data.ToShortDateString(), ve.Taxa));
 }
 Console.ReadKey();
 }
 
 }
 }
 
 
 Output console:
 14/7/2006 00:00:00
 2/1/2017 : 15,4801310765165
 4/1/2016 : 15,5401732778472
 2/1/2015 : 15,6202282612012
 2/1/2014 : 15,7203161327787
 2/1/2013 : 15,6403167163039
 14/7/2006 00:00:00
 1/1/0001 : 0
 1/1/0001 : 0
 1/1/0001 : 0
 1/1/0001 : 0
 1/1/0001 : 0
 14/7/2006 00:00:00
 1/1/0001 : 0
 1/1/0001 : 0
 1/1/0001 : 0
 1/1/0001 : 0
 1/1/0001 : 0
 
 When the object with composite-id comes of cache it this emptiness!
 We knows which is the problem?
 I find that the problem this in the GetHashCode but already I tried of everything and not consigor to decide.
 
 Comment:
 translated for Babel Fish
 sorry my bad English.
 _________________
 Antonio Luis Milesi Bastos
 
 
 |