Hi,
thank you for this clarification. I'm not completely there.
I defined the collection as inverse. But now there come up a lot of
questions to me:
1. lets say I created the entity (it's transient for the moment) to
bind to a Control. The user decided to cancel the process. The entity
should not be persisted - fine.
The user saves the input. The entity should be persisted - fine.
=> where would you add this entity to the bags of other related
entities, during creation or after successful saving it?
2. Would be a good Idea to let all entities which I eventually want to
save in my application inherit from an interface, which has a Method
like AddToForeignRelations and to call this method from my Save-Method
which after session.Save(object) was successfully executed.
E.G.:
public class EntityManager
{
public void Save(IList<IPersistable> pListOfObjectsTopBeSaved)
{
ISession session = SessionProvider.GetSession();
try
{
foreach(Object thisObject in pListOfObjectsTopBeSaved)
session.Save(thisObject);
}
catch
{ //WriteToLog}
foreach(Object thisObject in pListOfObjectsTopBeSaved)
{thisObject.AddToForeignRelations() ;}
public class EntityA: IPersistable
{
public void AddToForeignRelations()
{
this.entityB.ListOfEntityA.Add(this);
this.entityC.ListOfEntityA.Add(this);
...
}
}
3. If we wouldn't do it as described in 2 and add it immediately
during creating the transient entity (like obviously Paul Batum does)
we would have to take care that if the user decides to discard changes
that we remove it from the ListOfEntityA.
4. Or would it be a good Idea to load entityA immediately after
saving it.
5. Could I do something with events in NH 2.0 to solve the problem.
Hope somebody can provide a best practice.
Thanks for your feedback
antoschka
On 22 Okt., 11:01, "Gustavo Ringel" <[EMAIL PROTECTED]> wrote:
> When working with nh you should think, in general, as if you were working
> without a persistence enviornment.
> In a non persistence enviroment, you should put the item in the collection
> if you want it there, also in NH.
> The issue is that in general in NH in this case you put inverse="true" and
> set that the other side (the many-to-one side) is going to be in charge of
> the collection. Then if you get the entity again after saving the many to
> one side you are going to get the collection "magically" loaded with the new
> item.
>
> In any case you should always add the item to both sides if you are mapping
> each point of the relation, and this is by design.
>
> Gustavo.
>
> On Wed, Oct 22, 2008 at 8:30 AM, antoschka <[EMAIL PROTECTED]> wrote:
>
> > Thanks, I do not fully understand the mechanism of the bags. Could
> > somebody shortly explain me this.
>
> > Lets say I retrieved Entities from a Database. All collections are
> > initialized correctly. Now I got a transient entity lets say
> > EntityA entityA = new EntityA();
> > entityA.PropEntityB = someEntityB;
> > If I do a session.Save(entityA) I have persisted it and NH knows about
> > entityA having a property called PropEntityB of Type EntityB. Why
> > isn't the collection updated automatically? It's clear that I want to
> > have the persistent entityA in the collection EntityB.EntityAList?
>
> > Thanks for your feedback
>
> > antoschka
>
> > On 3 Okt., 16:33, Paul Batum <[EMAIL PROTECTED]> wrote:
> > > The typical approach would be:
>
> > > class EntityA
> > > {
> > > public void AddEntityB(Entity b)
> > > {
> > > this.EntityBList.Add(b);
> > > b.EntityA = this;
> > > }
>
> > > }
>
> > > I believe many people use this approach or something similar.
>
> > > On Oct 2, 7:26 pm,antoschka<[EMAIL PROTECTED]> wrote:
>
> > > > @ Fabio: So how would you approach the problem?
>
> > > > On 2 Okt., 13:58, "Fabio Maulo" <[EMAIL PROTECTED]> wrote:
>
> > > > > NH work as your DAL and don't play in your logical tier.
>
> > > > > 2008/10/2antoschka<[EMAIL PROTECTED]>
>
> > > > > > Hi,
>
> > > > > > I use one-to-many foreign keys in my entities. e.g. EntityA &
> > EntityB
> > > > > > - EntityB has a Property which references EntityA
> > > > > > "<many-to-one name="EntityA " cascade="none" column="EntityA " not-
> > > > > > null="true" />"
>
> > > > > > - EntityA has a inverse bag which contains all EntityBs referencing
> > to
> > > > > > EntityA
> > > > > > "<bag name="FkEntityB" inverse="true" lazy="true" cascade="all">
> > > > > > <key column="EntityA " />
> > > > > > <one-to-many class="[class], [assembly]" />
> > > > > > </bag>
>
> > > > > > The whole thing works fine. The only problem I have is when I add a
> > > > > > transient new EntityB which references EntityA, hhe bag will not be
> > > > > > updated and I have to do it manually, by adding EntityB to the bag-
> > > > > > Collection.
>
> > > > > > How can this be done automatiaclly without making the new EntityB
> > > > > > persistent?
>
> > > > > > Tahnska in advance for your helpantoschka
>
> > > > > --
> > > > > Fabio Maulo
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---