I'm sorry to bump my own thread, but I am quite stuck.

Just to make sure: using OData / DataService to generate the queries,
NHibernate does handle an $expand query for objects with a one-to-
many / many-to-one relationship?  Or is there a known bug in
NHibernate?  I am returning a NhQueryable<DesignerEntity> for the
basic query in my data context.

This is my $metadata, if that helps:

<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx";
Version="1.0">
<script/>
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/
dataservices/metadata" m:DataServiceVersion="1.0">
<Schema xmlns:d="http://schemas.microsoft.com/ado/2007/08/
dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/
dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2007/05/
edm" Namespace="ODataTest">
<EntityType Name="Widget">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property Name="Id" Type="Edm.Guid" Nullable="false"/>
<NavigationProperty Name="Designer"
Relationship="ODataTest.Designer_Widgets_Widget_Designer"
FromRole="Widget_Designer" ToRole="Designer_Widgets"/>
</EntityType>
<EntityType Name="Designer">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property Name="Id" Type="Edm.Guid" Nullable="false"/>
<NavigationProperty Name="Widgets"
Relationship="ODataTest.Designer_Widgets_Widget_Designer"
FromRole="Designer_Widgets" ToRole="Widget_Designer"/>
</EntityType>
<Association Name="Designer_Widgets_Widget_Designer">
<End Role="Designer_Widgets" Type="ODataTest.Designer"
Multiplicity="0..1"/>
<End Role="Widget_Designer" Type="ODataTest.Widget" Multiplicity="*"/>
</Association>
<EntityContainer Name="Container" m:IsDefaultEntityContainer="true">
<EntitySet Name="Widgets" EntityType="ODataTest.Widget"/>
<EntitySet Name="Designers" EntityType="ODataTest.Designer"/>
<AssociationSet Name="WidgetDesignerAssociationSet"
Association="ODataTest.Designer_Widgets_Widget_Designer">
<End Role="Designer_Widgets" EntitySet="Designers"/>
<End Role="Widget_Designer" EntitySet="Widgets"/>
</AssociationSet>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>

Thanks for any advice!!

On Dec 16, 12:12 pm, Paul <[email protected]> wrote:
> I'm pretty new to nHibernate, so I'm most likely doing something very
> dumb.  I am using nHibernate 3.2 GA.  I have two objects, "Widget" and
> "Designers".  A widget knows its designer, and it has only one
> designer.  A designer has many widgets, and it know of all of them.
> Thus, these entities:
>
> public class WidgetEntity
> {
>   public virtual Guid Id { get; set; }
>   public virtual DesignerEntity Designer { get; set; }
>
> }
>
> public class DesignerEntity
> {
>   public virtual Guid Id { get; set; }
>   public virtual string Name { get; set; }
>   public virtual string Address { get; set; }
>
>   public virtual ICollection<WidgetEntity> Widgets { get; set; }
>
> }
>
> I've created the following two mappings:
>
> <hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'
> assembly='ODataTest' namespace='ODataTest' >
>   <class name='WidgetEntity' table='WidgetEntity' >
>     <id name='Id' column='Id' >
>       <generator class='guid.comb' />
>     </id>
>     <many-to-one name='Designer' class='DesignerEntity'
> column='DesignerId' foreign-key='FK_DESIGNER_WIDGETS' cascade='save-
> update' />
>   </class>
> </hibernate-mapping>
>
> <hibernate-mapping xmlns='urn:nhibernate-mapping-2.2'
> assembly='ODataTest' namespace='ODataTest'>
>   <class name='DesignerEntity' table='DesignerEntity'>
>     <id name='Id' column='Id'>
>       <generator class='guid.comb' />
>     </id>
>     <set lazy='true' name='Widgets' inverse='true'>
>       <key column='DesignerId' />
>       <one-to-many class='WidgetEntity' />
>     </set>
>   </class>
> </hibernate-mapping>
>
> Basic queries for either widgets or designers work great; no
> problems.  But I am using this in context of an OData server, and so
> when I am issue a command 
> likehttp://[SERVER]/WidgetService/Designers(guid'[ID]')/?$expand=Widgets,
> I get an exception back.  OData is by-and-large hiding the nHibernate
> LINQ command, but inferring from stepping through the code, I think
> this is the exact command / expression being passed to
> DefaultQueryProvider.Execute:
>
> value(NHibernate.Linq.NhQueryable`1[ODataTest.DesignerEntity]).Where(elemen t
> => (element.Id == d5e3ce36-b0d0-49e7-b104-28029ff59414)).Select(p =>
> new ExpandedWrapper`2() {ExpandedElement = p, Description = "Widgets",
> ProjectedProperty0 = p.Widgets})
>
> The resulting SQL query is this:
>
> exec sp_executesql N'select designeren0_.Id as Id1_0_, widgets1_.Id as
> Id0_1_, widgets1_.DesignerId as DesignerId0_1_ from DesignerEntity
> designeren0_ inner join WidgetEntity widgets1_ on
> designeren0_.Id=widgets1_.DesignerId where designeren0_.Id=@p0',N'@p0
> uniqueidentifier',@p0='D5E3CE36-B0D0-49E7-B104-28029FF59414'
>
> I hacked out some columns for brevity sake.  My SQL server likes this
> just fine, and returns two rows.  However, when attempting to
> transform the SQL result into my returned data, I get an exception.
> What it comes down to is an InvalidCastException: "Unable to cast
> object of type 'ODataTest.WidgetEntity' to type
> 'System.Collections.Generic.ICollection`1[ODataTest.WidgetEntity]'."
> The exact location is in ResultTransformer.cs, line 25:
>
> return _itemTransformation == null ? tuple :
> _itemTransformation.DynamicInvoke(new object[] {tuple});
>
> _itemTransformation is "{Method =
> {System.Data.Services.Internal.ExpandedWrapper`2[ODataTest.DesignerEntity,S 
> ystem.Collections.Generic.IEnumerable`1[ODataTest.WidgetEntity]]
> lambda_method(System.Runtime.CompilerServices.Closure,
> System.Object[])}}".  tuple is an array of two objects.  The first is
> my DesignerEntity, complete with Widgets ICollection filled in.  The
> second is a single WidgetEntity, the values of which correlate to the
> first SQL result row.
>
> At this point, I have no idea what's going on.  :)  I am supposing
> that the problem is in my hbm files.  I have tried setting up many-to-
> one without cascade (which, I would think, doesn't matter here since
> I'm only doing a select).  I have tried setting up the one-to-many as
> a bag, and both with and without the inverse.
>
> Apologies if I'm missing some information, or this is too much
> information.  Any help would be appreciated... I've been pulling my
> hair for a couple of days on this.  :)  Thanks!!

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