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.  [ 8 posts ] 
Author Message
 Post subject: Need Help urgently!! MappingException: Unknown Entity Class
PostPosted: Thu Aug 30, 2007 3:15 pm 
Newbie

Joined: Thu Aug 30, 2007 2:50 pm
Posts: 7
Hi, I am trying to create my first nhibernate(ver=1.2) app which the source code I took from the web.

basically, the application consists only 1 class mapped to a table in embedded firebird(ver=2.0) database, below are the files in my application:

-----------------------------User.cs--------------------------
using System;

namespace NHibernate.Examples.Quickstart
{
class User
{
private string id;
private string userName;
private string password;
private string emailAddress;


public User()
{
}

public virtual string Id
{
get { return id; }
set { id = value; }
}

public virtual string UserName
{
get { return userName; }
set { userName = value; }
}

public virtual string Passwords
{
get { return password; }
set { password = value; }
}

public virtual string EmailAddress
{
get { return emailAddress; }
set { emailAddress = value; }
}
}
}

---------------------------app.config---------------------------
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>

<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<!-- Database connection settings -->
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>

<property name="connection.driver_class">
NHibernate.Driver.FirebirdClientDriver
</property>

<property name="connection.connection_string">
Database=User.FDB;
User=sysdba;
Password=masterkey;
ServerType=1
</property>

<property name="dialect">NHibernate.Dialect.FirebirdDialect</property>

<property name="show_sql">true</property>

<property name="cache.provider_class">
NHibernate.Cache.NoCacheProvider
</property>

<mapping file="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>

<log4net debug="false">

<!-- Define some output appenders -->
<appender name="trace"
type="log4net.Appender.TraceAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern"
value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>

<appender name="console"
type="log4net.Appender.ConsoleAppender, log4net">
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern"
value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>

<appender name="rollingFile"
type="log4net.Appender.RollingFileAppender,log4net" >

<param name="File"
value="log.txt" />
<param name="AppendToFile"
value="false" />
<param name="RollingStyle"
value="Date" />
<param name="DatePattern"
value="yyyy.MM.dd" />
<param name="StaticLogFileName"
value="true" />

<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern"
value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>

<!-- Setup the root category, add the appenders and set the default priority -->

<root>
<priority value="WARN" />
<appender-ref ref="console" />
</root>

<logger name="NHibernate.Bytecode.CodeDom">
<priority value="OFF" />
</logger>

<logger name="NHibernate.SQL">
<level value="OFF" />
</logger>

<logger name="NHibernate.Tool.hbm2ddl.SchemaExport">
<level value="WARN" />
</logger>
</log4net>
</configuration>

-------------------------------User.hbm.xml-------------------------
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernate.Examples.Quickstart.User,NHibernate.Examples" table="users">
<id name="Id" column="LogonId" type="String" length="20">
<generator class="assigned" />
</id>
<property name="UserName" column="Name" type="String" length="40"/>
<property name="Passwords" column ="Passwords" type="String" length="20"/>
<property name="EmailAddress" column="EmailAddress" type="String" length="40"/>
</class>
</hibernate-mapping>

-----------------------------UserTest.cs-------------------------------
//This is the main class to test the nhibernate mapping
using System;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using log4net;
using log4net.Config;

namespace NHibernate.Examples.Quickstart
{
class UserTest
{
static void Main(string[] args)
{
XmlConfigurator.Configure();
Configuration cfg = new Configuration();
cfg.Configure();
//new NHibernate.Tool.hbm2ddl.SchemaExport(cfg).Create(true, true);

ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();

User newUser = new User();
newUser.Id = "joe_cool";
newUser.UserName = "Joseph Cool";
newUser.Passwords = "abc123";
newUser.EmailAddress = "joe@cool.com";

// Tell NHibernate that this object should be saved
session.Save(newUser); <-- ERROR OCCURED HERE

// commit all of the changes to the DB and close the ISession
transaction.Commit();
session.Close();
}
}
}

I have tried everything such as: gone through the debugging process(using .NET 2005 debugging tool) and I have checked every property of the ISession, ISessionFactory and Configuration as well.

they seems to get mapped properly, however the error that occurs is like this: (I'll just dump the stack trace)
Message: "Unknown entity class: NHibernate.Examples.Quickstart.User"

StackTrace:
at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(Type theClass)
at NHibernate.Impl.SessionImpl.GetClassPersister(Type theClass)
at NHibernate.Impl.SessionImpl.GetEntityPersister(Object obj)
at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
at NHibernate.Impl.SessionImpl.Save(Object obj)
at NHibernate.Examples.Quickstart.UserTest.Main(String[] args) in C:\Swin\NHibernate.Examples.Quickstart\NHibernate.Examples.Quickstart\UserTest.cs:line 22
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


I have set the build action of the "User.hbm.xml" to "Embedded Resource" as well. I googled a lot and I found nothing that mention we should generate the assembly file callled "NHibernate.Examples" (setting the "User.hbm.xml" itself to "Embedded Resource" doesn't seems to solve the problem, since the .NET compile will start looking for the assembly file called "NHibernate.Examples which I used inside the "User.hbm.xml")

Most of the suggestions mentioned in lots of forum regarding this problem, I have tried and none of them solve my problem :(.

If any of you have successfully mapped the class to the firebird db, please tell me. I need it urgently.

just as additional note, my application is not Web application, but an Console Application. Most of the tutorial including the nhibernate_reference itself, only shows how to deal with Web application, thus the GetCurrentSession() implementation can't be adopted into my application.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 4:34 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
This exception is not related to a specific DBMS. Your class need to be public:
Code:
   public class User
   {
       ...

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 4:59 pm 
Newbie

Joined: Thu Aug 30, 2007 2:50 pm
Posts: 7
Thanks for your reply karl, at first I thought that was the problem but unfortunately it wasn't :(. I am totally stuck with that entity unknown error. I am pretty sure that the entity doesn't exist. I double-checked the property of classPersister in session > [NHibernate.Impl.SessionImpl] > SessionFactory > [NHibernate.Impl.SessionImpl] > non-public members > classPersisters (during the debugging process), and the value shows "Count = 1" which means that there is a class and I check the class value is "NHibernate.Examples.Quickstart.User".
But I don't know why I still got that error "unknown entity class: NHibernate.Examples.Quickstart.User"?????


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 5:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Upon a second look, I think you are missing a call to this:
Code:
    cfg.AddAssembly("NHibernate.Examples");

I can't recall whether you need to call that before cfg.Configure() or after; try both.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 5:28 pm 
Newbie

Joined: Thu Aug 30, 2007 2:50 pm
Posts: 7
karlchu wrote:
Upon a second look, I think you are missing a call to this:
Code:
    cfg.AddAssembly("NHibernate.Examples");

I can't recall whether you need to call that before cfg.Configure() or after; try both.


Most of the forum that I read also said something like that.
ok this is what I have experienced in regards to your suggestion:

first attempt:
Configuration cfg = new Configuration().AddAssembly("NHibernate.Examples");

result:
Exception: something related to setting up the hibernate dialect

second attempt:
Configuration cfg = new Configuration().AddAssembly("NHibernate.Examples");
cfg.Configure();

result:
Same as the previous one

third attempt:
Configuration cfg = new Configuration().AddClass("NHibernate.Examples.Quickstart.User")
.AddAssembly("NHibernate.Examples");

result:
Same as the previous one

I've dealt most of my time with that configuration and the only one working is the one that I am using right now new Configuration().Configure();

I don't know why the guide given by most of the Nhibernate programmers' on the web doesn't seems to get my program's running:(

I am totally stuck at the moment, I have been dealing with this problem for 2 days in :( and I really have to get it done within the next 5 hours, thus it's really urgent.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 5:30 pm 
Newbie

Joined: Thu Aug 30, 2007 2:50 pm
Posts: 7
I hope someone would mind to just list all of the possible solutions and I will work that out :). I think that's what so called as "urgent", thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 5:44 pm 
Newbie

Joined: Thu Aug 30, 2007 2:50 pm
Posts: 7
Here you go some weird problem again. in my User.hbm.xml file (to see the code, look at my first post) I changed the
<class name="NHibernate.Examples.Quickstart.User,NHibernate.Examples" table="users"> to <class name="NHibernate.Examples.Quickstart.User,NHibernate.Examples" table="eee"> and it produced the same error "Unknown entity class: NHibernate.Examples.Quickstart.User".

this problem is very clear to me that although I have specified the table name as "users" (note: I have created the database and the table structure named users as well before) nhibernate didn't really map my user class with table users. if you try to go back and take a look at my source code (in my first post), what would you recommend me to do???


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 31, 2007 12:56 am 
Newbie

Joined: Thu Aug 30, 2007 2:50 pm
Posts: 7
Solved the problem :). I've never realized that .NET is a bit fuzz about the assembly naming. in my User.hbm.xml source code, inside the tag <class> I put the value "NHibernate.Examples.Quickstart.User, NHibernate.Examples" for the attribute name; however, this's not right since all of my source code are under NHibernate.Examples.Quickstart namespace, therefore the value of the name attribute should be like this:
"NHibernate.Examples.Quickstart.User,NHibernate.Examples.Quickstart"
I got this kind of tiny mistake because I built all of my application manually using MSBuild instead of using .NET IDE, thus I couldn't see that I have put an incorrect assembly name, cuz if you're using .NET IDE you can just right click on your project name name in the project explorer and choose properties, there you just need to match the assembly name with the namespace :).

Hopefully this article will be useful for you guys who have the same problem as me. If you got an exception: "Unknow entity class: bla bla bla", that means there must be something wrong with the mapping process. the most important thing to do is tocheck your assembly name correctly. cuz honestly speaking, this kind of error is really hard to trace, even using .NET debugger tool!!


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