在用NHibernate1.2.0.GA版本中自带的NHibernate.Examples-2.0下的QuickStart测试连接Oracle数据库时,一直提示:System.Data.OracleClient.OracleException : ORA-12704: character set mismatch,但如果改成连接SqlServer数据库,一切OK。
Config文件中的配置如下:
<nhibernate>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.dialect" value="NHibernate.Dialect.OracleDialect" />
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.OracleClientDriver" />
<add key="hibernate.connection.connection_string" value="Data Source=dev;User ID=hcp;Password=hcp;" />
</nhibernate>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
<add key="hibernate.dialect" value="NHibernate.Dialect.OracleDialect" />
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.OracleClientDriver" />
<add key="hibernate.connection.connection_string" value="Data Source=dev;User ID=hcp;Password=hcp;" />
</nhibernate>
User.hbm.xml文件中的配置如下:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernateTest.User,NHibernateTest" table="users" lazy="false">
<id name="Id" column="LogonId" type="String" length="128">
<generator class="assigned" />
</id>
<property name="UserName" column="Name" type="String" length="128"/>
<property name="Password" type="String" length="128"/>
<property name="EmailAddress" type="String" length="128"/>
<property name="LastLogon" type="DateTime"/>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernateTest.User,NHibernateTest" table="users" lazy="false">
<id name="Id" column="LogonId" type="String" length="128">
<generator class="assigned" />
</id>
<property name="UserName" column="Name" type="String" length="128"/>
<property name="Password" type="String" length="128"/>
<property name="EmailAddress" type="String" length="128"/>
<property name="LastLogon" type="DateTime"/>
</class>
</hibernate-mapping>
在网上找了很久也没有找到这方面的错误信息,后来我查看了NHibernate的源码,在Type下发现了另外一种类型:AnsiString,这让我立即想起在使用Castle连接Oracle时,是使用的AnsiString类型,故把User.hbm.xml改成如下:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernateTest.User,NHibernateTest" table="users" lazy="false">
<id name="Id" column="LogonId" type="AnsiString" length="128">
<generator class="assigned" />
</id>
<property name="UserName" column="Name" type="AnsiString" length="128"/>
<property name="Password" type="AnsiString" length="128"/>
<property name="EmailAddress" type="AnsiString" length="128"/>
<property name="LastLogon" type="DateTime"/>
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernateTest.User,NHibernateTest" table="users" lazy="false">
<id name="Id" column="LogonId" type="AnsiString" length="128">
<generator class="assigned" />
</id>
<property name="UserName" column="Name" type="AnsiString" length="128"/>
<property name="Password" type="AnsiString" length="128"/>
<property name="EmailAddress" type="AnsiString" length="128"/>
<property name="LastLogon" type="DateTime"/>
</class>
</hibernate-mapping>
用NUnit测试,立即成功。
问题虽然已经解决,但我又想到另外一个问题,因为在使用NHibernate时,每一个实体类都对应一个*.hbm.xml,那在开发支持多数据库应用时,字符类型不就会出现在使用SqlServer 时使用String类型,在使用Oracle时使用AnsiString了吗?不知道这个问题大家是如何解决的,请高人指点,先谢了!