nHibernate & sqlite mappings

烈酒焚心 提交于 2019-12-23 03:23:35

问题


I'm having real problems with setting up nHibernate with sqlite.

Here is the hibernate.cfg.xml file:

<?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="dialect">NHibernate.Dialect.SQLite20Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
    <property name="connection.connection_string">Data Source=books.db;Version=3</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
    <property name="query.substitutions">true=1, false=0</property>

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


  </session-factory>
</hibernate-configuration>

and here is the (simplified) mapping file:

 <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly ="DataAccess" namespace="DataAccess.DOM">
  <class name="Book" table="book">
    <id name="id" type="integer" unsaved-value="null">
      <generator class="increment"/>
    </id>

    <property name="isbn" type="string" length="25"/>
    <property name="author" type="string" length="100"/>

  </class>
</hibernate-mapping>

The table:

create table book(
  id INTEGER primary key,
  author TEXT,
  isbn TEXT,
);

I get the error: "Could not compile the mapping document: DataAccess.DOM.Book.hbm.xml"

I'm developing in VS2008 on Vista 32bit.

I've added the System.Data.SQlite assembly and set to copy local.

Any suggestions?


回答1:


I fixed this ages ago, and now can't remember exactly what I did :o(

However, looking at the code: I notice this change to the mapping file, I changed the tag for the Id from integer to Int32 and the generator class from increment to native:

<id name="Id" type="Int32" unsaved-value="0">
  <generator class="native"/>
</id>



回答2:


Have you embedded the hbm.xml file in the assembly?

Taken from https://www.hibernate.org/362.html :

NOTE: If you are using Visual Studio .NET to compile make sure that you set the Build Action of the User.hbm.xml file to Embedded Resource. The mapping file will now be a part of the Asssembly. The subtle detail's importance will be evident later.




回答3:


In your book mapping file, set the properties like this:

<property name="isbn" column="isbn"/>
<property name="author" column="author"/>

Also make sure that your values in the above name attributes are the same case as they are your class.



来源:https://stackoverflow.com/questions/1512446/nhibernate-sqlite-mappings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!