Fluent Nhibernate - HQL select problem
I am new to NHibernate. I am using Fluent Nhibernate and when I run a simple query I get null results. Here is my configuration:
Fluent Configuration:
Fluently.Configure().Database(MsSqlConfiguration.MsSql2008
.ShowSql()
.ConnectionString(c => c
.FromConnectionStringWithKey("MY_DB")))
.Mappings(m =>m.FluentMappings.AddFromAssemblyOf<CsrDAL.Mappings.CsrRegistrationMap>())
.BuildSessionFactory();
E开发者_如何学运维ntity Class
public class Industry
{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual IList<CsrRegistration> ReferencedRegistrations { get; set; }
}
Map Class
public class IndustryMap : ClassMap<Industry>
{
public IndustryMap()
{
Table("industries");
Id(x => x.Id. "id");
Map(x => x.Name, "name");
}
}
Repository Fetch Method
public static IList<Industry> getData(CsrRegistration registration)
{
using (var session = CsrDalHelper.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
try
{
IQuery q = session.CreateQuery("from Industry ind where ind.Id=1");
IList<Industry> inds =
q.List<Industry>();
return inds; // <-- This is null
}
}
}
}
Thank you in advance! Dimitris
The problem was in session management, somewhere else in my code.
I was wrongly adding collection items to an entity outside the NH session.
As a newcomer to nhibernate, I am now studying the docs in more details.
Thank you
精彩评论