NHibernate, updating a db column to a function call when persisting a domain object
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.
精彩评论