NHibernate : Deleting child entity is also deleting the parent entity

Posts   
 
   
 
Anonymous
User
Posts: 0
Joined: 11-Nov-2006
# Posted on: 21-Jul-2011 13:19:52   

Hi guys,

I have TaxGroup table that is linked in a 1 : n relationship with a Tax table.

I have imported them into llblgen pro 3.1 and generated nhibernate classes and fluent mappings.

When I delete a Tax entity it also deletes the parent TaxGroup entity too. This was not not what I was expecting.

The code used to handle the delete is. Repository.Delete is really just a wrapper around Session.Delete().


public void Delete(int id)
        {
            var tax = GetById(id);

            Repository.Delete(tax);

            Repository.Flush();
        }

Anyway, my question is this. Is deleting the child entity supposed to delete the parent entity? And if so, how can I use the generated code to delete just the child entity?

I have included the SQL and Fluent configs below. If I change the .Cascade.All() call to .Cascade.None() in the TaxMap class, it deletes the tax but not the parent taxgroup but I don't want to have to keep updating my fluent files.

Cheers

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 21-Jul-2011 16:37:43   

Please check the cascading options for nhibernate (for navigators)

Frans Bouma | Lead developer LLBLGen Pro
Anonymous
User
Posts: 0
Joined: 11-Nov-2006
# Posted on: 21-Jul-2011 17:01:01   

Sorry, are you referring to a page in the help that I should review. (If so, I can't find it).

Or do I need to do something in the modeller? (I've looked under navigators for each entity and can't see anything that would allow me to set the cascade option).

Or are you suggesting that I delete the navigator that goes from Tax to TaxGroup?

Or do you mean something else entirely?

Cheers

Pete

Note: I've just upgraded from 2.6 so the gui is a bit new to me and this is my first time using nhibernate so I apologize if I'm asking daft questions.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 21-Jul-2011 17:54:06   

NHibernate controls these cascading deletes through mapping settings. You can set them in the code gen info tab of the entity: select the navigator and set SingleEntityNavigatorCascadeType or the CollectionNavigatorCascadeType to the option you want to use.

You can also set this globally: project properties => output setting values (set the e.g. CollectionNavigatorCascadeTypeDefault to set the CollectionNavigatorCascadeType setting for all collection navigators, see nhibernate specific docs for llblgen pro. )

But it's nhibernate specific, so I think you should look into the nhibernate documentation which option you should use

Frans Bouma | Lead developer LLBLGen Pro
Anonymous
User
Posts: 0
Joined: 11-Nov-2006
# Posted on: 22-Jul-2011 14:01:46   

Great, I found it.

Having a default cascade of All on the n side of a relationship seems dangerous at it's pretty easy to forget to change it. Wouldn't None be a better default for SingleEntityNavigatorCascadeTypeDefault. Or do you think I'm missing something?

Cheers

Pete

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 22-Jul-2011 14:31:13   

It's a trade-off. If we change it to none, people will complain they forgot to set it to all. wink . With NHibernate and all its settings, IMHO it's mandatory to review them all and see what you can use and what to avoid. Also e.g. the fetch strategies are key for success with nhibernate. So it's not a 'click a button and go' kind of solution -> you have to analyze what's set and eventually configure it before you go. That's the way nhibernate works...

Frans Bouma | Lead developer LLBLGen Pro
Anonymous
User
Posts: 0
Joined: 11-Nov-2006
# Posted on: 22-Jul-2011 15:15:34   

I'm learning that. I'll be honest, I'm not enjoying NHibernate. There's are too many ways to do some things, i.e, queries and no obvious ways to do others i.e, fetch an object graph with different filters on each branch. A lot of companies are asking for Nhibernate though so it's a 'have to know' rather than 'would like to use'. Maybe, I'll get used to it but right now I am missing the llblgen framework rage Anyway, thanks for your help and keep up the good work. LLBLGEN is still the best. Pete

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 22-Jul-2011 17:59:13   

Thanks for the kind words, Pete simple_smile Good luck with nhibernate, and you can always switch the target framework if you're fed up with NH wink

Frans Bouma | Lead developer LLBLGen Pro
Anonymous
User
Posts: 0
Joined: 11-Nov-2006
# Posted on: 13-Oct-2011 11:31:32   

I've been bitten by this issue again disappointed

I've just checked the documentation for nhibernate and the default cascade is set as follows:

default-cascade (optional - defaults to none): a default cascade style.

I think llblgen should have the same default.

Also, wouldn't this let the database handle the cascades instead?

I'd appreciate your thoughts on this.

Cheers

Pete

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 14-Oct-2011 11:21:17   

lad4bear wrote:

I've been bitten by this issue again disappointed

I've just checked the documentation for nhibernate and the default cascade is set as follows:

default-cascade (optional - defaults to none): a default cascade style.

I think llblgen should have the same default.

That's cool, but we have a different opinion wink .

Also, wouldn't this let the database handle the cascades instead?

No idea, as it's not documented, but I think it simply doesn't do session based cascading, so it indeed relies on the FK cascading settings defined in the relational schema whether cascading happens in the database.

You can set the default to none in the project settings.

Frans Bouma | Lead developer LLBLGen Pro
Anonymous
User
Posts: 0
Joined: 11-Nov-2006
# Posted on: 19-Oct-2011 13:41:03   

That's cool, but we have a different opinion.

I now agree with you on this.