开发者

NHibernate, updating a db column to a function call when persisting a domain object

开发者 https://www.devze.com 2022-12-31 06:59 出处:网络
I have a domain class that when persisted to Oracle must update a column to sysdate. NHibernate must only generate 1 SQL. e.g.

I have a domain class that when persisted to Oracle must update a column to sysdate. NHibernate must only generate 1 SQL. e.g.

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

Can this b开发者_JAVA技巧e done?

EDITED:

Could be something like:

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


You could update the stamp via a database trigger:

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

Then all hibernate would need to do is "persist" the change to age (for example).


If you want to do that on demand, you can just execute the query above:

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

Interestingly, that is both valid HQL and SQL.

0

精彩评论

暂无评论...
验证码 换一张
取 消