We have three C# classes: Branch, Team and Employee. A branch has many 
teams and many employees. A team has many employees. Each employee belongs 
to one team only.

Implementation 1:

session.CreateQuery("DELETE FROM Employee e WHERE e.Branch.Id = 1"); // line 1
session.CreateQuery("DELETE FROM Team t WHERE t.Branch.Id = 1"); // line 2

On line 2, we get “foreign key constraint failed” if one of the teams has 
employees. So it's like line 1 did not really work.

Implementation 2:

ICriteria criteria = session.CreateCriteria(typeof(Employee));
criteria.Add(NHibernate.Criterion.Expression.Eq(“Branch”, branch));var e = 
criteria.List();if (e != null && e.Count > 0) foreach (var e2 in e) 
this.Session.Delete(e2); 
ICriteria criteria = session.CreateCriteria(typeof(Team));
criteria.Add(NHibernate.Criterion.Expression.Eq(“Branch”, branch));var t = 
criteria.List();if (t != null && t.Count > 0) foreach (var t2 in t) 
this.Session.Delete(t2);

Implementation 2 works, but we think it’s slower because it reads objects 
from the DB.  How can we get implementation 1 to work?

Also note:

class BranchMap : ClassMap<Branch> { ...
  HasMany(x => x.Teams).Table("TEAM").KeyColumn("BRANCH_ID").Inverse();
  HasMany(x => x.Employees).Table("EMPLOYEE").KeyColumn("BRANCH_ID").Inverse();
class TeamMap : ClassMap<Team> { ...
  HasMany(x => x.Employee).Table("EMPLOYEE").KeyColumn("TEAM_ID").Inverse();
  References(x => x.Branch).Column("BRANCH_ID").Not.Nullable().ForeignKey();
class EmployeeMap : ClassMap<Employee> { ...
  References(x => x.Team).Column("TEAM_ID").ForeignKey();
  References(x => x.Branch).Column("BRANCH_ID").Not.Nullable().ForeignKey();

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to