1

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.

5
  • -1 this is a terrible question- no code or mapping is shown, nothing is said as to what changes are made, no details of the exception are given... in spite of all that- I think @Ryan Stewart managed to give a helpful answer Commented Aug 23, 2011 at 8:59
  • @sJhonny If you know the answer, you will know what area needs changes. Please give the answer or go away! Commented Aug 23, 2011 at 9:53
  • @sJhonny read it carefully, the exception has been provided. Please dont make terrible vote on someone's post. Commented Aug 23, 2011 at 10:03
  • the possibilty to vote down exists in order to indicate that a question is not informative. no one here works for you, so the attitude of 'give the answer or go away' is not what SO is about. I meant to indicate that your question is not clear and makes it hard to help you (therefore- voted down). so you can go ahead and argue the vote down, or you can try to actually provide the missing information so that i can give you the answer. Commented Aug 23, 2011 at 10:22
  • @sJhonny Thank you for your advice. For my question, i think i provided enough information. FYI, 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. Commented Aug 23, 2011 at 11:24

1 Answer 1

2

You haven't shown much code to go off of, but with Hibernate, generally speaking, all you have to do is:

start transaction
load your objects
make whatever changes to the loaded objects
commit transaction

Note the lack of any calls to Save() or SaveOrUpdate(). After loading an object using a Hibernate Session, that object is still attached to the session, and any changes made to it will propagate back to the database upon commit. New objects added into an existing (persistent) graph will need to be saved, either directly by passing the object to Session.Save() or by having it cascade along a relationship.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your advice. Do I need to perform all the operations (load data from db, make changes to the graph of objects) within a session.
You can only do things with a session in Hibernate. If you meant "within a transaction", then yes, many people consider it best practice to put all persistence-related operations, including reads, in a transaction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.