2
我有2個實體(這些被分解到的問題要簡單一些):NHibernate的QueryOver分頁雖然選擇車篷1子查詢
實體A
public class EntityA
{
protected IList<EntityB> _bList = new List<EntityB>();
virtual public int Id { get; set; }
virtual public int ExtId { get; set; }
public virtual void AddB(EntityB b)
{
if (!_bList.Contains(b)) _bList.Add(b);
b.A = this;
b.ExtId = this.ExtId;
}
public virtual void RemoveB(EntityB b)
{
_bList.Remove(b);
}
public virtual IList<EntityB> BList
{
get { return _bList.ToList().AsReadOnly(); }
}
}
實體的映射
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="hibernate.domain.mappings.EntityA, hibernate.domain" lazy="true">
<id name="Id">
<generator class="native" />
</id>
<property type="int" name="ExtId" column="[ExtId]" />
<bag
name="BList"
table="EntityB"
cascade="all"
lazy="true"
inverse="true"
access="field.camelcase-underscore"
optimistic-lock="false"
>
<key column ="ExtId" property-ref="ExtId" />
<one-to-many class="hibernate.domain.mappings.EntityB, hibernate.domain" />
</bag>
</hibernate-mapping>
實體B
public class EntityB
{
protected EntityA _a;
virtual public int Id { get; set; }
virtual public int ExtId { get; set; }
virtual public EntityA A
{
get { return _a; }
set { _a = value; }
}
}
實體B映射
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="hibernate.domain.mappings.EntityB, hibernate.domain" lazy="true">
<id name="Id">
<generator class="native" />
</id>
<property type="int" name="ExtId" column="[EXTID]" />
<many-to-one
name = "A"
property-ref ="ExtId"
not-null="true"
class = "hibernate.domain.mappings.EntityA, hibernate.domain"
access="field.camelcase-underscore"
cascade = "save-update"
fetch="select"
insert = "false"
lazy = "false"
update = "false"
column="ExtId"
/>
</class>
</hibernate-mapping>
我需要做的是使用Queryover分頁得到一個清單,而選擇與A相關的B的第一項,
我用以下queryover,
using (ISession session = SessionProvider.OpenSession())
{
var bOver = (QueryOver<EntityB, EntityB>)session.QueryOver(() => bAlias)
.JoinAlias(() => bAlias.A,() => aAlias)
.SelectList(b => b.Select(() => bAlias.Id))
.Take(1);
var aOver = session.QueryOver(() => aAlias)
.SelectList(l => l.Select(() => aAlias.Id)
.SelectSubQuery<EntityB>(bOver));
var result = aOver.Skip(1).Take(1).List<object[]>();
}
但是所生成的查詢等,以下
SELECT TOP (10) y0_,
(SELECT TOP (10) this_0_.id AS y0_
FROM (SELECT this_.id
AS y0_,
(SELECT TOP (1) this_0_.id
AS
y0_,
Row_number() OVER(ORDER BY
current_timestamp) AS
__hibernate_sort_row
FROM entityb this_0_
INNER JOIN entitya aalias1_
ON
this_0_.extid = aalias1_.[EXTID])
AS y1_
FROM entitya this_) AS QUERY
WHERE QUERY.__hibernate_sort_row > 1
ORDER BY QUERY.__hibernate_sort_row)
這是不正確的,那麼我該如何解決這種情況(在現實世界的情況下,我需要選擇多個第一項,如B與A)