Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate Many To Many Relationship - Issue when deleting a part of the relationship
    primarykey
    data
    text
    <p>I have a many to many relationship between a Team and an Employee entity.</p> <p>I mapped them as following:</p> <pre><code>public class EmployeeMap : ClassMap&lt;Employee&gt; { public EmployeeMap() { // identifier mapping Id(p =&gt; p.Id).Column("EmployeeID"); // column mapping Map(p =&gt; p.EMail); Map(p =&gt; p.LastName); Map(p =&gt; p.FirstName); // relationship mapping HasManyToMany(m =&gt; m.Teams).Table("EmployeeTeam") .Inverse() .Cascade.All() .AsSet() .LazyLoad() .ParentKeyColumn("EmployeeID") .ChildKeyColumn("TeamID"); HasMany(p =&gt; p.LoanedItems).Cascade.SaveUpdate().KeyColumn("EmployeeId"); } } public class TeamMap : ClassMap&lt;Team&gt; { public TeamMap() { // identity mapping Id(p =&gt; p.Id).Column("TeamID"); // column mapping Map(p =&gt; p.Name); // relationship mapping HasManyToMany(m =&gt; m.Employees) .Table("EmployeeTeam") .LazyLoad() .Cascade.All() .AsSet() .ParentKeyColumn("TeamID") .ChildKeyColumn("EmployeeID"); } } </code></pre> <p>Then I created 3 Teams and 2 Employees:</p> <pre><code>TeamID EmployeeID 1 1 1 2 2 2 3 1 </code></pre> <p>The Employee1 has also 2 LoanedItems(Books, Magazines). Employee2 has no LoanedItems.</p> <p>Now I want to delete Employee1, who is in Team1 and Team3. In Team 1 is also Employee2. So when I delete Employee1, I assume that Employee1 is deleted and also Team3, because I also assume that an Team can only exist when it has an Employe and vice versa. So Team1 may not be deleted, because it has Employee2 and can still exists. </p> <p>I used the following code lines with a new Session: </p> <pre><code>var loadedEmployee = session.Get&lt;Employee&gt;(1); session.Delete(loadedEmployee); transaction.Commit(); </code></pre> <p>But what happens? -> <strong>NHibernate deletes all Teams and all Employees</strong>! -> NHibernate updated my LoanedItem table correctly by setting the FK EmployeeID to NULL.</p> <p>What is wrong there?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload