nhibernate / nhibernate-core

NHibernate Object Relational Mapper
https://nhibernate.info
GNU Lesser General Public License v2.1
2.13k stars 925 forks source link

specifying columns or formulas with both attributes and sub-elements is invalid. Please use only sub-elements, or only one of them as attribute. #2013

Closed cpistiner closed 5 years ago

cpistiner commented 5 years ago

Hi all,

Recently I upgrade to 5.2.3 from 5.1.3 and I'm having the following error message: NHibernate.MappingException: Could not compile the mapping document: Infraestructura.Repositories.Mappings.Comprobante.hbm.xml ---> NHibernate.MappingException: On Numero property: specifying columns or formulas with both attributes and sub-elements is invalid. Please use only sub-elements, or only one of them as attribute.

This error refer to this mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                 assembly="Modelo" namespace="Modelo"
                 default-access="field.camelcase-underscore" default-lazy="false">

    <class name="Chofer" table="Chofer">
        <id name="Id">
            <generator class="guid.comb"/>
        </id>

        <many-to-one name="Transportista" column="IdTransportista" class="Modelo.Transportista" foreign-key="FK_Chofer_Transportista"/>

        <property name="Codigo"/>
        <property name="Nombre"/>

        <component name="Cuit" class="Cuit">
            <property name="Numero" column="Cuit" type="AnsiString" length="11">
                <column name="Cuit" sql-type="char(11)"/>
            </property>
        </component>

        <property name="IdExterno" type="AnsiString" length="12">
            <column name="IdExterno" sql-type="char(12)"></column>
        </property>

        <many-to-one name="Fletero" column="IdFletero" class="Modelo.Transportista" foreign-key="FK_Chofer_Fletero"/>
    </class>

</hibernate-mapping>

And this property from class Chofer:

       public Cuit Cuit
        {
            get
            {
                if (_cuit == null)
                    _cuit = Cuit.NoDefinido;
                return _cuit;
            }
            set { _cuit = value; }
        }

How would the correct mapping be in this version?

Regards, César

bahusoid commented 5 years ago

IMHO exception message is pretty clear. What exactly is not clear to you?

You have both attribute column and sub-element <column> in your mapping for Numero property. So:

Please use only sub-elements, or only one of them as attribute.

cpistiner commented 5 years ago

Hi bahusoid,

Yes, I understand that. But in 5.1.3 this isn't a problem.

The sub-element was to optimize queries, at least that was understood.

Removing this sub-element would bring me performance problems?

Thanks for your time.

fredericDelaporte commented 5 years ago

Having both was always invalid, only one of them was taken into account. This is explained in release notes.

So anyway, in your mappings, in NH 5.1.3, either the attributes or the elements were ignored. (That was depending on the parent element, it was not even behaving consistently.)

Keep the ones you really need. Test if it works, just in case you were in the situation were the removed ones were the ones taken into account in NH v5.1 in that invalid situation.

See the release notes for more details, or the PR, #1808.

cpistiner commented 5 years ago

Having both was always invalid, only one of them was taken into account. This is explained in release notes.

So anyway, in your mappings, in NH 5.1.3, either the attributes or the elements were ignored. (That was depending on the parent element, it was not even normalized.)

Keep the ones you really need. Test if it works, just in case you were in the situation were the removed ones were the ones taken into account in NH v5.1 in that invalid situation.

See the release notes for more details, or the PR, #1808.

Very complete, thank you!