As I wrote above...The alias is needed not only in the XML tag but in the
query.

2009/4/2 jpcleve <[email protected]>

>
> Yep...I finally figured that one out. I have been able to load the
> collection via the custom loader but ONLY if I use the following
> statement from my object repository:
>
> using (ISession session = NHibernateHelper.OpenSession())
>            {
>                return session
>                    .GetNamedQuery("GetContactsByAccessContact")
>                    .SetInt32("AccessContactId", Id)
>                    .List<Contact>();
>
>            }
>
>
> Here's my collection mapping: (table is actually a view)
>
> <bag name="Contacts" table="vwContacts">
>      <key column ="Id"/>
>      <one-to-many class="AccessData.Domain.Contact, AccessData"/>
>      <loader query-ref="GetContactsByAccessContact"/>
>    </bag>
>
> And my custom loader that executes a sproc:
>
> <sql-query name="GetContactsByAccessContact">
>    <load-collection alias="Contacts" role="AccessContact.Contacts"/>
>    exec GetContactsByAccessContact :AccessContactId
>  </sql-query>
>
> The sproc:
>
> ALTER PROCEDURE [dbo].[GetContactsByAccessContact]
>
>        @AccessContactID int
> AS
> BEGIN
>
>        DECLARE @ContactUniqueID nvarchar(50)
>        DECLARE @AccountID_FK int
>
>        SET @ContactUniqueID = (SELECT ContactUniqueID FROM AccessContacts
> WHERE ID = @AccessContactID)
>        SET @AccountID_FK = (SELECT AccountID_FK FROM AccessContacts WHERE
> ID
> = @AccessContactID)
>
>        SELECT *
>        FROM vwContacts c
>        WHERE c.UniqueID = @ContactUniqueID AND c.AccountID_FK =
> @AccountID_FK
>
> END
>
>
> I don't understand, though, why NH won't populate the collection for
> me without my explict method-calling. The collection is mapped, so why
> isn't it lazy loaded when I evaluate the property that corresponds to
> my mapped collection? All of my other collections work just fine, but
> this one's giving me a fit. Any ideas why it won't lazy load for me?
>
> Thanks,
>
> John
>
> On Apr 2, 3:15 pm, -_- <[email protected]> wrote:
> > I think nHibernate just "knows" to populate the first ? with Id
> > (because Id is the first declared property of the class you are using
> > to populate it?).
> >
> > On Apr 2, 2:18 pm, jpcleve <[email protected]> wrote:
> >
> >
> >
> > > I've been following your thread on the other forum hoping for a
> > > solution.
> > > I have my example configured just as yours and it's almost working. My
> > > problem is that I don't know how to define parameters for the prepared
> > > sql statement. For example:
> >
> > > <sql-query name="customLoader">
> > >    <load-collection alias="dev" role="Page"/>
> > >    <![CDATA[SELECT * FROM PAGES WHERE Pages.fkbookId= :id]]>
> > >  </sql-query>
> >
> > > how is ":id" being set? where is that parameter defined?
> >
> > > Any ideas?
> >
> > > On Apr 2, 12:32 pm, -_- <[email protected]> wrote:
> >
> > > > Anyone have any sample code for populating a bag with a custom SQL
> > > > SELECT statement? I need to be able to run a query using the object's
> > > > id and it should return a list of another type of object.
> >
> > > > The mapping code below demonstrates sorta what I need to do but this
> > > > doesn't work.
> >
> > > > Mapping Code:
> >
> > > > <class name="Book" table="Devices">
> > > >    <id name="Id" type="Int32" column="Id" access="property">
> > > >      <generator class="identity">
> > > >        <param name="table">Books</param>
> > > >        <param name="column">Id</param>
> > > >      </generator>
> > > >    </id>
> > > >    <bag name="PagesInBook" inverse="true">
> > > >      <key></key>
> > > >      <one-to-many class="Pages"/>
> > > >      <loader query-ref="customLoader"/>
> > > >    </bag>
> > > >  </class>
> > > > <class name="Page" table="Pages">
> > > >    <id name="Id" type="Int32" column="Id" access="property">
> > > >      <generator class="identity">
> > > >        <param name="table">Pages</param>
> > > >        <param name="column">Id</param>
> > > >      </generator>
> > > >    <property name ="BookId" column="fkBookId"/>
> > > >    </id>
> > > > </class>
> > > > <sql-query name="customLoader">
> > > >    <load-collection alias="dev" role="Page"/>
> > > >    <![CDATA[SELECT * FROM PAGES WHERE Pages.fkbookId= :id]]>
> > > >  </sql-query>
> >
> > > > FOR MORE INFO you can look my forum post here:
> http://forum.hibernate.org/viewtopic.php?t=995589
> >
> > > > Thanks- Hide quoted text -
> >
> > - Show quoted text -
> >
>


-- 
Fabio Maulo

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