These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Getting Mapping Attributes to work
PostPosted: Wed Sep 23, 2009 8:18 am 
Newbie

Joined: Tue Dec 18, 2007 7:42 am
Posts: 19
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>


Entity
Code:
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?


Top
 Profile  
 
 Post subject: Re: Getting Mapping Attributes to work
PostPosted: Fri Oct 09, 2009 4:45 am 
Newbie

Joined: Thu Oct 08, 2009 1:19 pm
Posts: 1
Hi there,

I had pretty much an identical problem to yours and solved it with the following code.

Domain Object:

Code:
using System;
using NHibernate.Mapping.Attributes;

namespace FirstSolution.Domain
{
   [Class(Name="FirstSolution.Domain.Product, FirstSolution")]
   public class Product
   {
      [Id(Name="Id")]
      public virtual Guid Id { get; set; }
      [Property(Name="Name")]
      public virtual string Name { get; set; }
      [Property(Name="Category")]
      public virtual string Category { get; set; }
      [Property(Name="Discontinued")]
      public virtual bool Discontinued { get; set; }
   }
}


Session factory initialisation:

Code:
[TestFixtureSetUp]
      public void TestFixtureSetUp()
      {
         MemoryStream stream = new MemoryStream();
         HbmSerializer.Default.Validate = true;
         HbmSerializer.Default.Serialize(stream, typeof(Product).Assembly);
         stream.Position = 0;
         _configuration = new Configuration();
         _configuration.Configure();
         _configuration.AddInputStream(stream);
         _sessionFactory = _configuration.BuildSessionFactory();
      }


I think the solution lies with specifying the full type name and assembly in the Class attribute.

Hope this helps.

Cheers,
Godders.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.