首页 新闻 会员 周边

遭遇unknown entity class问题

0
悬赏园豆:20 [已解决问题] 解决于 2010-07-30 11:06

各位大侠好,我是NHibernate初学者,写一个最初级的NHibernate程序操作Postgresql数据库遇到错误,附上源代码请帮忙看看。

 

hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
    <property name="connection.connection_string">Driver={PostgreSQL UNICODE};Server=localhost;Port=5432;Database=test;Uid=postgres;Pwd=pass123;</property>
    <property name="dialect">NHibernate.Dialect.PostgreSQL81Dialect</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

 

mapping文件(player.hbm.xml),已经配置为嵌入资源:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="TestNHibernate" namespace="TestNHibernate">
  <class name="TestNHibernate.Player" table="Player">
    <id name="ID" column="ID">
      <generator class="increment" />
    </id>
    <property name="PlayerName" />
    <property name="Country" />
  </class>
</hibernate-mapping>

实体类:

namespace TestNHibernate
{

  public class Player
    {
        public virtual int ID { get; set; }
        public virtual string PlayerName { get; set; }
        public virtual string Country { get; set; }
    }

}

数据库访问代码:

    Configuration cfg = new Configuration().Configure();

            ISession session = cfg.BuildSessionFactory().OpenSession();

            Player player = new Player();
            player.PlayerName = "Maradona";
            player.Country = "Argentina";

            session.Save(player);

    session.Close();

 

在session.Save一行出错:unknown entity class: TestNHibernate.Player

 

不知道为什么系统没有找到Player类的映射关系。

fat fox的主页 fat fox | 初学一级 | 园豆:180
提问于:2010-07-28 15:43
< >
分享
最佳答案
0

Configuration cfg = new Configuration().Configure();是读取hibernate.cfg.xml里面的设置配置SessionFactory,你在hibernate.cfg.xml没有配置Mapping。

可以使用下面两种方法:

1。可以在hibernate.cfg.xml里面加上Mapping配置<mapping assembly="程序集名称"/>

2。可以用Configuration类的一些AddXXX方法

例如:让NHibernate自动到程序集加载所有的嵌入式资源xml映射文件,NHibernate会自动到应用程序集里查找所有的以.hbm.xml结尾的资源文件。

Configuration cfg = new Configuration()
    .AddAssembly( "NHibernate.Auction" );
收获园豆:20
李永京 | 老鸟四级 |园豆:3114 | 2010-07-28 15:56
谢谢李兄的回答,我用了两种方法都有些问题,应该是我用的不对。 方法1, 修改后我的hibernate.cfg.xml如下: <?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property> <property name="connection.connection_string">Driver={PostgreSQL UNICODE};Server=localhost;Port=5432;Database=test;Uid=postgres;Pwd=wentangwood;</property> <property name="dialect">NHibernate.Dialect.PostgreSQL81Dialect</property> <property name="show_sql">true</property> <mapping assembly="TestNHibernate"/> </session-factory> </hibernate-configuration> 问题依旧。我把assembly改为player.hbm.xml也还是一样的错误。 方法2,我把configure语句改为 Configuration cfg = new Configuration().AddAssembly("TestNHibernate").Configure(); 出现错误:Could not compile the mapping document: TestNHibernate.Player.hbm.xml 貌似找到player.hbm.xml了,但编译失败。我的xml文件已经放上来了,请看看哪儿有问题。 谢谢。
fat fox | 园豆:180 (初学一级) | 2010-07-28 16:35
innerException错误是什么就知道了,Could not compile the mapping document是表象错误
李永京 | 园豆:3114 (老鸟四级) | 2010-07-28 16:41
<class name="TestNHibernate.Player"应该是<class name="Player"吧
李永京 | 园豆:3114 (老鸟四级) | 2010-07-28 16:43
InnerException是: The dialect was not set. Set the property hibernate.dialect。 我在hibernate.cfg.xml中已经设置了dialect,难道在entity mapping的xml里面还要配?
fat fox | 园豆:180 (初学一级) | 2010-07-28 17:22
具体信息异常...
李永京 | 园豆:3114 (老鸟四级) | 2010-07-29 12:38
莫名其妙的好了,我也不记得改了什么,大致是我用AddAssembly函数把程序集加进来,稍微改了下配置文件就好了。 结贴,谢谢李永京
fat fox | 园豆:180 (初学一级) | 2010-07-30 11:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册