3

I'm trying to implement unit tests for my NHibernate data access layer. The first test, which I drew from an example I found on the web (http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/01/your-first-nhibernate-based-application.aspx), is just trying to recreate the database using my domain classes/mappings. I've been able to get this sample working in C# (Product table is created in database) but not when I implement it in VB.NET.

I have two projects, Todd.Core (containing Product class and Product.hbm.xml mapping) and Todd.Core.Test (containing Test Fixture and NHibernate configuration). When I attempt to run this test using MBUnit GUI I get this message (line 10 is the call to the .Configure method):

Message: Could not compile the mapping document: Todd.Core.Product.hbm.xml

Type: NHibernate.MappingException
Source: NHibernate
TargetSite: Void LogAndThrow(System.Exception)
HelpLink: null
Stack:   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.AddResource(String path, Assembly assembly)
   at NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly)
   at NHibernate.Cfg.Configuration.AddAssembly(String assemblyName)
   at NHibernate.Cfg.Configuration.DoConfigure(IHibernateConfiguration hc)
   at NHibernate.Cfg.Configuration.Configure()
   at Todd.Core.Test.GenerateSchema_Fixture.Can_generate_schema() in C:\Development\Todd\Todd.Core.Test\GenerateSchema_Fixture.vb:line 10

Any ideas appreciated. Below is my code….

My Product class:

Namespace Todd.Core

    Public Class Product
        Private _id As Guid
        Private _name As String
        Private _category As String
        Private _discontinued As Boolean

        Public Overridable Property Id() As Guid
            Get
                Return _id
            End Get
            Set(ByVal value As Guid)
                _id = value
            End Set
        End Property
        Public Overridable Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property
        Public Overridable Property Category() As String
            Get
                Return _category
            End Get
            Set(ByVal value As String)
                _category = value
            End Set
        End Property
        Public Overridable Property Discontinued() As Boolean
            Get
                Return _discontinued
            End Get
            Set(ByVal value As Boolean)
                _discontinued = value
            End Set
        End Property
    End Class
End Namespace

My Product.hbm.xml file:

<?xml version="1.0" encoding="utf-8" ?>
  <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Todd.Core.Product, Todd.Core" table="Product">
    <id name="Id">
      <generator class="guid" />
    </id>
    <property name="Name" />
    <property name="Category" />
    <property name="Discontinued" />
  </class>
</hibernate-mapping>

My test fixture:

Imports MbUnit.Framework
Imports Todd.Core

<TestFixture()> _
Public Class GenerateSchema_Fixture

    <Test()> _
    Public Sub Can_generate_schema()
        Dim cfg As New NHibernate.Cfg.Configuration
        cfg.Configure()
        cfg.AddAssembly(GetType(Todd.Core.Product).Assembly)
        Dim exp As NHibernate.Tool.hbm2ddl.SchemaExport = New NHibernate.Tool.hbm2ddl.SchemaExport(cfg)
        exp.Execute(True, True, False, True)
    End Sub

End Class

My app.config (from Test project):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration"
      type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>
      <property name="connection.connection_string">data source=.\SQLEXPRESS;Initial Catalog=NHibernateTestDB;Integrated Security=SSPI</property>
      <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
      <property name="show_sql">true</property>
      <mapping assembly="Todd.Core" />
    </session-factory>
  </hibernate-configuration>
</configuration>
5
  • Is your hbm marked as Embedded Resource? Jul 1, 2009 at 16:50
  • Yes, my hbm is marked as Embedded Resource. Jul 1, 2009 at 16:54
  • try removing the <mapping> from the app.config Jul 1, 2009 at 17:02
  • Thanks, removing the <mapping> allows the test to run. But, WHY? The <mapping> element did adversely affect the C# version of my code. Jul 1, 2009 at 17:13
  • Excuse me, did NOT adversely affect the C# version Jul 1, 2009 at 17:14

1 Answer 1

0

The section declares the name of an assembly containing class declarations and mapping files. The mapping files contain the metadata for the mapping of the POCO class to a database table (or multiple tables).

On the other hand you've got

cfg.AddAssembly(GetType(Todd.Core.Product).Assembly)

in your "GenerateSchema_Fixture". This means that NHibernate will load all of the mapping files contained in an Assembly.

You've to decide which way you want to provide the information for NHibernate.

If you want to do everything programmatically, I would sugest the use FluentNHibernate. You declaring everything over a fluent interface and don't have to think about XML files. (fluentnhibernate.org).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.