Loading...

nhusers@googlegroups.com

[Prev] Thread [Next]  |  [Prev] Date [Next]

Re: [nhusers] ICriteria: select different type then type of criteria Robert Rudduck Wed Mar 31 08:00:16 2010

Probably the best way to do it is to do exactly what you are doing, specify
an eager fetch for Bar, and then return from the method the right type. So
you would get (or something along these lines):

ISession session = GetSessionFromSomewhere();
ICriteria criteria = session
                             .CreateCriteria<Foo>()
                             .CreateAlias( "BarRef", "b" )
                             .SetFetchMode( "BarRef", FetchMode.Eager )
                             .Add( Expression.Eq( "b.Name", "1887" ) );

criteria.List<Foo>().Select( x => x.BarRef );

Simplest way I can think of right now.

- Robert

On Wed, Mar 31, 2010 at 8:49 AM, Robert Rudduck <[EMAIL PROTECTED]> wrote:

> Actually sorry, I didn't see that there wasn't a reference in the other
> direction. Let me rethink my answer.
>
> - Robert
>
>
> On Wed, Mar 31, 2010 at 8:48 AM, Robert Rudduck <[EMAIL PROTECTED]>wrote:
>
>> Reverse the order. You can still have the same join no problem, just
>> reverse the order of them.
>>
>> - Robert
>>
>>
>> On Wed, Mar 31, 2010 at 6:25 AM, void* <[EMAIL PROTECTED]>wrote:
>>
>>> Howdy y'all,
>>>
>>> supposed I have a simple scenario like this:
>>>
>>> public class Foo
>>> {
>>>        // id and so on omitted
>>>
>>>        public virtual Bar BarRef { get; set; }
>>>
>>> }
>>>
>>> public class Bar
>>> {
>>>        // id and so on omitted
>>>
>>>        public virtual string Name { get; set; }
>>>
>>> }
>>>
>>> <hibernate-mapping
>>>        xmlns="urn:nhibernate-mapping-2.2"
>>>        default-access="property"
>>>        auto-import="true"
>>>        default-cascade="none"
>>>        default-lazy="true" >
>>>
>>>  <class
>>>                xmlns="urn:nhibernate-mapping-2.2"
>>>                name="Foo, Abc"
>>>                table="tFoos" >
>>>
>>>                <id name="Id" type="System.Guid" >
>>>                        <generator class="assigned" />
>>>                </id>
>>>
>>>                <version name="RevisionNumber" type="System.Int32"
>>> unsaved-
>>> value="0" />
>>>
>>>                <property name="Name" type="System.String" />
>>>
>>>                <many-to-one
>>>                        class="Bar, Abc"
>>>                        name="BarRef"
>>>                        >
>>>                        <column name="BarID" />
>>>                </many-to-one>
>>>
>>>  </class>
>>>
>>> </hibernate-mapping>
>>>
>>> <hibernate-mapping
>>>        xmlns="urn:nhibernate-mapping-2.2"
>>>        default-access="property"
>>>        auto-import="true"
>>>        default-cascade="none"
>>>        default-lazy="true" >
>>>
>>>  <class
>>>                xmlns="urn:nhibernate-mapping-2.2"
>>>                name="Bar, Abc"
>>>                table="tBars" >
>>>
>>>                <id name="Id" type="System.Guid" >
>>>                        <generator class="assigned" />
>>>                </id>
>>>
>>>                <version name="RevisionNumber" type="System.Int32"
>>> unsaved-
>>> value="0" />
>>>
>>>                <property name="Name" type="System.String" />
>>>  </class>
>>>
>>> </hibernate-mapping>
>>>
>>> With HQL I can do this:
>>> select b from Foo f inner join f.BarRef b where b.Name = "1887"
>>>
>>> Even though I'm starting at a Foo I can return a Bar. Is something
>>> similar possible with ICriteria?
>>> I tried this (and different stuff):
>>>
>>> ISession session = GetSessionFromSomewhere();
>>> ICriteria criteria =
>>>                                session
>>>                                        .CreateCriteria<Foo>()
>>>                                        .CreateAlias( "BarRef", "b" )
>>>                                        .Add( Expression.Eq( "b.Name",
>>> "1887" ) );
>>>
>>> // BOOM
>>> criteria.List<Bar>();
>>>
>>> Searching the net I found no examples like that, neither in NHibernate
>>> in Action...
>>> Are ResultTransformers or Projection what I need?
>>>
>>> Regards,
>>> void*
>>>
>>> --
>>> 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]<[EMAIL PROTECTED]>
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/nhusers?hl=en.
>>>
>>>
>>
>

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