NHibernate,在持久化域对象时将db列更新为函数调用

时间:2010-05-25 09:06:57

标签: oracle nhibernate

我有一个域类,当持久化到Oracle时必须将列更新为sysdate。 NHibernate必须只生成1个SQL。 e.g。

update person set age = 12,stamp = sysdate where id = 1;

可以这样做吗?

编辑:

可能是这样的:

Person person = (Person)session.Get(typeof(Person), 1);
session.SetFunction(person, "stamp", Functions.CurrentTimestamp);
person.Age = 12;
session.Flush();

2 个答案:

答案 0 :(得分:0)

您可以通过数据库触发器更新戳记:

create trigger person_trg
before update
for each row
begin
    :new.stamp := sysdate;
end;

然后所有hibernate都需要做的是“坚持”改变年龄(例如)。

答案 1 :(得分:0)

如果您想按需执行此操作,只需执行上述查询:

session.CreateQuery("update person set age = 12, stamp = sysdate where id = 1")
       .ExecuteUpdate();

有趣的是,这是有效的HQL和SQL。