Skip to content

A few programming code tips (Part 2) – Things to know about NHibernate Mappings

September 26, 2012

Following on from learning about a few programming code tips, I had to write some things or quirks with the NHibernate Mappings.

As I said about saving old code, let me elaborate, it would be things that you are not sure about, hence you comment it as it may be useful at a later time.

My point was made yesterday, when I had commented out on some old code on the NHibernate Mappings. Looking at the code below: –

<?xml version=”1.0″ encoding=”utf-8″ ?>
<hibernate-mapping xmlns=”urn:nhibernate-mapping-2.2″
namespace=”Project.Subscription”
assembly=”Project”>
<class name=”Subscription” table=”Subscription”>
<id name=”SubscriptionID” column=”SubscriptionID” type=”long”>
<generator />
</id>

<property name=”UserID” type=”long” update=”false” insert=”false”/>

<property name=”SubscriptionPackageID” type=”int” update=”false” insert=”false”/>
<property name=”SubscriptionUnit” type=”int” />
<property name=”Deleted” type=”boolean” />

<many-to-one name=”SubscriptionPackage”
column=”SubscriptionPackageID” lazy=”false” cascade=”none” />

<!–<many-to-one name=”User”
column=”UserID” lazy=”false” cascade=”none” />–>

</class>
</hibernate-mapping>

I had commented out one of the mappings. It had later turned out that I needed this many to one mapping relationship for User to signify that it is a foreign key of UserID to the User table, without this, if you update the User Table, then it cannot refer to the foreign key to update the Subscription table. Hence you need that relationship mapping, so uncomment it.

You can notice here that I also have the UserID property name aswell, I later found out that the property name UserID and the mapping many-to-one UserID cannot coexist together, with the exception of adding update=”false” and insert=”false” on the property name UserID. Otherwise you get the error ‘Invalid Index N for this SqlParameterCollection with Count=N error’.

Hope that helps!

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: