using (var transaction = session.BeginTransaction())
{
var invoice = new Invoice {Issued = DateTime.Now};
session.Save(invoice);
transaction.Commit();
}
using (var session = OpenSession())
{
var invoices = session.CreateCriteria<Invoice>().List<Invoice>();
}
复制代码
实例二
在我们的项目中,错综复杂的关系经常需要Join查询和子查询,但是Join查询我们都在在映射文件中配置关系,如果没有配置关系NHibernate的Join查询就无能为力了,只有Select两张表,然后让两张表的Id相等。这好像是linq to sql的一贯做法。NHibernate在这点上强了很多,只要在映射文件中配置一下,小小查询什么都搞定了。
1.Domain
var det= s.CreateQuery(@"from Detail d left join d.Master m
where (select count(e) from d.Master.Elements e where e.Description='e1')>0")
.List<Detail>();
复制代码
参考资料
NHibernate Jira:Protected properties and public properties cannot have the same name with different case
NHibernate Jira:Join used together with subquery generates wrong SQL