使用 Fluent NHibernate 连接表
Join on tables using Fluent NHibernate
我想使用 FN 在表之间进行连接。
但是我得到的输出不是很正确的结果。
我的代码:
public class Store
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
public virtual IList<Product> Products { get; protected set; }
public virtual IList<Employee> Staff { get; protected set; }
public virtual string FirstName { get; set; }
}
public StoreMap()
{
Table("store");
Id(store => store.Id).Column("id").GeneratedBy.Increment();
Map(store => store.Name).Column("name");
Join("employee", m =>
{
m.Optional();
m.KeyColumn("store_id");
m.Map(x => x.FirstName).Column("first_name");
});
}
var shops = session.QueryOver<Store>().Where(shop => shop.Id == 1).List();
SQL 生成的查询
SELECT this_.id as id3_0_,
this_.name as name3_0_,
this_1_.first_name as first2_0_0_
FROM store this_
left outer join employee
this_1_ on this_.id=this_1_.store_id
WHERE this_.id == 1
如果我只执行这个 SQL 查询,我会得到形式为
的正确结果
id3_0_ name3_0_ first2_0_0_
1 Bargin Basin Daisy
1 Bargin Basin Jack
1 Bargin Basin Sue
但是如果我通过 FN 进行操作,那么变量 shops 我会得到以下数组:
1 Bargin Basin Daisy
1 Bargin Basin Daisy
1 Bargin Basin Daisy
我用的是FN version 2.0.1.0,NHibernate 4.0。谢谢
Join(...)
用于一对一关联,但您与 Employee 有一对多关联,因此每个商店的连接 returns 多于一行,NHibernate 始终看到相同的 Id 告诉他这是同一个对象,因此它给你 2 倍相同的引用。
您可能想要的是员工及其商店的投影
class EmploymentDto
{
public int StoreId { get; set; }
public string StoreName { get; set; }
public string FirstName { get; set; }
}
EmploymentDto dto = null;
Employee employee = null;
var employments = session.QueryOver<Store>()
.Where(shop => shop.Id == 1)
.JoinAlias(s => s.Staff, () => employee)
.SelectList(l =>
l.Select(s => s.Id).WithAlias(() => dto.StoreId)
l.Select(s => s.Name).WithAlias(() => dto.StoreName)
l.Select(() => employee.FirstName).WithAlias(() => dto.FirstName)
.TransformUsing(Transformers.AliasToBean<EmploymentDto>())
.List<EmploymentDto>();
我想使用 FN 在表之间进行连接。 但是我得到的输出不是很正确的结果。 我的代码:
public class Store
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
public virtual IList<Product> Products { get; protected set; }
public virtual IList<Employee> Staff { get; protected set; }
public virtual string FirstName { get; set; }
}
public StoreMap()
{
Table("store");
Id(store => store.Id).Column("id").GeneratedBy.Increment();
Map(store => store.Name).Column("name");
Join("employee", m =>
{
m.Optional();
m.KeyColumn("store_id");
m.Map(x => x.FirstName).Column("first_name");
});
}
var shops = session.QueryOver<Store>().Where(shop => shop.Id == 1).List();
SQL 生成的查询
SELECT this_.id as id3_0_,
this_.name as name3_0_,
this_1_.first_name as first2_0_0_
FROM store this_
left outer join employee
this_1_ on this_.id=this_1_.store_id
WHERE this_.id == 1
如果我只执行这个 SQL 查询,我会得到形式为
的正确结果id3_0_ name3_0_ first2_0_0_
1 Bargin Basin Daisy
1 Bargin Basin Jack
1 Bargin Basin Sue
但是如果我通过 FN 进行操作,那么变量 shops 我会得到以下数组:
1 Bargin Basin Daisy
1 Bargin Basin Daisy
1 Bargin Basin Daisy
我用的是FN version 2.0.1.0,NHibernate 4.0。谢谢
Join(...)
用于一对一关联,但您与 Employee 有一对多关联,因此每个商店的连接 returns 多于一行,NHibernate 始终看到相同的 Id 告诉他这是同一个对象,因此它给你 2 倍相同的引用。
您可能想要的是员工及其商店的投影
class EmploymentDto
{
public int StoreId { get; set; }
public string StoreName { get; set; }
public string FirstName { get; set; }
}
EmploymentDto dto = null;
Employee employee = null;
var employments = session.QueryOver<Store>()
.Where(shop => shop.Id == 1)
.JoinAlias(s => s.Staff, () => employee)
.SelectList(l =>
l.Select(s => s.Id).WithAlias(() => dto.StoreId)
l.Select(s => s.Name).WithAlias(() => dto.StoreName)
l.Select(() => employee.FirstName).WithAlias(() => dto.FirstName)
.TransformUsing(Transformers.AliasToBean<EmploymentDto>())
.List<EmploymentDto>();