Formula and automapping

281 views
Skip to first unread message

Krzysztof Koźmic

unread,
Mar 27, 2011, 9:21:13 PM3/27/11
to fluent-n...@googlegroups.com
Hi,

I've been trying to have a calculated property with a formula specified
so the mapping in HBM would look like:

<property name="Foo" formula = "from bar select baz" />

If I want to
do it via IPropertyConvention though and I specify formula it still adds
a column element (there is no column in the database) and NHibernate
ignores the formula and just tried to get the value from column, which
obviously fails.

So I'm thinking either when formula is specified it should wipe out all
columns (I'm not sure if there is a scenario where you would want to
have a column and a formula since the formula would be ignored then but
perhaps there is).
Alternatively having an option to manually say "the column does not
exist" would also be acceptable.

James Gregory

unread,
Mar 29, 2011, 10:09:09 AM3/29/11
to fluent-n...@googlegroups.com, Krzysztof Koźmic
Sounds like a bug!

At this rate 1.2 is never going to get finished... ;)

Vahid

unread,
Mar 29, 2011, 4:04:28 PM3/29/11
to Fluent NHibernate
Hi,
You should do it this way:

public class UserCustomMappings : IAutoMappingOverride<User>
{
public void Override(AutoMapping<User> mapping)
{
mapping.Id(u => u.Id).GeneratedBy.Identity();
mapping.Map(x => x.FullName).Formula("FirstName + ' ' +
LastName";); //for public virtual string FullName { get; private
set; }
}
}

Chris V

unread,
Apr 22, 2011, 1:42:19 PM4/22/11
to Fluent NHibernate
Was this ever fixed? I just downloaded 1.2 and ran into this problem.
The formula was working as expected in 1.1, but now my code is borked
because of this issue.

On Mar 27, 8:21 pm, Krzysztof Koźmic <krzysztof.koz...@gmail.com>
wrote:
> Hi,
>
> I've been trying to have a calculated property with aformulaspecified
> so the mapping in HBM would look like:
>
> <property name="Foo"formula= "from bar select baz" />
>
> If I want to
> do it via IPropertyConvention though and I specifyformulait still adds
> a column element (there is no column in the database) and NHibernate
> ignores theformulaand just tried to get the value from column, which
> obviously fails.
>
> So I'm thinking either whenformulais specified it should wipe out all
> columns (I'm not sure if there is a scenario where you would want to
> have a column and aformulasince theformulawould be ignored then but

James Gregory

unread,
Apr 26, 2011, 8:56:25 AM4/26/11
to fluent-n...@googlegroups.com
This was not working in 1.1, and specifically fixed in 1.2. Are you sure you're having the same issue?

Chris V

unread,
Apr 26, 2011, 4:36:29 PM4/26/11
to Fluent NHibernate
The symptom was the same, but maybe it wasn't the same issue. I was
applying a formula to a property and that formula was being ignored.
Nhibernate instead tried to retrieve the value from a column of the
same name which of course did not exist. As it turns out, I had a
convention that assigned all properties a column value of the same
name as the property. For whatever, reason, the presence of a column
caused the formula mapping to be ignored.

I did try doing a Columns.Clear() on the property prior to appending
the forumla, but this had no effect and the end result was that I had
to remove the convention and add overrides where necessary. It does
work now, provided I leave out the column name convention.

James Gregory

unread,
Apr 27, 2011, 4:50:15 AM4/27/11
to fluent-n...@googlegroups.com
So you're setting the formula in your ClassMap and changing column names in a convention? Sounds like the column name convention is undoing the formula.

You do realise that column names default to the same as the property name?

belliard

unread,
May 10, 2011, 8:49:32 AM5/10/11
to Fluent NHibernate
I want to confirm that apparently, previous versions of Fluent NH
would give precedence to the FluentMappingOverride. I modified my
column-name convention to exclude the entities with Formula column and
now they work.


public class OracleUnderscoredNamingConvention :
IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
// This previously worked without this condition.
if (instance.Property.PropertyType == typeof(Partner))
{

instance.Column(OracleConventionSetter.ApplyOracleNamingConventions(instance.Property.Name));
Reply all
Reply to author
Forward
0 new messages