0

The exception is:

Could not compile the mapping document: WindowsFormsApplication2.Products.hbm.xml

and the inner exception is:

Persistent class Sample.CustomerService.Domain.Products, Sample.CustomerService. Domain not found

my configuration is:

config file ,i could not paste here because of error ,i did not accept all code hare so i put a link here

and this is mapping file.

And class for mapping is

namespace Sample.CustomerService.Domain
{
    public class Product
    {
        public virtual int Productid { get; set; }
        public virtual string Name { get; set; }
    }
}

and session factory is

public sealed class SessionFactory
{       
    private static volatile ISessionFactory iSessionFactory;
    private static object syncRoot = new object();

    public static ISession OpenSession
    {
        get
        {
            if (iSessionFactory == null)
            {
                lock (syncRoot)
                {
                    if (iSessionFactory == null)
                    {
                        Configuration configuration = new Configuration();
                        configuration.AddAssembly(Assembly.GetCallingAssembly());
                        iSessionFactory = configuration.BuildSessionFactory();
                    }
                }
            }
            return iSessionFactory.OpenSession();
        }
    }
} 

when i try to get record by this code

using (ISession session = SessionFactory.OpenSession)
{
    IQuery query = session.CreateQuery("FROM Products");
    IList<Products> pInfos = query.List<Products>();
    dgView.DataSource = pInfos;
}

it gives me error could not compile mapping document as explained above. i am new to hibernate, i added hibernte from nugget.org by manage nugget pcakage option in visual studio 2012, it added two dlls nhhibernate and Iesi.collections. please help me fix this error, i gave all info in my knowledge.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
BR BHARDWAJ
  • 399
  • 2
  • 17
  • Please don't insert images. Insert **the code** instead. – abatishchev Apr 29 '15 at 04:00
  • See http://www.dofactory.com/net/singleton-design-pattern#net for (much) better singleton implementation. – abatishchev Apr 29 '15 at 04:03
  • Please be aware that Hibernate (for Java) is not the same as NHibernate (for .Net) and use the correct name please. – Oskar Berggren May 08 '15 at 00:38
  • i know its mistake actually i wanted to say nhibernate. i am thinking to start with fluent nhibernate ,because i am fed up of non resolving errors in mapping which makes nhibernate difficult ,writing xml then get it to work is so painful. – BR BHARDWAJ May 08 '15 at 05:52

2 Answers2

2

In your mapping you have Products, and your class is named Product (in singular). So it should be:

<class name="Product" table="Products" lazy="true">

BTW, lazy is default for nhibernate so you can omit it in your mapping. Also if for any chance the table name is the same as the class name you can omit it too, it is a good practice to keep the mapping file content at minimun necesary.

E-Bat
  • 4,792
  • 1
  • 33
  • 58
  • after changing class name to products ,getting same error, actually it might have changed while pasting here somehow but i have same class names in xml file and cs.file – BR BHARDWAJ Apr 29 '15 at 15:04
  • i have generated this mapping file by cubrid nmg tool. – BR BHARDWAJ Apr 29 '15 at 15:06
  • problem was with wrong assembly name in mapping file,i randomly gave assembly name this was the problem i guess, i changed to same assembly name at all places it worked then. still i dont know what should be assembly name in mapping fie,i just renamed to windowsapplication2 to all name space.| can i have different name space in form1.cs and mapping files? – BR BHARDWAJ Apr 29 '15 at 17:21
  • For nhibernate, the only assembly that matter is where you define your persistent classes. Assembly name are defined in Project Properties page, do you have problem setting or reading this? – E-Bat Apr 29 '15 at 18:03
0

What we would need is mapping like this:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="Sample.CustomerService.Domain" 
    assembly="Sample.CustomerService.Domain"> // expecting same as namespace hre 
    // as the xml sais: class name (C# class name) is Product, not Products
    <class name="Product" table="Products" lazy="true" batch-size="25" >
        // I would also from the beginning suggest to use batch-size="25"

        // name must be equal to C# name, including case sensitivity
        <id name="Productid" column="Product_ID" class="native" />

        // property could be expressed without the column, 
        // if C# name and column name are the same
        <property  not-null="false"  name="Name" />

    </class>    
</hibernate-mapping>

NOTE: Check more about batch fetching here (the batch-size="25" details)

Now, if want to query this - using HQL, we should know, that HQL is built on top of our C# objects. We have to use their exact names:

Chapter 14. HQL: The Hibernate Query Language

NHibernate is equiped with an extremely powerful query language that (quite intentionally) looks very much like SQL. But don't be fooled by the syntax; HQL is fully object-oriented, understanding notions like inheritence, polymorphism and association.

14.1. Case Sensitivity

Queries are case-insensitive, except for names of .NET classes and properties. So SeLeCT is the same as sELEct is the same as SELECT but Eg.FOO is not Eg.Foo and foo.barSet is not foo.BARSET.

And also (I would append) the singular vs plural must fit. So because we have:

public class Product
{
  ...

We must query that like this (suprised that you were able to build that, because Products are not in the code, just Product).

using (ISession session = SessionFactory.OpenSession)
{
    //IQuery query = session.CreateQuery("FROM Products");
    var query = session.CreateQuery("FROM Product"); // just Product

    // here again
    //IList<Products> pInfos = query.List<Products>();
    IList<Product> pInfos = query.List<Product>();
    ...
Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • after changing class name to products ,getting same error, actually it might have changed while pasting here somehow but i have same class names in xml file and cs.file i have generated this mapping file by cubrid nmg tool. i am posting intellitrace while debugging;;http://www.use.com/CKGPI – BR BHARDWAJ Apr 29 '15 at 16:51
  • problem was with wrong assembly name in mapping file,i randomly gave assembly name this was the problem i guess, i changed to same assembly name at all places it worked then. still i dont know what should be assembly name in mapping fie,i just renamed to windowsapplication2 to all name space.| can i have different name space in form1.cs and mapping files? – BR BHARDWAJ Apr 29 '15 at 17:20