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.  [ 3 posts ] 
Author Message
 Post subject: Unknown entity class when saving entity
PostPosted: Wed Feb 27, 2008 5:07 am 
Newbie

Joined: Wed Feb 27, 2008 3:52 am
Posts: 2
Hibernate version: 1.2.0

Hi all,

I'm trying out NHibernate but i'm running into the above mentioned error when i'm trying to save an entity. Here's my code:

The class straight from the docs

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1 {
    public class Cat {
        private string id;
        private string name;
        private char sex;
        private float weight;

        public Cat() {
        }

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

        public virtual string Name {
            get { return name; }
            set { name = value; }
        }

        public virtual char Sex {
            get { return sex; }
            set { sex = value; }
        }

        public virtual float Weight {
            get { return weight; }
            set { weight = value; }
        }
    }
}


The mapping file (named Cat.hbm.xml):

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

  <class name="ClassLibrary1.Cat, ClassLibrary1" table="Cat">

    <!-- A 32 hex character is our surrogate key. It's automatically
            generated by NHibernate with the UUID pattern. -->
    <id name="Id">
      <column name="CatId" not-null="true"/>
      <generator class="assigned" />
    </id>

    <!-- A cat has to have a name, but it shouldn' be too long. -->
    <property name="Name">
      <column name="Name" length="16" not-null="true" />
    </property>
    <property name="Sex" />
    <property name="Weight" />
  </class>

</hibernate-mapping>


My app.config:

Code:
<?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 name="WindowsFormsApplication1">
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="dialect">NHibernate.JetDriver.JetDialect, NHibernate.JetDriver</property>
      <property name="connection.driver_class">NHibernate.JetDriver.JetDriver, NHibernate.JetDriver</property>
      <property name="connection.connection_string">Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb</property>
    </session-factory>
  </hibernate-configuration>

</configuration>


And finally the code to save the entity:

Code:
        private void button1_Click(object sender, EventArgs e) {
            Configuration config = new Configuration();
            ISessionFactory sessionFactory = config.Configure().BuildSessionFactory();
            config.AddAssembly("ClassLibrary1");
            ISession session = sessionFactory.OpenSession();

            ITransaction tx = session.BeginTransaction();

            Cat princess = new Cat();
            princess.Name = "Princess";
            princess.Sex = 'F';
            princess.Weight = 7.4f;

            session.Save(princess);
            tx.Commit();

            session.Close();
            sessionFactory.Close();
        }


I'm getting the Unknown entity class on the session.Save(princess); line of code. When i debug the code and check the exception the inner exception is null. Here's the stacktrace:

Code:
   bij NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(Type theClass)
   bij NHibernate.Impl.SessionImpl.GetClassPersister(Type theClass)
   bij NHibernate.Impl.SessionImpl.GetEntityPersister(Object obj)
   bij NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
   bij NHibernate.Impl.SessionImpl.Save(Object obj)
   bij WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) in D:\Documenten\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:regel 36
   bij System.Windows.Forms.Control.OnClick(EventArgs e)
   bij System.Windows.Forms.Button.OnClick(EventArgs e)
   bij System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   bij System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   bij System.Windows.Forms.Control.WndProc(Message& m)
   bij System.Windows.Forms.ButtonBase.WndProc(Message& m)
   bij System.Windows.Forms.Button.WndProc(Message& m)
   bij System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   bij System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   bij System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   bij System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   bij System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   bij System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   bij System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   bij System.Windows.Forms.Application.Run(Form mainForm)
   bij WindowsFormsApplication1.Program.Main() in D:\Documenten\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:regel 15
   bij System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   bij System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   bij Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   bij System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   bij System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   bij System.Threading.ThreadHelper.ThreadStart()


So what am i doing wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 5:22 am 
Expert
Expert

Joined: Tue Aug 23, 2005 5:52 am
Posts: 335
Have you made your mapping files Embedded Resources?

Cheers,

Symon.

_________________
Symon Rottem
http://blog.symbiotic-development.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 5:30 am 
Newbie

Joined: Wed Feb 27, 2008 3:52 am
Posts: 2
Yes i did that, just after i posted here i got an answer on another forum which solved my problem. The problem was that i should call config.AddAssembly() before i call config.BuildSessionFactory(). So my code now looks like this:

Code:
            Configuration config = new Configuration();
            config.Configure();
            config.AddAssembly("ClassLibrary1");
            ISessionFactory sessionFactory = config.BuildSessionFactory();
            ISession session = sessionFactory.OpenSession();

            ITransaction tx = session.BeginTransaction();

            Cat princess = new Cat();
            princess.Name = "Princess";
            princess.Sex = 'F';
            princess.Weight = 7.4f;

            session.Save(princess);
            tx.Commit();

            session.Close();
            sessionFactory.Close();


merge_s.rottem, thanks for your help!


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