NHibernate - how to save object with where clause
-
19-08-2019 - |
Question
I don't know how to save object with where clause. I need it to prevent saving object with range of dates overlapping on others.
public class TaskEvent
{
public DateTime StartDate {get;set;}
public DateTime EndDate {get;set;}
}
I want to check overlaping in criteria within saving operation but I don't know how.
Any ideas?
Solution
You can use HQL for ad hoc update queries
session.CreateQuery("UPDATE TaskEvent SET ... WHERE ID = :ID and ...")
.SetInt32("ID", ID)
//.SetDateTime("", )
//.SetDateTime("", )
.ExecuteUpdate();
or to do in a more NHibernate kind of way...fetch the required TaskEvents(where clause), update their properties, and commit the transaction.
OTHER TIPS
You need to figure out in code which objects need saving, then save those. This is business logic and should not be pushed into persistence operations. IMO, even if NH could support that.
One approach would be to retermine which TaskEvent objects you do not want to save in code and evict them from the ISession so that they won't be persisted.