Hallo,
Ich arbeite mich gerade in NHibernate ein. Dazu habe ich eine simple Klasse in .NET C# geschrieben. Jedoch erhalte ich eine MappingException, sobald ich ein AddAssembly ausführe. Es deutet ja auf einen Fehler in der Mapping-file hin, denke ich. Die Eigeschaft Buildvorgang der Mappingfile steht auf Eingebettete Ressource. Vielleicht kann mir jemand helfen? Hier die Daten:
Mappingfile (Person.hbm.xml):
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="Person, NHibernate" lazy="false">
<id name="id" access="field">
<generator class="native" />
</id>
<property name="name" access="field" column="name"/>
<many-to-one access="field" name="parent" column="parent" cascade="all"/>
</class>
</hibernate-mapping>
Klasse:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NHibernate
{
class Person
{
public int Id { get; set; }
public String Name { get; set; }
public Person Parent { get; set; }
public int OrderNumber { get; set; }
public Person Branch { get; set; }
public Person()
{}
public Person(String name, Person parent)
{
Name = name;
Parent = parent;
}
public bool IsRoot()
{
return null == Parent ? true : false;
}
public Person GetRoot()
{
Person root = Parent;
while (root.Parent != null)
{
root = root.Parent;
}
return root;
}
public Person Add(Person child)
{
child.Parent = this;
Person ancestor = this;
while (ancestor.Parent != null)
{
ancestor.Parent = ancestor.Parent.Parent;
}
child.Branch = ancestor;
return child;
}
public void Delete()
{
Name = null;
Parent = null;
}
}
}
Program.cs:
Code:
using System;
using System.Collections.Generic;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;
namespace NHibernate
{
class Program
{
static ISessionFactory factory;
static void Main(string[] args)
{
CreateFamiliy();
}
static void CreateFamiliy()
{
Person Adam = new Person("Adam", null);
Person Peter = new Person("Peter", Adam);
Person Jack = new Person("Jack", Peter);
Person Charly = new Person("Charly", Adam);
Person Tom = new Person("Tom", Charly);
Person Mike = new Person("Mike", Tom);
using (ISession session = OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(Adam);
transaction.Commit();
}
}
}
static ISession OpenSession()
{
ISession result = null;
if (factory == null)
{
try
{
Configuration c = new Configuration().Configure();
c.AddAssembly(typeof(Person).Assembly);
factory = c.BuildSessionFactory();
result = factory.OpenSession();
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException.Message);
return null;
}
}
return result;
}
}
}