views:

92

answers:

2

I'm trying to adopt Fluent NHibernate with my project, currently I can get data from database, when I'm at application server, data is include its PK but when I return this data (as List) to client all of its PK is loose.

How can I fixed this problem?

Update

My POCO class is below: PKs are CountryCd and CityCd

public class coCity
{
    public virtual string CountryCd { get; private set; }
    public virtual string CityCd { get; private set; }
    public virtual string CityNameTH { get; set; }
    public virtual string CityNameEN { get; set; }
    public virtual int DeliveryLeadTime { get; set; }
    public virtual string CreateBy { get; set; }
    public virtual DateTime CreateDate { get; set; }
    public virtual string UpdateBy { get; set; }
    public virtual DateTime UpdateDate { get; set; }

    public override bool Equals(object obj)
    {
        return this.GetHashCode().Equals(obj.GetHashCode());
    }

    public override int GetHashCode()
    {
        return (this.CountryCd + this.CityCd).GetHashCode();
    }
}

Mapping class:

public class coCityMap : ClassMap<coCity>
{
    public coCityMap()
    {
        Table("coCity"); // this is optional

        CompositeId()
            .KeyProperty(x => x.CountryCd)
            .KeyProperty(x => x.CityCd);
        Map(x => x.CityNameTH);
        Map(x => x.CityNameEN);
        Map(x => x.DeliveryLeadTime);
        Map(x => x.CreateBy);
        Map(x => x.CreateDate);
        Map(x => x.UpdateBy);
        Map(x => x.UpdateDate);
    }
}

Source code to get data at application server

public List<coCity> GetTest()
{
    List<coCity> result = new List<coCity>();

    var sessionFactory = CreateSessionFactory();

    using (var session = sessionFactory.OpenSession())
    {
        result = (List<coCity>)session.CreateCriteria(typeof(coCity)).List<coCity>();
    }

    return result;
}

When its still at application server data is retrieve correctly as image below alt text

However when this data transit back to client side all of its PKs is loose like below. alt text

+1  A: 

First of all, this isn't a problem with Fluent NHibernate so:

  1. Serializable must be used on your POCO's when you serialize them.
  2. (from your comment) NHibernate keeps a reference of the object retrieved from the database to a cache (1-st level cache). While you serialize this 'managed' object the output of the serialization is an unmanaged object. Nhibernate does not detect that a an object exists in the db just because you set an value in a newly constructed object. You must get the object from the database and update its properties and call Update() or you work with pure sql with the object that returned from the client (yikes!).

Note that is irrelevant with this question: your Equals() implementation is really bad as it doesn't take into account types and depends only on GetHashCode value. If all your classes have this implementation you could run into trouble.

Jaguar
A: 

I think the problem is with that private setter on the PK's properties. Try changing that to public.

Either way, mark your entity with Serializable

A few comments:

  • As a general recomendation when using nhibernate is to avoid composite Ids. Create on your model a surrogate Id that is an identity column and enforce uniqueness of CityCd and CountryCd somewhere else
  • When passing data around client/server tiers, consider using DTOs to avoid some commong LazyInitializationExceptions problems.
Pedro