我有一个已回答的 StackOverflow 问题,关于如何在我的 POCO 中将遗留数据库CHAR
日期和时间字段合并到一个 .NETDateTime
属性中
(非常感谢Berryl!)。现在我试图让一个自定义的 ICritera 查询来对抗那个属性,但无济于事。这是我的查询:DateTime
ICriteria criteria =
Session.CreateCriteria<InputFileLog>()
.Add(Expression.Gt(MembersOf<InputFileLog>.GetName(x => x.FileCreationDateTime), DateTime.Now.AddDays(-14)))
.AddOrder(Order.Desc(Projections.Id()))
.CreateCriteria(typeof(InputFile).Name)
.Add(Expression.Eq(MembersOf<InputFile>.GetName(x => x.Id), inputFileName));
IList<InputFileLog> list = criteria.List<InputFileLog>();
这是它生成的查询:
SELECT this_.input_file_token as input1_9_2_,
this_.file_creation_date as file2_9_2_,
this_.file_creation_time as file3_9_2_,
this_.approval_ind as approval4_9_2_,
this_.file_id as file5_9_2_,
this_.process_name as process6_9_2_,
this_.process_status as process7_9_2_,
this_.input_file_name as input8_9_2_,
gonogo3_.input_file_token as input1_6_0_,
gonogo3_.go_nogo_ind as go2_6_0_,
inputfile1_.input_file_name as input1_3_1_,
inputfile1_.src_code as src2_3_1_,
inputfile1_.process_cat_code as process3_3_1_
FROM input_file_log this_
left outer join go_nogo gonogo3_ on this_.input_file_token=gonogo3_.input_file_token
inner join input_file inputfile1_ on this_.input_file_name=inputfile1_.input_file_name
WHERE this_.file_creation_date > :p0 and
this_.file_creation_time > :p1 and
inputfile1_.input_file_name = :p2
ORDER BY this_.input_file_token desc;
:p0 = '20100401',
:p1 = '15:15:27',
:p2 = 'LMCONV_JR'
实际上,该查询正是我所期望的,除了它实际上并没有给我想要的东西(过去 2 周内的所有行),因为在 DB 中,它使用CHAR
s 而不是DATE
s 进行大于比较。我不知道如何在不执行 CreateSQLQuery() 的情况下让查询将CHAR
值转换为查询中的 a,我想避免这样做。有人知道怎么做吗?DATE
更新:我一直在考虑尝试使用Projections.SqlFunction()
或公式来实现这一点,但到目前为止无济于事。这是我使用的代码SqlFunction()
,但出现NHibernate.QueryException : property does not map to a single column: FileCreationDateTime
错误:
DateTime twoWeeksAgo = DateTime.Now.AddDays(-14);
ICriteria criteria =
Session.CreateCriteria<InputFileLog>()
.Add(Restrictions.Gt(Projections.SqlFunction("to_date", NHibernateUtil.DateTime, Projections.Property(MembersOf<InputFileLog>.GetName(x => x.FileCreationDateTime))), twoWeeksAgo))
//.Add(Expression.Gt(MembersOf<InputFileLog>.GetName(x => x.FileCreationDateTime), DateTime.Now.AddDays(-14)))
.AddOrder(Order.Desc(Projections.Id()))
.CreateCriteria(typeof(InputFile).Name)
.Add(Expression.Eq(MembersOf<InputFile>.GetName(x => x.Id), inputFileName));
我确定我在这里做错了,它仍然不喜欢它,因为它FileCreationDateTime
使用了ICompositeUserType
将 .NETDateTime
属性拆分为两个 Oracle SQLCHAR
列的自定义(有关详细信息,请参阅此 StackOverflow问题)。