Sql – Fluent NHibernate / NHibernate Mappings

fluent-nhibernatenhibernate-mappingsql

I have a parent table and relationship table to find child parent relationship.
I need to find child, parent and its siblings…

EMPLOYEE_RELATION has columns EMPLOYEE_ID and PARENT_ID that saves the relationship.

Here's what SQL looks like. How do I map this to Fluent NHibernate / NHibernate.

select 
   V1.PARENT_ID AS PARENT_ID, 
   V.EMPLOYEE_ID AS EMPLOYEE_ID,
   V2.EMPLOYEE_ID AS SIBLINGS_ID 
 FROM
    EMPLOYEE V
 INNER JOIN EMPLOYEE_RELATION  V1
   ON  V.EMPLOYEE_ID = V1.EMPLOYEE_ID
 INNER JOIN EMPLOYEE_RELATION V2
   ON V2.PARENT_ID = V1.PARENT_ID
 WHERE V.EMPLOYEE_ID = 6357

Best Answer

you should be able to create graph with using many-to-many approach with your cross table EMPLOYEE_RELATION. In other words after you've fetched some entity you should be able to fetch all children attached to it as simple list of the same objects as parent is. And you should be able to traverse it further, but I'm not completely sure.

Here is how it might look in your mapping file:

        HasManyToMany(x => x.Employees)
            .WithTableName("EMPLOYEE_RELATION")
            .WithParentKeyColumn("PARENT_ID")
            .WithChildKeyColumn("EMPLOYEE_ID")
            .Cascade.AllDeleteOrphan()
            .LazyLoad();
Related Topic