NHibernate - how to persist an object with a where clause

I don't know how to store an object with a where clause. I need this to prevent the object from being saved with a date range overlapping on others.

public class TaskEvent
{
    public DateTime StartDate {get;set;}
    public DateTime EndDate {get;set;}
}

      

I want to check if the criteria match in a save operation, but I don't know how.

Any ideas?

0


source to share


3 answers


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 do it in a more NHibernate way ... get the required TaskEvents (where clause), update their properties and commit the transaction.

+1


source


You need to figure out the code to save the resources and then save them. This is business logic and should not be entered in save operations. IMO, even if NH supported it.



+2


source


One approach is to rename the TaskEvent objects that you don't want to persist in your code, and evict them from the ISession so they won't persist.

0


source







All Articles