NHibernate在子查询中使用连接:不生成连接sql语句

时间:2012-10-06 00:18:39

标签: nhibernate nhibernate-criteria

我使用QueryOver进行调用,其布局如下,并且不生成子查询中所需的内部连接语句。

我要查询的对象的结构如下:

public class Product {
    public virtual long Id { get; set; }
    ... OTHER DATA MEMBERS ...
}

public class ProductListType1 {
    public virtual int Id { get; set; }
    public virtual DateTime Date { get; set; }
    public virtual char Category { get; set; }
    ... OTHER DATA MEMBERS ...
    public virtual IDictionary<short, Detail> Details { get; set; }

    public class Detail {
        public virtual short SequenceNumber { get; set; }
        public virtual ProductListType1 ProductListType1 { get; set; } // PARENT REFERENCE FOR BIDIRECTIONAL RELATIONSHIP
        public virtual Product Product { get; set; }
        ... OTHER DATA ...
    }
}


public class ProductListType2 {
    // SAME STRUCTURE AS PRODUCT LIST TYPE 1
}


ProductListType1.Detail detailAlias = null;
Product productAlias = null;

ProductListType2 productListType2Alias = null;
ProductListType2.Detail productListType2DetailAlias = null;

_session.QueryOver<ProductListType1>()
    .Left.JoinAlias(productListType1 => productListType1.Details, () => detailAlias)
    .Left.JoinAlias(() => detailAlias.Product, () => productAlias)
    .Where(productListType1 => productListType1.Next == "abc" || productListType1.Final == "abc")
    .And(productListType1 => productListType1.Status.IsIn(new string[] { "STATUS1", "STATUS2", "STATUS3", "STATUS4"}))
    .WithSubquery.WhereNotExists(QueryOver.Of<ProductListType2.Detail>(() => productListType2DetailAlias)
        .Select(productListType2Detail => productListType2Detail.Product.id)
        .Inner.JoinAlias(productListType2Detail => productListType2Detail.ProductListType2, () => productListType2Alias)
        .Where(productListType2Detail => productListType2Detail.SequenceNumber < 900)
        .And(productListType2Detail => productListType2Detail.Product.Id == detailAlias.Product.Id)
        .And(() => productListType2Alias.Date == DateTime.Today)
        .And(() => productListType2Alias.Category == "D")
    )
    .TransformUsing(Transformers.DistinctRootEntity)
    .List();

调用它时生成的sql如下。

SELECT (lots of stuff)
FROM PRODUCT_LIST_TYPE1_TABLE this_
LEFT OUTER JOIN PRODUCT_LIST_TYPE1_DETAILS_TABLE detailalia1_
    ON this_.id=detailalia1_.parentId
LEFT OUTER JOIN PRODUCT_TABLE product2_
    ON detailalia1_.productId=product2_.id
WHERE (this_.next = ? or this_.dest = ?)
AND this_.stat in (?, ?, ?, ?)
AND NOT EXISTS (
    SELECT this_0_.productId as y0_
    FROM PRODUCT_LIST_TYPE2_DETAILS_TABLE this_0_
    WHERE this_0_.sequenceNumber < ?
    AND this_0_.productId = detailalia1_.pron
    AND productlisttype2_.date = ?
    AND productlisttype2_.type = ?
); PARAMETERS

生成的sql会引发异常:

GenericADOException: {"SQL5001 Column qualifier or table productlisttype2_ undefined."}

有人能告诉我为什么子查询.JoinAlias()调用没有为PRODUCT_LIST_TYPE2_TABLE生成内连接语句以及如何让它这样做?

要清楚我要生成的sql是:

SELECT (lots of stuff)
FROM PRODUCT_LIST_TYPE1_TABLE this_
LEFT OUTER JOIN PRODUCT_LIST_TYPE1_DETAILS_TABLE detailalia1_
    ON this_.id=detailalia1_.parentId
LEFT OUTER JOIN PRODUCT_TABLE product2_
    ON detailalia1_.productId=product2_.id
WHERE (this_.next = ? or this_.dest = ?)
AND this_.stat in (?, ?, ?, ?)
AND NOT EXISTS (
    SELECT this_0_.productId as y0_
    FROM PRODUCT_LIST_TYPE2_DETAILS_TABLE this_0_
    INNER JOIN PRODUCT_LIST_TYPE2_TABLE productlisttype2_  // THIS PART IS MISSING
        ON this_0_.parentId=productlisttype2_.id
        AND productlisttype2_.date = ?
        AND productlisttype2_.type = ?
    WHERE this_0_.sequenceNumber < ?
    AND this_0_.productId = detailalia1_.pron
); PARAMETERS

或在子查询中大致相同。

更新: 我也试图改变:

 .Inner.JoinAlias(productListType2Detail => productListType2Detail.ProductListType2, () => productListType2Alias)

 .Inner.JoinAlias(() => productListType2DetailAlias.ProductListType2, () => productListType2Alias)

并尝试使用连接查询:

 .Inner.JoinQueryOver(productListType2Detail => productListType2DetailAlias.ProductListType2)

这两个更改都生成了相同的sql,其中INNER JOIN不存在。

1 个答案:

答案 0 :(得分:1)

您可以尝试按如下方式重新解析查询,

ProductListType1.Detail detailAlias = null;
Product productAlias = null;

ProductListType2 productListType2Alias = null;
ProductListType2.Detail productListType2DetailAlias = null;

_session.QueryOver<ProductListType1>()
    .Left.JoinAlias(productListType1 => productListType1.Details, () => detailAlias)
    .Left.JoinAlias(() => detailAlias.Product, () => productAlias)
    .Where(productListType1 => productListType1.Next == "abc" || productListType1.Final == "abc")
    .And(productListType1 => productListType1.Status.IsIn(new string[] { "STATUS1", "STATUS2", "STATUS3", "STATUS4"}))
    .WithSubquery.WhereNotExists(QueryOver.Of<ProductListType2.Detail>(() => productListType2DetailAlias)
        .Select(productListType2Detail => productListType2Detail.Product.id)
        .Inner.JoinAlias(() => productListType2Detail.ProductListType2, () => productListType2Alias)
        .Where(productListType2Detail => productListType2Detail.SequenceNumber < 900)
        .And(productListType2Detail => productListType2Detail.Product.Id == detailAlias.Product.Id)
        .And(() => productListType2Alias.Date == DateTime.Today)
        .And(() => productListType2.Category == "D")
    )
    .TransformUsing(Transformers.DistinctRootEntity)
    .List();

改变是,

.Inner.JoinAlias(productListType2Detail => productListType2Detail.ProductListType2, () => productListType2Alias)

.Inner.JoinAlias(() => productListType2Detail.ProductListType2, () => productListType2Alias)