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.  [ 9 posts ] 
Author Message
 Post subject: Difficulty with simple NHibernate App
PostPosted: Mon Oct 09, 2006 5:16 pm 
Newbie

Joined: Wed Sep 27, 2006 4:07 pm
Posts: 9
I have created a small NHibernate app in Visual Studio 2005, using NHibernate 1.2.0B1. I'm using NHibernate.Mapping.Attributes, and the following code throws a MappingException:

Code:
  class Program
  {
    static void Main(string[] args)
    {
      Configuration cfg = new Configuration()
        .AddAssembly("ConsoleApplication2");

      cfg.AddInputStream(NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(
         System.Reflection.Assembly.GetExecutingAssembly()));


      ISessionFactory factory = cfg.BuildSessionFactory();
      ISession session = factory.OpenSession();
    }
  }


I am new to NHibernate and am probably missing something obvious.

I have one persistent class, Sale.cs:

Code:

namespace nextgen.domain
{
  [NHibernate.Mapping.Attributes.Class(Table="Sale")]
  class Sale
  {
    private int saleId;
   

    [NHibernate.Mapping.Attributes.Id(Column="SaleID")]
    [NHibernate.Mapping.Attributes.Generator(Class="Assigned")]
    public int SaleId
    {
      get { return saleId; }
      set { saleId = value; }
    }

    private DateTime saleDate;

    [NHibernate.Mapping.Attributes.Property(Column="SaleDate")]
    public DateTime SaleDate
    {
      get { return saleDate; }
      set { saleDate = value; }
    }
  }
}



Here is my App.config:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
   <section name="nhibernate" type="System.Configuration.NameValueSectionHandler" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"  />
  </configSections>

  <nhibernate>
    <add
        key="hibernate.connection.provider"         
        value="NHibernate.Connection.DriverConnectionProvider"
        />
    <add
        key="hibernate.dialect"
        value="NHibernate.Dialect.MsSql2000Dialect"
        />
    <add
        key="hibernate.connection.driver_class"
        value="NHibernate.Driver.SqlClientDriver"
        />
    <add
        key="hibernate.connection.connection_string"
        value="Server=127.0.0.1; Initial Catalog=thedatabase; Integrated Security=SSPI"
        />
    <add key="hibernate.connection.isolation" value="ReadCommitted"    />
    <add key="hibernate.hbm2ddl.auto" value="create" />
    <add key="hibernate.show_sql" value="true" />

  </nhibernate>


  <!-- This section contains the log4net configuration settings -->
  <log4net>
    <!-- Define an output appender (where the logs can go) -->
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender, log4net">
      <param name="File" value="log.txt" />
      <param name="AppendToFile" value="false" />
      <layout type="log4net.Layout.PatternLayout, log4net">
        <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] &lt;%X{auth}&gt; - %m%n" />
      </layout>
    </appender>

    <!-- Setup the root category, set the default priority level and add the appender(s) (where the logs will go) -->
    <root>
      <priority value="WARN" />
      <appender-ref ref="LogFileAppender" />
    </root>

    <!-- Specify the level for some specific namespaces -->
    <!-- Level can be : ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF -->
    <logger name="NHibernate">
      <level value="INFO" />
    </logger>
  </log4net>

  <!-- other app specific config follows -->
</configuration>


The exact error message:

An unhandled exception of type 'NHibernate.MappingException' occurred in NHibernate.dll

Additional information: Could not compile the mapping document: (unknown)

Thanks in advance for any tips!

_________________
Stephen Schaub


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 12:16 am 
Regular
Regular

Joined: Tue Aug 08, 2006 4:28 am
Posts: 96
Location: Hong Kong
Please try changing

Code:
    [NHibernate.Mapping.Attributes.Id(Column="SaleID")]
    [NHibernate.Mapping.Attributes.Generator(Class="Assigned")]


to

Code:
    [NHibernate.Mapping.Attributes.Id(-1, Column="SaleID")]
    [NHibernate.Mapping.Attributes.Generator(Class="Assigned")]


(set position parameter to -1 in NHibernate.Mapping.Attributes.Id)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 7:27 am 
Newbie

Joined: Wed Sep 27, 2006 4:07 pm
Posts: 9
canton wrote:
Please try changing

Code:
    [NHibernate.Mapping.Attributes.Id(Column="SaleID")]
    [NHibernate.Mapping.Attributes.Generator(Class="Assigned")]


to

Code:
    [NHibernate.Mapping.Attributes.Id(-1, Column="SaleID")]
    [NHibernate.Mapping.Attributes.Generator(Class="Assigned")]


(set position parameter to -1 in NHibernate.Mapping.Attributes.Id)


Thanks for the suggestion. I tried that, but I still get the same error.

_________________
Stephen Schaub


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 7:37 am 
Newbie

Joined: Wed Sep 27, 2006 4:07 pm
Posts: 9
I just added the following to my Main() method to take a look at the generated mapping:

Code:
NHibernate.Mapping.Attributes.HbmSerializer.Default.Validate = true;
NHibernate.Mapping.Attributes.HbmSerializer.Default.Serialize(
          System.Reflection.Assembly.GetExecutingAssembly(), "Foo.txt");


Here's the contents of Foo.txt:
Code:
<?xml version="1.0" encoding="utf-8"?>
<!--Generated from NHibernate.Mapping.Attributes on 2006-10-10 07:34:44Z.-->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="nextgen.domain.Sale, ConsoleApplication2" table="Sale">
    <id column="SaleID">
      <generator class="Assigned" />
    </id>
    <property name="SaleDate" column="SaleDate" />
  </class>
</hibernate-mapping>


Does this reveal any problems?

_________________
Stephen Schaub


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 8:28 am 
Regular
Regular

Joined: Tue Aug 08, 2006 4:28 am
Posts: 96
Location: Hong Kong
I managed to get it to work

Code:
namespace nextgen.domain
{
    [NHibernate.Mapping.Attributes.Class(Table = "Sale", Lazy = false)]
    class Sale
    {
        private int saleId;


        [NHibernate.Mapping.Attributes.Id(-1, Name = "SaleId", Column = "SaleID")]
        [NHibernate.Mapping.Attributes.Generator(Class = "assigned")]
        public int SaleId
        {
            get { return saleId; }
            set { saleId = value; }
        }

        private DateTime saleDate;

        [NHibernate.Mapping.Attributes.Property(Column = "SaleDate")]
        public DateTime SaleDate
        {
            get { return saleDate; }
            set { saleDate = value; }
        }
    }
}


1. Parameter "Name" is needed for Id
2. All properties have to be made virtual or explicitly mark the class Lazy = false
3. Class = "assigned" instead of Class = "Assigned" for Generator

Hope this help


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 10:07 am 
Newbie

Joined: Wed Sep 27, 2006 4:07 pm
Posts: 9
Thank you! That works for me. I appreciate your quick responses.

I mistakenly rated your last post not helpful. If you would reply, I will be glad to rate it helpful.

_________________
Stephen Schaub


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 10, 2006 9:30 pm 
Regular
Regular

Joined: Tue Aug 08, 2006 4:28 am
Posts: 96
Location: Hong Kong
A question. Though we managed to get the code works. We have to know why it works in that way. Fix #2 and #3 are trivial. But why #1?! I suppose parameter Name is optional. In case parameter Name is missing, the serializer should take the property name in the same way as PropertyAttribute. Any clues?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 11, 2006 2:19 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
I'm not an expert in NHMA, but NHibernate allows you to have classes without an identifier property, but having an identifier column. The session then keeps track of the identifier.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 12, 2006 12:04 pm 
Newbie

Joined: Wed Sep 27, 2006 4:07 pm
Posts: 9
sergey wrote:
I'm not an expert in NHMA, but NHibernate allows you to have classes without an identifier property, but having an identifier column. The session then keeps track of the identifier.


True. According to the NHibernate documentation at http://www.hibernate.org/hib_docs/nhibernate/html/mapping.html#mapping-declaration-id, the Name attribute is optional for the <id> element. If omitted, NHibernate assumes that there is no ID property in the class. Since, in practice, you usually have a property in the class that maps to the primary key, you must specify the Name for things to work properly, and it must match the name of the property (including capitalization).

_________________
Stephen Schaub


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.