I have a problem saving a graph of objects into database via NHibernate. Specifically, I read the data from database, initialise the objects using the data, make changes to the objects, and try to save the object into database, via saveorupdate on the root report instance, in one transaction. The changes might include add, update, and delete operation to the objects.
I want to find out Whether or not Nhibernate be able to figure out what types of sql commands (ie, update, delete, insert) to generate, and the sequence to execute based on database foreign key constraints, all in one transaction.
Below is my code:
public class Report{
public virtual int Id {get;set;}
public virtual IList<Report> Children {get;set;}
public virtual Report Parent {get;set;}
public virtual IList<Parameter> Parameters {get;set;}
}
public class Parameter{
public virtual int Id {get;set;}
public virtual Report Report {get;set;}
}
A report can contains a collection of parameters, reports, and a parent report.
After initialised based on the data retrieved from database, changes are made to the graph of objects, and try to save it into database by using NHibernate Session.SaveOrUpdate() passing the root report instance. However, it throws exception, saying cannot insert null into id column.
Edit I only want to find out if it is possible for NHibernate to generate sql commands (CRUD). I will open another question if I have problem in my code.
Any ideal would be very much appreciated.