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 like
http://[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(element
=> (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,System.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.