带有Postgresql Timestamp列的NHibernate Criteria查询,.NET DateTime截断查询参数中的毫秒数

时间:2013-01-30 00:19:51

标签: c# postgresql nhibernate

我有一个NHibernate条件查询如下:

using (var unitOfWork = this.UnitOfWorkFactory.CreateUnitOfWork())
{
    const string fromstring = "2013-01-29 23:30:33.5993";
    var from = DateTime.Parse(fromstring);

    const string tostring = "2013-01-29 23:30:33.63434";
    var to = DateTime.Parse(tostring);

    var criteria = unitOfWork.Session.CreateCriteria<DataModel>();

    criteria.Add(Restrictions.Ge("Created", from));
    criteria.Add(Restrictions.Le("Created", to));

    items = criteria.List<DataModel>();
}

我正在使用Postgresql,这是表映射:

-- Table: data

-- DROP TABLE data;

CREATE TABLE data
(
  id integer NOT NULL DEFAULT nextval('data__sequence'::regclass),
  created timestamp without time zone NOT NULL,
  CONSTRAINT itemsupdatelog_pkey PRIMARY KEY (id )
)
WITH (
  OIDS=FALSE
);
ALTER TABLE data
  OWNER TO ubuntu;

和NHibernate映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Data" namespace="Data.Models">
  <class name="DataModel" table="Data" mutable="false">
    <id name="Id">
      <generator class="sequence">
        <param name="sequence">Data__Sequence</param>
      </generator>
    </id>

    <property name="Created" not-null="true" type="Timestamp" />

  </class>
</hibernate-mapping>

我可以确认表中的数据看起来很好(即所有Created值上肯定有毫秒数,当我使用NHibernate查询数据库的OUT时,DateTime对象具有正确的毫秒数)。

但是,当我使用NHProf检查查询时,NHibernate不会在查询中发送正确的毫秒数:

    SELECT this_.Id            as Id1_0_,
       this_.Created       as Created1_0_,
FROM   Data this_
WHERE  this_.Created >= '2013-01-29T23:30:33.00' /* :p1 */
       and this_.Created <= '2013-01-29T23:30:33.00' /* :p2 */
ORDER  BY this_.Created desc

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

<强>更新 问题似乎出现在NHProf中。 NHProf报告的SQL:

SELECT this_.Id      as Id0_0_,
   this_.Created as Created0_0_
FROM   data this_
WHERE  this_.Created >= '2013-01-29T23:30:33.00' /* :p0 */
       and this_.Created <= '2013-01-29T23:30:33.00' /* :p1 */

在postgres日志中报告的SQL:

SELECT this_.Id as Id0_0_, this_.Created as Created0_0_ 
FROM data this_ 
WHERE this_.Created >= ((E'2013-01-29 23:30:33.599300')::timestamp) 
and this_.Created <= ((E'2013-01-29 23:30:33.634340')::timestamp)

要仔细检查,我在查询中插入了一行介于毫秒之间的行:

insert into data (created) values ('2013-01-29 23:30:33.600')

然后返回此行。