user2547522
user2547522

Reputation: 120

NHibernate.MappingException Could not determine type for: Entity

i have a asp.net aplication and add a library with my domains objects and xml files.

the trace is:

Message=Could not determine type for: VaLibrary.Core.Domain.VAEmpresa, VaLibrary,    Version=1.0.5.18335, Culture=neutral, PublicKeyToken=null, for columns:    NHibernate.Mapping.Column(IdDev)

Source=NHibernate

i search in the web same error, but, solutions are for other mapping options (fluent, castle, etc.) not for xml

my Code class is here

using System;
   using System.Text;
   using System.Collections.Generic;
   namespace VaLibrary.Core.Domain {

    public class VAEmpresa {
        private int? _id;
        private string _nombre;
        private string _tipo;
        public VAEmpresa() {
            VAJu_Dev = new List<VAJu>();
            VAJu_Pub = new List<VAJu>();
        }
        public virtual int Id {
            get {
                return (int)this._id;
            }
            set {
                this._id = value;
            }
        }
        public virtual string Nombre {
            get {
                return this._nombre;
            }
            set {
                this._nombre = value;
            }
        }
        public virtual string Tipo {
            get {
                return this._tipo;
            }
            set {
                this._tipo = value;
            }
        }
        public virtual IList<VAJu> VAJu_Dev { get; set; }
        public virtual IList<VAJu> VAJu_Pub { get; set; }
    }
}

and reference is:

using System;
using System.Text;
using System.Collections.Generic;
namespace VaLibrary.Core.Domain {

    public class VAJu {
        private int? _id;
        private VAEmpresa _idDev;
        private VAEmpresa _idPub;
        private VACon _vACon;
        private string _titulo;
        private int _numerop;
        public VAJu() {
            VApllist = new List<VApllist>();
        }
        public virtual int Id {
            get {
                return (int)this._id;
            }
            set {
                this._id = value;
            }
        }
        public virtual VAEmpresa IdDev {
            get {
                return this._idDev;
            }
            set {
                this._idDev = value;
            }
        }
        public virtual VAEmpresa IdPub {
            get {
                return this._idPub;
            }
            set {
                this._idPub = value;
            }
        }
        public virtual VACon VACon {
            get {
                return this._vACon;
            }
            set {
                this._vACon = value;
            }
        }
        public virtual string Titulo {
            get {
                return this._titulo;
            }
            set {
                this._titulo = value;
            }
        }
        public virtual int Numerop {
            get {
                return this._numerop;
            }
            set {
                this._numerop = value;
            }
        }
        public virtual IList<VApllist> VApllist { get; set; }
    }
}

the mappings files are:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="VaLibrary" namespace="VaLibrary.Core.Domain" xmlns="urn:nhibernate-mapping-2.2">
  <class name="VAEmpresa" table="VA-Empresa" lazy="true" >
    <id name="Id" column="Id">
      <generator class="identity" />
    </id>
    <property name="Nombre">
      <column name="nombre" sql-type="varchar" not-null="true" />
    </property>
    <property name="Tipo">
      <column name="tipo" sql-type="varchar" not-null="false" />
    </property>
    <bag name="VAJu_Dev" inverse="true" cascade="none">
      <key column="IdDev" />
      <one-to-many class="VaLibrary.Core.Domain.VAJu, VaLibrary" />
    </bag>
    <bag name="VAJu_Pub" inverse="true" cascade="none">
      <key column="IdPub" />
      <one-to-many class="VaLibrary.Core.Domain.VAJu, VaLibrary" />
    </bag>
  </class>
</hibernate-mapping>

and

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="VaLibrary" namespace="VaLibrary.Core.Domain" xmlns="urn:nhibernate-mapping-2.2">
  <class name="VAJu" table="VA-Ju" lazy="true" >
    <id name="Id" column="Id">
      <generator class="identity" />
    </id>
    <property name="Titulo">
      <column name="Titulo" sql-type="varchar" not-null="true" />
    </property>
    <many-to-one insert="false" update="false" lazy="false" name="IdDev">
      <column name="IdDev" sql-type="int" not-null="true" />
    </many-to-one>
    <property name="IdDev">
      <column name="IdDev" sql-type="int" not-null="true" />
    </property>
    <many-to-one insert="false" update="false" lazy="false" name="IdPub">
      <column name="IdPub" sql-type="int" not-null="true" />
    </many-to-one>
    <property name="IdPub">
      <column name="IdPub" sql-type="int" not-null="true" />
    </property>  
    <property name="Numerop">
      <column name="numero-p" sql-type="int" not-null="true" />
    </property>
    <bag name="VApllist" inverse="true" cascade="none">
      <key column="IdJu" />
      <one-to-many class="VApllist" />
    </bag>
  </class>
</hibernate-mapping>

the relantionship in DB is: Va-Ju.IdDev FK to VA-Empresa.Id PK Va-JU.IdPub FK to VA-Empresa.Id PK

i try to change for but is the same.

I can do?

thank you very much for your answers

[Edit]

i use Nhibernate 3.2

Upvotes: 2

Views: 8015

Answers (2)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123861

The mapping is missing the correct relational (object to object) mapping. The best way how to explain that, is the example:

Change this:

private VAEmpresa _idDev;

public virtual VAEmpresa IdDev 
{
  get { return this._idDev; }
  set { this._idDev = value; }
}

Into this:

private VAEmpresa _dev;

public virtual VAEmpresa Dev  // Reference Object
{
  get { return this._idDev; }
  set 
  { 
      this._idDev = value; 
      if(_idDev != null)      // assure that with reference change
      {                       // the referenceId is changed as well
          IdDev = _idDev.Id
      }
  }
}
public virtual int IdDev { get; set; } // Refernece Id

Now we have two representations of the Dev reference (which is of type VAEmpresa and has int id). And we can map it working way. So

Change this:

<many-to-one insert="false" update="false" lazy="false" name="IdDev">
  <column name="IdDev" sql-type="int" not-null="true" />
</many-to-one>
<property name="IdDev">
  <column name="IdDev" sql-type="int" not-null="true" />
</property>

Into that:

<many-to-one name="Dev" class="VAEmpresa" column="IdDev"
         insert="false" update="false" lazy="false" />
<property name="IdDev" not-null="true" />

From that moment, NHibernate will know where to search for Dev property in DB. It will be column IdDev and class to instantiate is VAEmpresa

If the code requires just the referenceId IdDev ... we also have it.

SMALL NOTE: my experience is to use different readonly setting. The editable is reference, readonly is referencId. That forces the code to work with existing VAEmpresa objects, while IdDev could be any integer. Handling is complicated, but brings fruits later

Upvotes: 2

rgrano
rgrano

Reputation: 351

The ID may require a type. You have:

<id name="Id" column="Id">
  <generator class="identity" />
</id>

Try specifying a type as follows:

<id name="Id" >
  <column name="Id" sql-type="int" not-null="true" unique="true" />
  <generator class="identity" />
</id>

Another problem is you forgot to put the name of the columns / domains in the bag property. You need to put the actual column name and not the property names. Let me show you for VAJu:

 <bag name="VApllist" inverse="true" cascade="none">
  <key column="Id"  />
  <one-to-many class="VaLibrary.Core.Domain.VApllist" column="Id"  />
 </bag>

Upvotes: 0

Related Questions