Johan Haest
Johan Haest

Reputation: 4421

NHibernate map a column to a property and a bag

I'm having some difficulties mapping the following sitation:

I have a person with a connection id, I use this to get map a bag and a property. This works if the database already exists. But for our unit tests, we generate one from the schema, it gives an error : "duplicate column names".

Here is the mapping:

    <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="DomainLayer.SearchDomain" assembly="Test">
  <class name="SearchPerson" table="person" lazy="false" mutable="false">
    <id name="Id" column="`id`" type="int">
      <generator class="identity" />
    </id>
    <property name="Name" column="`NAAM`" type="string"  />
    <property name="FirstName" column="`VOORNAAM`" type="string"  />
    <property name="ConnectionId" column="`Koppelid`" type="int" />
    <bag name="Languages" lazy="false" mutable="false" access="field.camelcase-underscore">
      <key property-ref="ConnectionId" column="Koppelid" />
      <one-to-many class="DomainLayer.Person.LanguageSkill, Test" />
    </bag>
  </class>
</hibernate-mapping>

Problem: "Koppelid" both in Key property from the bag as in the property.

Edit:

The LanguageSkill mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="DomainLayer.Person" assembly="Test">
  <class name="LanguageSkill" table="languageskill" lazy="false">
    <id name="Id" type="Int32">
      <generator class="identity" />
    </id>
    <property name="ConnectionId" column="`KoppelId`" type="Int64"  />
    <property name="Remark" column="`Opmerking`" type="string"  />
    <property name="Source" column="`CdOorsprong`" type="string"  />
    <property name="MotherTongue" column="`moedertaal`" type="boolean"  />
    <property name="ModifiedDate" column="`wyzdat`" type="DateTime"  />
  </class>
</hibernate-mapping>

Upvotes: 0

Views: 6154

Answers (2)

Firo
Firo

Reputation: 30813

the reason is actually that the column is defined twice in the LanguageSkillMapping. Since you mapped the column you could map it as reference and set the bag to inverse

<bag name="Languages" inverse="true" lazy="false" mutable="false" access="field.camelcase-underscore">
  <key property-ref="ConnectionId" column="Koppelid" />
  <one-to-many class="DomainLayer.Person.LanguageSkill, Test" />
</bag>


<many-to-one name="SearchPerson" column="`KoppelId`" />

Update: nevermind, Stefan got it

Upvotes: 0

Stefan Steinegger
Stefan Steinegger

Reputation: 64628

It should actually work. It may be because you have marked it with ` in one case, but not in the other.

Try this:

<bag ...>
    <key property-ref="ConnectionId" column="`Koppelid`" />

Upvotes: 1

Related Questions