对于特定的colmumn,我要求能够获取与特定日期相关的数据而不是DateTime。
是否可以根据日期时间的提取日期部分映射相关对象?两个实体之间是否存在明确的外键关系?
将它与SQL相关联,我希望它生成如下查询:
SELECT *
FROM Transactions trans
INNER JOIN TransactionDetails details
ON trans.DatePerformed = CAST(details.datetimedetails AS DATE)
答案 0 :(得分:1)
QueryOver
的方式是:
TransactionEntitytrans = null;
TransactionDetails details = null;
var results = session.QueryOver<TransactionEntity>(() => trans)
.JoinQueryOver(() => trans.Details, () => details, JoinType.InnerJoin
, Restrictions.EqProperty(
Projections.Property<TransactionEntity>(_ => trans.DatePerformed ),
Projections.Cast(
NHibernate.NHibernateUtil.Date,
Projections.Property<Occupation>(_ => details.datetimedetails ))
)
)
.List<TransactionEntity>();
我们实际做的是使用.JoinQueryOver()
的第四个参数。此参数是限制(ICriterion withClause
),并使用AND运算符添加到JOIN
INNER JOIN TransactionDetails details
ON trans.ID = details.TransactonID
// here is injected the with clause
AND trans.DatePerformed = CAST(details.datetimedetails AS DATE)
在这个withClause中,我们只是通过一个小技巧 Restrictions.EqProperty()
创建限制,因为这需要投影
EqProperty(IProjection lshProjection, IProjection rshProjection)
(所以我们不比较两个属性,而是比较其他一些预测)。然后我们只使用CAST来获得所需的结果
答案 1 :(得分:1)
如果我们需要跳过映射 关系映射(需要CROSS JOIN
带WHERE
子句时的方法) )我们可以使用 HQL (只有这样,请查看此处:)
HQL sytax将是:
var hql = " SELECT trans " +
" FROM TransactionEntity trans "
" , TransactionDetails details " + // CROSS JOIN without any ON
" WHERE trans.DatePerformed = CAST(trans.datetimedetails as Date) " +
"";
var query = session.CreateQuery(hql);
var results = query.List<TransactionEntity>();