Like you said its a really simple question.
nhibernate does not work the way you think. The nhibernate docs contain plenty information that can help you in this regard.
First thing is that it takes care of all the related object ids so you dont have to deal with that. Each object will have its own id which can be generated, assigned etc. Your classes will end up looking like this instead:
Code:
public class MainEntity
{
protected long m_ID;
... Some other stuff
protected OtherEntity1 m_OtherEntity1;
protected OtherEntity2 m_OtherEntity2;
}
public class OtherEntity1
{
protected long m_ID;
... Some other stuff
// if association is bidirectional you will also have
protected MainEntity m_main;
}
public class OtherEntity2
{
protected long m_ID;
... Some other stuff
// if association is bidirectional you will also have
protected MainEntity m_main;
}
To save the entities you create them make the associations just like you would if they would not be saved to a database, "persistence ignorant", as follows:
Code:
MainEntity main = new MainEntity();
OtherEntity1 entity1 = new OtherEntity1();
main.setOtherEntity1(entity1);
entity1.setmainEntity(main);
OtherEntity2 entity2 = new OtherEntity2();
main.setOtherEntity2(entity2);
entity2.setmainEntity(main);
You will have to create mapping files that tell the framework more about the associations. So I suggest you go through the documentation to see how this is done. Really simple.
Depending on how the mappings are done persisting these objects can be as involved as
Code:
{
// code to set up links
...
session.Save(main);
session.Save(entity1);
session.Save(entity2);
session.Flush();
}
or it can be as simple as
Code:
{
// code to set up links
....
session.Save(main);
session.Flush();
}
if in the mapping files you set the cascade="save-update" or any of the other options on the associations. nhibernate will follow all the references from main and save entity1 and entity2 as well.
Hope this helps