Hi everyone,

I'm having troubles mapping two one-to-one relations and would like to
check whether I'm doing the mapping wrong before filing a bug.

My entities:
     public class Parent
     {
         public virtual int ID { get; set; }
         public virtual int ModuleId { get; set; }
         public virtual Child A { get; set; }
         public virtual Child B { get; set; }
     }

     public class Child
     {
         public virtual int ID { get; set; }
         public virtual Parent AParent { get; set; }
         public virtual Parent BParent { get; set; }
     }

My tables:
     CREATE TABLE public.child
     (
       id serial NOT NULL,
       CONSTRAINT pk_child PRIMARY KEY (id)
     )

     CREATE TABLE public.parent
     (
       id serial NOT NULL,
       moduleid integer NOT NULL,
       a integer,
       b integer,
       CONSTRAINT pk_parent PRIMARY KEY (id),
       CONSTRAINT fk_a FOREIGN KEY (a)
           REFERENCES public.child (id),
       CONSTRAINT fk_b FOREIGN KEY (b)
           REFERENCES public.child (id)
     )

My mapping, according to section 5.1.11. one-to-one, "unique foreign key
associations":
     <class name="Parent">
       <id name="ID">
         <generator class="native"/>
       </id>
       <property name="ModuleId" />
       <many-to-one name="A"
                    class="Child" />
       <many-to-one name="B"
                    class="Child" />
     </class>

     <class name="Child">
       <id name="ID">
         <generator class="native"/>
       </id>

       <one-to-one name="AParent"
                   class="Parent"
                   constrained="false"
                   property-ref="A" />
       <one-to-one name="BParent"
                   class="Parent"
                   constrained="false"
                   property-ref="B" />
     </class>

My query:
     List<Child> childs = session
         .Query<Child>()
         .Where(i => (i.AParent != null && i.AParent.ModuleId == 10)
             || (i.BParent != null && i.BParent.ModuleId == 10))
         .ToList();

That is, select all Childs whose parent has a ModuleId == 10 or no
parent at all.

The problem now is, that this is translated into wrong SQL:

     select child0_.ID as ID1_
     from Child child0_, Parent parent1_, Parent parent2_
     where
         child0_.ID=parent1_.A
         and child0_.ID=parent2_.B
         and (
             (child0_.ID is not null) and parent1_.ModuleId=:p0
             or (child0_.ID is not null) and parent2_.ModuleId=:p1);
     :p0 = 10 [Type: Int32 (0)], :p1 = 10 [Type: Int32 (0)]

A similar query is created using this HQL:
     from Child c
     where c.AParent.ModuleId = 10 or c.BParent.ModuleId = 10

Here's the query I'd expect:

     select child0_.ID as ID1_
     from Child child0_
         left join Parent parent1_ on (child0_.ID=parent1_.A)
         left join Parent parent2_ on (child0_.ID=parent2_.B)
     where
         (parent1_.A is not null) and parent1_.ModuleId=10
         or (parent2_.B is not null) and parent2_.ModuleId=10



What have I done wrong? Or, is this an NHibernate bug?



Best Regards, David

--
dasz.at OG              Tel: +43 (0)664 2602670     Web: http://dasz.at
Klosterneuburg                                         UID: ATU64260999

       FB-Nr.: FN 309285 g          FB-Gericht: LG Korneuburg

--
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.

Reply via email to