NHibernate Stored procedure mapping properties on a complex object

I have a SQL stored procedure that I am trying to use to load a collection of objects. My objects look like this:

public class Person
{ 
       public virtual int Id {get;set;}
       public virtual string Name {get;set;}
       public virtual Colour FavoriteColour {get;set;}
}

public class Colour
{
       public virtual int Id {get;set;}
       public virtual string Name {get;set;}
}

My stored procedure looks something like this:

Create Procedure getAllPersons
AS
     SELECT
            p.Id as PersonId,
            p.Name as PersonName,
            c.Id as ColourId,
            c.Name as ColourName
     FROM
            Person p JOIN Colour c
      p.ColourId = c.Id

My mapping is as follows

<class name="Person">
  <id name="Id">
    <generator class="native"/>
  </id>
  <property name="Name" />
  <many-to-one name="FavoriteColour" class="Foo, Colour" column="ColourId"/>
</class>

<sql-query name="getAllPersons">
    <return class="Person">
        <return-property name="Id" column="PersonId"/>
        <return-property name="Name" column="PersonName"/>
        <return-property name="FavoriteColour">
            <return-column name="ColourId"/>
        </return-property>
    </return>
    exec getAllPersons
</sql-query>

I want to know how I can map the colourName column that comes from my proc to the FavoriteColour.Name property in my Person object so that the FavoriteColour object for Person is populated with both its Id and Name properties. Any ideas?

+3
source share

Source: https://habr.com/ru/post/1716035/


All Articles