These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hopefully simple entity mapping question
PostPosted: Mon Oct 19, 2009 7:43 am 
Newbie

Joined: Fri Sep 25, 2009 8:36 am
Posts: 9
Hey,

I have one main entity that stores an auto incremented id (on the DB side its AI), then i have 2 other entities that link to this one based off that ID...

The main entity also contains the 2 smaller entities like shown below:

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_LinkedID;
   ... Some other stuff
}

public class OtherEntity2
{
   protected long m_LinkedID;
   ... Some other stuff
}



Now the DB wont have any data relating to these and when i insert the MainEntity into the DB i want to also insert in the other 2 entities, with the linked ID. So is this best to do as 3 seperate operations, insert the main, then get the returned ID and insert the other 2, (thats providing i can limit it to not insert these sub entities automatically)... or is there some way to map the IDs to link to the main entity ID and get them to be added after the main entity has been inserted and has an ID?

Im fairly new to NHibernate so sorry if this is a simple question...


Top
 Profile  
 
 Post subject: Re: Hopefully simple entity mapping question
PostPosted: Fri Oct 23, 2009 6:11 am 
Newbie

Joined: Wed Oct 21, 2009 5:34 pm
Posts: 3
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.