[fluent-nhib] Map(..).Formula() generates wrong mapping

1,167 views
Skip to first unread message

Filip Kinsky

unread,
Apr 21, 2010, 8:39:19 AM4/21/10
to Fluent NHibernate
I just needed to map one property using formula, but it looks like the
formula statement isn't executed at all and I get DB exception for
nonexistent column (ChildrenCount) instead, because FNH always
includes column element in property mapping. I'm using NHibernate
3.0.0.1001 so maybe that's the problem - I wasn't able to find it out.
The entity I'm trying to map looks like this:

public class RegionTreeItemDTO
{
public long Id { get; set; }
public long ParentId { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public int ChildrenCount { get; set; }

public bool HasChildren { get { return ChildrenCount > 0; } }
}

the FNH mapping class look like this:

public class RegionTreeItemDTOMap: DTOMapBase<RegionTreeItemDTO>
{
public RegionTreeItemDTOMap() : base("REGION")
{
Id(x => x.Id);
Map(x => x.ParentId, "PARENT_ID");
Map(x => x.Code);
Map(x => x.Name);
Map(x => x.ChildrenCount)
.Formula("(select count(1) from REGION x where x.PARENT_ID =
Id)");
}
}

and the generated property mapping for the field with formula is this:

<property name="ChildrenCount" formula="(select count(1) from
REGION x where x.PARENT_ID = Id)" type="System.Int32, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="ChildrenCount" />
</property>

The column element is causing the problems. I just tried to exclude
the FNH mapping class and replace it with HBM mapping file without the
"column" element and the mapping just become to work. So it looks like
NH does not expect to have column element when mapping using formula,
but FNH still generates the column element anyway.

Is this FNH (I'm using the 1.0 RTM) bug or is it a bug/incompatibility
with NH3 I'm using?

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.

Daniel Mirapalheta

unread,
Apr 21, 2010, 9:38:13 AM4/21/10
to fluent-n...@googlegroups.com
Filip,

I just tested with the latest build and it's working fine, make a try
with the 636 (at http://fluentnhibernate.org/downloads)

Best regards,
DM

2010/4/21 Filip Kinsky <fi...@filovo.net>:
--
Daniel Mirapalheta

Filip Kinský

unread,
Apr 21, 2010, 9:47:57 AM4/21/10
to fluent-n...@googlegroups.com
Hi Daniel,
thanks for quick response. Did you try it with NH3? As I already noted in my previous post I'm using FNH RTM version compiled against NHibernate 3.0.0.1001 binaries. This is is probably causing the problems - it looks like NH3 skips formula if column element exists in the mapping... 

Daniel Mirapalheta

unread,
Apr 21, 2010, 10:04:55 AM4/21/10
to fluent-n...@googlegroups.com
No, I used NH2 but I'm not getting exactly the point here, is it FNH
or NH? I think it's FNH who should not put the column name when using
formula. So it doesn't depends of the NH version ...

Best regards,
DM

2010/4/21 Filip Kinský <fi...@filovo.net>:

Filip Kinský

unread,
Apr 21, 2010, 10:18:32 AM4/21/10
to fluent-n...@googlegroups.com
If I understood it right it's a little compatibility issue between NH2 and NH3 which I discovered because I use FNH :) The problem is that NH2 just skips the column element when there's formula defined on a property (prefers formula to column element), but NH3 prefers column element to formula when both are defined on a property and than formula is thrown away. FNH supplies column element for each mapped property even if there's formula defined so it's making formulas unusable when running against NH3. I don't know if there's some NH rule in documentation defining formula vs column element conflict/behavior though so it's hard to say if it's FNH bug or if FNH just does not support NH3...

Daniel Mirapalheta

unread,
Apr 21, 2010, 10:45:14 AM4/21/10
to fluent-n...@googlegroups.com
To be honest there are two issues:
1- The FNH putting that column. As your own code shows you are not
setting a column name for the property, if you take a look at the FNH
code (class FluentNHibernate.Mapping.PropertyPart method
IPropertyMappingProvider.GetPropertyMapping) it put automatically a
column name according to the property, in the latest build if you set
a formula it does that ONLY if you haven't specified a formula for the
property BUT if you force the column name (see example above) it will
generate it letting NH what it *wants* to use.

Map(x => x.ChildrenCount, "MyDbColumnName").Formula("(select
count(1) from REGION x where x.PARENT_ID = Id)");
Map(x => x.ChildrenCount).Column("MyDbColumnName").Formula("(select
count(1) from REGION x where x.PARENT_ID = Id)");

2- NH used to ignore the column name and started to ignore the
formula, this is purely a NH issue as the FNH is supposed to just
generate the mappings and I don't see it as an FNH x NHn
incompability.

Best Regards,

Filip Kinský

unread,
Apr 21, 2010, 10:49:51 AM4/21/10
to fluent-n...@googlegroups.com
I see - so it's FNH RTM issue which should be fixed in trunk - I'll update to latest FNH trunk version than. Thanks for your effort.

Paul Batum

unread,
Apr 25, 2010, 10:40:17 AM4/25/10
to fluent-n...@googlegroups.com
Yeah I came across this one a few months ago when I was testing out NH3.

This is the particular commit that fixed the issue:

Please let me know if your problem persists after updating to one of the binaries provided at:

2010/4/22 Filip Kinský <fi...@filovo.net>

Filip Kinský

unread,
Apr 25, 2010, 3:53:09 PM4/25/10
to fluent-n...@googlegroups.com
Hi, thanks for your interest. I already updated to last trunk build
and it really solved the issue.
Reply all
Reply to author
Forward
0 new messages