I'm unable to get some simple Mapping Attribute code to work. Please help.
Versions:-NHibernate.Mapping.Attributes-for-NHibernate-2.1.0.GA-bin
-NHibernate-2.1.0.GA-bin
-MySQL 5.1.37-community
-Visual Studio 2008
Filesystem structure:Code:
C:\Users\xxxxx\Documents\Visual Studio 2008\Projects\NHAttTest>tree /F
C:.
│ NHAttTest.sln
│
└───NHAttTest
│ hibernate.cfg.xml
│ NHAttTest.csproj
│ Product.cs
│ Program.cs
│
├───bin
│ └───Debug
│ Antlr3.Runtime.dll
│ Castle.Core.dll
│ Castle.Core.xml
│ Castle.DynamicProxy2.dll
│ Castle.DynamicProxy2.xml
│ hibernate.cfg.xml
│ Iesi.Collections.dll
│ Iesi.Collections.xml
│ MySql.Data.dll
│ NHAttTest.exe
│ NHAttTest.pdb
│ NHAttTest.vshost.exe
│ NHAttTest.vshost.exe.manifest
│ NHibernate.ByteCode.Castle.dll
│ NHibernate.ByteCode.Castle.xml
│ NHibernate.dll
│ NHibernate.Mapping.Attributes.dll
│ NHibernate.xml
│ nunit.framework.dll
│ nunit.framework.xml
│
├───obj
│ └───Debug
│ │ NHAttTest.csproj.FileListAbsolute.txt
│ │ NHAttTest.exe
│ │ NHAttTest.pdb
│ │
│ └───TempPE
└───Properties
AssemblyInfo.cs
NHibernate Configuration:Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MySQLDialect</property>
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">Database=test2;Data Source=localhost;User Id=xxxxx;Password=xxxxx</property>
<property name="hbm2ddl.keywords">none</property>
<property name="show_sql">true</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
</session-factory>
</hibernate-configuration>
EntityCode:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Mapping.Attributes;
namespace NHAttTest {
[Class]
public class Product {
[Id]
public virtual int Id { get; set; }
}
}
Test code:Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NUnit.Framework;
using NHibernate.Mapping.Attributes;
using NHibernate.Tool.hbm2ddl;
namespace NHAttTest {
[TestFixture]
public class Program {
public static void Main(String[] args) {
Start();
}
[Test]
public static void Start() {
Configuration cfg = new Configuration();
cfg
.Configure()
.AddInputStream(HbmSerializer.Default.Serialize(typeof(Product).Assembly));
}
}
}
Error message:Code:
------ Test started: Assembly: NHAttTest.exe ------
TestCase 'NHAttTest.Program.Start'
failed: NHibernate.MappingException : Could not compile the mapping document: (unknown)
----> System.NullReferenceException : Object reference not set to an instance of an object.
at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
at NHibernate.Cfg.Configuration.ProcessMappingsQueue()
at NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document)
at NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name)
at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream)
C:\Users\xxxxxx\Documents\Visual Studio 2008\Projects\NHAttTest\NHAttTest\Program.cs(21,0): at NHAttTest.Program.Start()
--NullReferenceException
at NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindClass(XmlNode node, IDecoratable classMapping, PersistentClass model, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(XmlNode node, HbmClass classSchema, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(XmlNode parentNode, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(XmlNode node)
at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
0 passed, 1 failed, 0 skipped, took 4,74 seconds (NUnit 2.5).
The documentation of NHibernate 2.1.0.GA says:
Quote:
Important breaking change: When using the [*Class] attributes, the property Name must now be manually specified.
This is due to the fact that this property is optional since you can use EntityName instead
So I tried changing
Code:
[Class]
public class Product {
....
to
Code:
[Class(Name="NHAttTest.Product")]
public class Product {
....
which will give the error:
Code:
failed: NHibernate.MappingException : Could not compile the mapping document: (unknown)
----> NHibernate.MappingException : persistent class NHAttTest.Product not found
----> System.TypeLoadException : Could not load type NHAttTest.Product. Possible cause: no assembly name specified.
and changing it to
Code:
[Class(Name="Product")]
public class Product {
....
will give
Code:
failed: NHibernate.MappingException : Could not compile the mapping document: (unknown)
----> NHibernate.MappingException : persistent class Product not found
----> System.TypeLoadException : Could not load type Product. Possible cause: no assembly name specified.
Can somebody tell me the problem?