我有一个使用 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();
调用this时生成的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。