我有一个关于如何将传统CHAR
数据库日期和时间字段合并为一个.NET DateTime
属性在我的POCO here(谢谢Berryl!)如何结合回答StackOverflow问题。现在我正试图获得一个自定义的ICritera查询来对付那个非常有用的DateTime
属性。这里是我的查询:如何比较在ICompositeUserType的NHibernate ICriteria查询中使用.NET类型?
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周内),因为在数据库中,它使用CHAR
而不是DATE
s进行比较。我不知道如何获得查询,以在查询中将CHAR
值转换为DATE
,而无需执行CreateSQLQuery(),,我想避免。有人知道怎么做吗?
更新: 我一直在寻找尝试使用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
其拆分它不喜欢它仍然反正将.NET DateTime
属性分成两个Oracle SQL CHAR
列(有关详细信息,请参阅此StackOverflow question)。
很愚蠢,我不能接受我自己的答案,因为我实际上已经在2周后发现它,甚至没有其他人甚至试图回答(或在他们的意见中缺乏任何明确的评论)。我想我会删除这篇文章,即使我认为这是一个有用的帖子,为一个不起眼的NHibernate场景。 (我很明显试图得到一个反应/这个评论8o)。 – gabe 2010-05-13 16:24:33