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.  [ 11 posts ] 
Author Message
 Post subject: Pls help with NHibernate. Could not compile the mapping doc.
PostPosted: Mon May 18, 2009 3:34 am 
Newbie

Joined: Fri Mar 13, 2009 10:23 am
Posts: 10
Hi all,

I'm doing a program that saves customer details to the database and I'm using NHibernate. I'm very new in it and I'm getting help from the net. I have a problem compiling the mapping file, I get the following error:
"Could not compile the mapping document: Customer.hbm.xml"

I don't have an idea of what I did wrong. Please, if you know what I missed or did wrong, I'd really appreciate your help/advice. Thanks.

hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate_Demo" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NH">
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name="connection.driver_class">NHibernate.Driver.MsSql2005Dialect</property>
<property name="connection.connection_string">Server=D9X3KQ3J\SQLEXPRESS; Initial Catalog=NHibernateTest; Integrated Security=SSPI;</property>
<property name="connection.release_mode">on_close</property>
<property name="show_sql">true</property>

<property name="use_outer_join">true</property>
<!-- mapping files -->
<!-- <mapping resource="User.hbm.xml" assembly="MysqlHibernet"/> -->
<!-- <mapping resource="Products.hbm.xml" assembly="MysqlHibernet"/> -->
<mapping assembly="NHibernate" />

</session-factory>
</hibernate-configuration>
</configuration>

Customer.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!--<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"> -->

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate" namespace="NHibernate_Demo" >

<class name="NHibernate_Demo.Customer, NHibernate_Demo"
table="Customers" lazy="false">
<id name="CustomerID" type="String" length="5">
<generator class="assigned" />
</id>
<property name="CompanyName" column="CompanyName" type="String" length="40"/>
<property name="ContactName" column="ContactName" type="String" length="30"/>
<property name="ContactTitle" column="ContactTitle" type="String" length="30"/>
<property name="Phone" column="Phone" type="String" length="24"/>

</class>
</hibernate-mapping>

Here is my class:

using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Cfg;

namespace NHibernate_Demo
{

class Program
{
static void Main(string[] args)
{

// Loads the NHibernate Types to prepare for Serialization
Configuration cfg = new Configuration();
cfg.Configure();

//cfg.AddAssembly(typeof(NHibernate_Demo.Customer).Assembly);
//cfg.AddClass(typeof(Customer));
cfg.AddFile("Customer.hbm.xml");

//Opens a session to NHiberbate to allow us to work with objects
ISessionFactory sessionsF = cfg.BuildSessionFactory();

//let ISessiionFactory open connection
ISession sessionS = sessionsF.OpenSession();

ITransaction transaction = sessionS.BeginTransaction();
{
Customer customer = (Customer)
sessionS.Load(typeof(Customer), "ALFKI");

// Show the Contact Name of the Customer
//MessageBox.Show(customer.ContactName);

sessionS.Close();
}


}

}

}


I get an error at this line:

cfg.AddFile("Customer.hbm.xml");

Thanks.


Top
 Profile  
 
 Post subject: Re: Pls help with NHibernate. Could not compile the mapping doc.
PostPosted: Mon May 18, 2009 5:52 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Does the exception have an inner exception ? What does it say ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Pls help with NHibernate. Could not compile the mapping doc.
PostPosted: Mon May 18, 2009 6:08 am 
Newbie

Joined: Fri Mar 13, 2009 10:23 am
Posts: 10
The inner exception says:

{"There is an error in XML document (1, 2)."}


Top
 Profile  
 
 Post subject: Re: Pls help with NHibernate. Could not compile the mapping doc.
PostPosted: Mon May 18, 2009 7:12 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Is there a line break before <?xml version="1.0" encoding="utf-8"?> ? This has to be the first line !

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Pls help with NHibernate. Could not compile the mapping doc.
PostPosted: Mon May 18, 2009 8:31 am 
Newbie

Joined: Fri Mar 13, 2009 10:23 am
Posts: 10
No, there's no line break.


Top
 Profile  
 
 Post subject: Re: Pls help with NHibernate. Could not compile the mapping doc.
PostPosted: Mon May 18, 2009 8:23 pm 
Newbie

Joined: Mon May 18, 2009 7:23 pm
Posts: 7
1) Did you set the build action to include the XML file as an embedded resource?

2) If you did, can you post your code for the actual customer class, not only is NHibernate very case-sensitive, but some other detail like a property being private or having no setter or something else may be the issue.


Top
 Profile  
 
 Post subject: Re: Pls help with NHibernate. Could not compile the mapping doc.
PostPosted: Tue May 19, 2009 2:20 am 
Newbie

Joined: Fri Mar 13, 2009 10:23 am
Posts: 10
How do i set Build Action to include XML file as an embedded resources? I don't know most of the things in this Nhibernate concept as I am a new in it.

Here is my Customer.cs class:

using System;
using System.Collections.Generic;
using System.Text;

namespace NHibernate_Demo
{
class Customer
{
public class Customers
{
string _customerID;

public string CustomerID
{
get { return _customerID; }
set { _customerID = value; }
}
string _companyName;

public string CompanyName
{
get { return _companyName; }
set { _companyName = value; }
}
string _contactName;

public string ContactName
{
get { return _contactName; }
set { _contactName = value; }
}
string _contactTitle;

public string ContactTitle
{
get { return _contactTitle; }
set { _contactTitle = value; }
}
string _phone;

public string Phone
{
get { return _phone; }
set { _phone = value; }
}
string _altPhone;

public string AltPhone
{
get { return _altPhone; }
set { _altPhone = value; }
}

}

}
}


Top
 Profile  
 
 Post subject: Re: Pls help with NHibernate. Could not compile the mapping doc.
PostPosted: Tue May 19, 2009 2:36 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Do ou get the same error, when you skip the AddFile ? What is happening when you use one of the other methods:

Code:
//cfg.AddAssembly(typeof(NHibernate_Demo.Customer).Assembly);
//cfg.AddClass(typeof(Customer));
cfg.AddFile("Customer.hbm.xml");

//Opens a session to NHiberbate to allow us to work with objects
ISessionFactory sessionsF = cfg.BuildSessionFactory();


You can set the build action when you right click the file in the solution explorer. In the "Advanced" part you have a property called "Build Action". You have to set it to "Embedded Resource" for all hbm.xml files if you want to use AddClass or AddAssembly. If you use AddFile, afaik it's not necessary.

Something else. NHibernate_Demo is the wrong assembly name here, should be NHibernate !

Code:
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate_Demo" />


If you're new to NHibernate, try this:

http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application.aspx

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Pls help with NHibernate. Could not compile the mapping doc.
PostPosted: Tue May 19, 2009 2:57 am 
Newbie

Joined: Fri Mar 13, 2009 10:23 am
Posts: 10
I've set the build action to include the XML file as an embedded resource just like you adviced me. I commented out the other methods and left only the AddAssembly one:

cfg.AddAssembly(typeof(NHibernate_Demo.Customer).Assembly);
//cfg.AddClass(typeof(Customer));
//cfg.AddFile("Customer.hbm.xml");

then I get the different error:
Could not find a getter for property 'CustomerID' in class
'NHibernate_Demo.Customer'


I checked it on my Customer class and its there,

string _customerID;
public string CustomerID
{
get { return _customerID; }
set { _customerID = value; }
}

I'm not sure why its complaining about it, maybe I've done something wrong somewhere.


Top
 Profile  
 
 Post subject: Re: Pls help with NHibernate. Could not compile the mapping doc.
PostPosted: Tue May 19, 2009 3:33 am 
Newbie

Joined: Mon May 18, 2009 7:23 pm
Posts: 7
It looks to me like you have a class "Customer" and then another class "Customers" nested inside.

So, NHibernate is looking for the properties in your customer class and they don't exist: they are declared in the nested class Customers.


Top
 Profile  
 
 Post subject: Re: Pls help with NHibernate. Could not compile the mapping doc.
PostPosted: Tue May 19, 2009 4:14 am 
Newbie

Joined: Fri Mar 13, 2009 10:23 am
Posts: 10
Thanks a lot twesterd,
Yes, I had two classes declared. I got all sorted out now.

One problem I have now is with the database. I have a database name NHibernateTest and that is where I want my Customer deatils to be saved but I didn't create any table. I dont know whether I should creat it myself manually or Nhibernate will create it for me since everything is being declared in Customer.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate" namespace="NHibernate_Demo" >
<class name="NHibernate_Demo.Customer, NHibernate_Demo"
table="Customers" lazy="false">
<id name="CustomerID" type="String" length="5">
<generator class="assigned" />
</id>
<property name="CompanyName" column="CompanyName" type="String" length="40"/>
<property name="ContactName" column="ContactName" type="String" length="30"/>
<property name="ContactTitle" column="ContactTitle" type="String" length="30"/>
<property name="Phone" column="Phone" type="String" length="24"/>
<property name="AltPhone" column="AltPhone" type="String" length="24"/>

</class>
</hibernate-mapping>

When running the project now, I get the following error:

{"could not load an entity: [NHibernate_Demo.Customer#ALFKI][SQL: SELECT customer0_.CustomerID as CustomerID0_0_, customer0_.CompanyName as CompanyN2_0_0_, customer0_.ContactName as ContactN3_0_0_, customer0_.ContactTitle as ContactT4_0_0_, customer0_.Phone as Phone0_0_, customer0_.AltPhone as AltPhone0_0_ FROM Customers customer0_ WHERE customer0_.CustomerID=?]"}

InneException: {"Invalid object name 'Customers'."}

Please, can you advice on what I should do concerning the database.

Thanks.


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