Nhibernate Unknown entity class 的解决办法

时间:2022-09-21 07:30:00

概述:

     当运行 session.Save(tnp);时,出现这个"Unknown entity class: TestCleanSnow.TestNhibernatePerson"这个异常.

     映射文件 TestNhibernatePerson.hbm.xml如下:  

<? xml version="1.0" encoding="utf-8" ?>
< hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.2"  assembly ="TestCleanSnow"  namespace ="TestCleanSnow" >
  
< class  name ="TestCleanSnow.TestNhibernatePerson,TestCleanSnow"  table ="TEST_NHIBERNATE_PERSON"  lazy ="false" >
    
< id  name ="Usertestid"  column ="USERTESTID"  type ="Decimal" >
      
< generator  class ="sequence" >
        
< param  name ="sequence" > emp_sequence </ param >
      
</ generator >
    
</ id >
    
< property  type ="string"  not-null ="true"  length ="6"  name ="Usertestname"  column ="USERTESTNAME"   />
  
</ class >
</ hibernate-mapping >

 

操作代码如下:

 

Configuration config  =   new  Configuration();              
                ISessionFactory factory 
=  config.BuildSessionFactory();
                ISession session 
=  factory.OpenSession();

                TestNhibernatePerson tnp 
=   new  TestNhibernatePerson();
 tnp.Usertestname 
=   " test4 " ;
  ITransaction trans 
=  session.BeginTransaction();
           
try
           {
             
               
//  保存记录
               session.Save(tnp);
               trans.Commit();
               Console.WriteLine(
" Insert Success! " );

                                          
           }
           
catch  (Exception ex)
           {
               trans.Rollback();
               Console.WriteLine(ex.Message);
           }

           
finally
           {
               session.Close();
           }

 

配置代码app.config如下: 

< configuration >
  
<!--  Add this element  -->
  
< configSections >
    
< section  name ="hibernate-configuration"  type ="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"   />
    
<!-- <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" /> -->
  
</ configSections >
  
<!--  Add this element  -->
  
< hibernate-configuration  xmlns ="urn:nhibernate-configuration-2.2"    >

    
< session-factory >
      
< property  name ="dialect" > NHibernate.Dialect.OracleDialect </ property >
      
< property  name ="connection.provider" > NHibernate.Connection.DriverConnectionProvider </ property >
      
< property  name ="connection.connection_string" > user id=jkpt;data source=jkorasvr;password=designer; </ property >
      
< property  name ="connection.isolation"   > ReadCommitted </ property >
      
< property  name ="show_sql" > true </ property >
      
<!--  mapping files  -->
      
< mapping  assembly ="TestCleanSnow"   />
    
</ session-factory >
  
</ hibernate-configuration >

 

 

说明:在操作代码中加载相应引用后 config.AddAssembly("TestCleanSnow");,就不出现错误.

如果以配置文件的形式加载引用却出现如题的错误.

 

原因分析:

解决1:

   可能是相应的映射文件没有设置成"嵌入式资源".我这里相应的映射文件为 TestNhibernatePerson.hbm.xml.

   在解决方案资源管理器中找到TestNhibernatePerson.hbm.xml该映射文件

   右击---属性----在将“生成操作”设置成“嵌入的资源”.

 

解决2:

我把 Configuration config = new Configuration();改成 Configuration config = new Configuration().Configure();

让程序去找hibernate.cfg.xml,这个配置文件的配置跟app.config中的一样。

 

解决3:
检查映射文件映射文件 TestNhibernatePerson.hbm.xml的配置是否正确,其中

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="模块名称" namespace="命名空间">
  
<class name="类名" table="数据库表名" lazy="false">
    
<id name="Usertestid" column="USERTESTID" type="Decimal">    //主键

 <generator class="sequence">      //主键生成方式