0

I am trying to map a SQL Server database with nHibernate that is full of tables with varchar primary keys that are generated by external software and I need update/read (no insert) access.

I cannot find a way to get past the following error:

XXXX.Tests.GMCRepository_Fixture.Can_get_existing_GMC_by_parameter'
failed: NHibernate.MappingException :
XXXX.Domain.Mappings.GMC2.hbm.xml(4,6): XML validation error: The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'property' in     namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'meta, subselect, cache, synchronize, comment, tuplizer, id, composite-id' in namespace 'urn:nhibernate-    mapping-2.2'.

Research has suggested this error is relating to there not being a valid primary key (id) defined.

Mapping XML looks like:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="XXXX.Domain" namespace="XXXX.Domain" xmlns="urn:nhibernate-mapping-2.2" schema="GM.dbo">
  <class name="GMC2" table="C2" lazy="true" >
    <property name="PARAMETER">
      <column name="PARAMETER" sql-type="varchar" not-null="true" />
    </property>
    ...
    <id name="Recid">
      <column name="recid" sql-type="varchar" not-null="true" />
    </id>
  </class>
</hibernate-mapping>

Thanks for your help!

StigM
  • 711
  • 6
  • 12

1 Answers1

1

I believe that the convention is to have the ID mapping as the first thing under the class declaration. Also as part of the Id mapping you need to specify the Generator of the ID. In your case I think you will need the assigned generator added to your ID mapping. Your class mapping will look something like this.

<?xml version="1.0" encoding="utf-8"?> 
<hibernate-mapping assembly="XXXXCRMAPI.Domain" namespace="XXXXCRMAPI.Domain" xmlns="urn:nhibernate-mapping-2.2" schema="GoldMine.dbo"> 
  <class name="GMContact2" table="CONTACT2" lazy="true" > 
    <id name="Recid"> 
      <generator type="assigned" />
      <column name="recid" sql-type="varchar" not-null="true" /> 
    </id> 
    <property name="Accountno"> 
      <column name="ACCOUNTNO" sql-type="varchar" not-null="true" /> 
    </property> 
    ... 

  </class> 
</hibernate-mapping>
Nathan Fisher
  • 7,961
  • 3
  • 47
  • 68