映射到 Sql DateTime 的日期部分
Mapping to Date part of Sql DateTime
我需要能够为特定列获取与特定日期相关的数据,而不是与日期时间相关的数据。
是否可以根据日期时间的提取日期部分映射相关对象?两个实体之间不存在显式外键关系的地方?
将其与 SQL 相关联,我希望它生成如下查询:
SELECT *
FROM Transactions trans
INNER JOIN TransactionDetails details
ON trans.DatePerformed = CAST(details.datetimedetails AS DATE)
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
) 并添加到 JOIN with AND operator
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 来获得所需的结果
万一我们需要跳过映射关系映射(当CROSS JOIN
需要 WHERE
子句) 我们可以使用 HQL (唯一的方法,检查例如这里:)
- Nhibernate QueryOver JoinAlias UnRelated Entity
HQL 语法将是:
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>();
我需要能够为特定列获取与特定日期相关的数据,而不是与日期时间相关的数据。
是否可以根据日期时间的提取日期部分映射相关对象?两个实体之间不存在显式外键关系的地方?
将其与 SQL 相关联,我希望它生成如下查询:
SELECT *
FROM Transactions trans
INNER JOIN TransactionDetails details
ON trans.DatePerformed = CAST(details.datetimedetails AS DATE)
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
) 并添加到 JOIN with AND operator
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 来获得所需的结果
万一我们需要跳过映射关系映射(当CROSS JOIN
需要 WHERE
子句) 我们可以使用 HQL (唯一的方法,检查例如这里:)
- Nhibernate QueryOver JoinAlias UnRelated Entity
HQL 语法将是:
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>();