RSS

Category Archives: ORM

NHibernate mappings for Composite Keys with associations

This example demonstrates how to work on mappings with associations for legacy database tables designed purely using multiple primary keys/composite keys. The article NHibernate and Composite Keys by Anne Epstein provides a thorough explanation on fundamental  composite keys mapping and supporting lazy loading by declaring the composite keys as a class object. Here we only focus on a simple code sample of the associations between entities with composite keys.

Considering 2 sample tables, Products and Orders:

Products:

Name Type
StoreID NUMBER PK
ProductID NUMBER PK
ProductName VARCHAR

Orders:

Name Type
StoreID NUMBER PK
ProductID NUMBER PK
OrderID NUMBER PK
Amount NUMBER

Read the rest of this entry »

 
Leave a comment

Posted by on July 11, 2012 in .NET, C#, NHibernate, ORM

 

NHibernate & ORA-12571

When I was querying against Oracle database using NHibernate, I ran into Nhibernate.Exceptions.GenericADOException:{“could not update: … The InnerException of it was ORA-12571: TNS:packet writer failure. Resources on the net state that ORA-12571 happens generally when there is a network failure. But in my case it always happens on certain querys. Queries without string type parameters work normally. Turns out it is a Unicode to ASCII conversion problem. Adding a type attribute for string properties in the mapping file as follow can solve it:

<property name="SomePropertyName"  type="AnsiString" ></property>

The exception message is really misleading.

Reference:

NHibernate and The Case of the Crappy Oracle Error Message

NHibernate and ORA-12571 Errors

Failed to UPDATE or INSERT using NHibernate

 
Leave a comment

Posted by on June 26, 2012 in .NET, NHibernate, ORM

 

NHibernate configuration for Oracle

Our team developing environment is a bit outdated. We’re using vs2005 with Oracle 8i client. The data provider is Microsoft’s System.Data.OracleClient. Here is a simple workable NHibernate configuration. For those applications developed using ODP.NET, simply replace the connection.driver_class setting like the sample below.

<?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.Oracle8iDialect</property>
    <!--Using ADO.NET System.Data.OracleClient driver-->
    <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
    <!--Using ODP.NET driver-->
    <!--<property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>-->
    <property name="connection.connection_string">Data Source=XXXX;User ID=XXXX;Password=XXXX;Unicode=True</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

Reference:Nhibernate with Microsoft System.Data.OracleClient

 
Leave a comment

Posted by on June 22, 2012 in .NET, ADO.NET, NHibernate, Oracle, ORM