我有這個功能NHibernate映射:NHibernate的 - 與期貨取
public LossMap()
{
Table("losses");
Id(x => x.Id).Column("id");
References(x => x.Policy).Column("pol_id");
HasMany(x => x.Statuses).KeyColumn("loss_id").Cascade.All().Inverse();
HasMany(x => x.Reserves).KeyColumn("loss_id").Cascade.All().Inverse();
HasMany(x => x.Payments).KeyColumn("loss_id").Cascade.All().Inverse();
}
public LossPaymentMap()
{
Table("losspayments");
Id(x => x.Id).Column("id");
Map(x => x.Type).Column("type_id");
References(x => x.Reserve).Column("reserve_id");
}
public LossReserveMap()
{
Table("lossreserves");
Id(x => x.Id).Column("id");
Map(x => x.Type).Column("type_id");
Map(x => x.Status).Column("status_id");
References(x => x.ParentReserve).Column("parent_reserve_id");
}
public LossStatusMap()
{
Table("lossstatuses");
Id(x => x.Id).Column("id");
Map(x => x.Status).Column("status_id");
Map(x => x.ExpirationDate).Column("expirationdate");
References(x => x.Loss).Column("loss_id");
}
總結:
- 損失有很多支付,儲備和狀態
- 付款有一個儲備
我正在嘗試獲取損失和他們的付款和儲備(但不是狀態)與下列翅膀限制:
- 只能獲取損失至少有一個狀態 「status.Status不在(1,2,7)」。
- 僅取Loss.Payments其中 「loss.Payment.Type = 2和loss.Payment.Reserve.Status!= 4)」
- 僅取Loss.Reserves其中Reserve.Status!= 3
當我想取2間平行的關係,我必須使用multiqueries或期貨,以避免笛卡爾乘積(右?),因爲這裏說明:http://ayende.com/blog/4367/eagerly-loading-entity-associations-efficiently-with-nhibernate
我想出了這個查詢(在HQL):
int[] statuslist = new int[3] {1, 2, 7};
var losses =
session.CreateQuery(
"from Loss l left join fetch l.Payments as payment join l.Statuses as status where l.Policy.Product.Id = :tid1 " +
"and status.Status not in (:statuslist1) " +
"and payment.Type = 2 and payment.Reserve.Status != 4")
.SetParameter("tid1", productid)
.SetParameterList("statuslist1", statuslist)
.Future<Loss>();
session.CreateQuery(
"from Loss l left join fetch l.Reserves as reserve join l.Statuses as status where l.Policy.Product.Id = :tid2 " +
"and status.Status not in (:statuslist2) " +
"and reserve.Status != 3 ")
.SetParameter("tid2", productid)
.SetParameterList("statuslist2", statuslist)
.Future<Loss>();
var list = losses.ToList();
但是,執行此查詢時,出現錯誤:NHibernate.HibernateException:無法執行多個查詢[.. SQL查詢] ---> System.ArgumentException:值「System.Object []」不是類型「 Entities.Loss「,並且不能用於此泛型集合。
任何線索我在這裏做錯了什麼?
當我刪除的狀態約束,查詢工作:
var losses =
session.CreateQuery(
"from Loss l left join fetch l.Payments as payment where l.Policy.Product.Id = :tid1 " +
"and payment.Type = 2 and payment.Reserve.Status != 4")
.SetParameter("tid1", productid)
.Future<Loss>();
session.CreateQuery(
"from Loss l left join fetch l.Reserves as reserve where l.Policy.Product.Id = :tid2 " +
"and reserve.Status != 3 ")
.SetParameter("tid2", productid)
.Future<Loss>();
然而,結果不是我想要的(我需要約束)。
有什麼建議嗎?
哦,並且使用HQL不是「必須」,如果這可以使用Linq或QueryOver,我沒有問題。
謝謝!
有沒有辦法做同樣項目離子使用NHibernate的LINQ API? – 2012-04-05 16:27:31
我不使用LINQ所以我不確定,雖然這似乎是你想要的東西http://stackoverflow.com/questions/796889/nhibernate-linq-and-distinctrootentity –