NHibernate Relations - One to One

This is my first post on NHibernate relations. I will first talk about a somewhat misused relation, one to one.

This relation is about two tables that share a common primary key, for example, one of these tables contains some data and the other some additional data, and there is a 1:1 relationship between the two, meaning that one record from the first table has exactly one corresponding record on the second table and that all records from the second table have also exactly one corresponding record on the first.

The domain model for my example looks like this:

One to One

The classes look like this:

One to One Classes

For the moment, just ignore the Version property. Property E of class A is an instance of class E and property A of class E is an instance of class A. Class A holds a property which is the primary key of table A.

Let's look at the mappings:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateTest" assembly="NHibernateTest" auto-import="true" default-access="property">

<class name="A" table="`A`" optimistic-lock="version">

<id name="Id" column="`A1`">

<generator class="foreign">

<param name="property">E</param>

</generator>

</id>

<version name="Version" generated="always" unsaved-value="null" type="BinaryBlob" column="`VERSION`" />

<property name="SomeProperty" column="`SOME_PROPERTY`"/>

<one-to-one name="E" class="E" constrained="true"/>

<map name="Attributes" table="`F`" outer-join="true" cascade="all">

<key column="`A1`"/>

<index column="`KEY`" type="System.String"/>

<element column="`VALUE`" type="System.String"/>

</map>

</class>

</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateTest" assembly="NHibernateTest" auto-import="true" default-access="property">

<class name="E" table="`E`">

<id type="System.Int32">

<column name="`A1`"/>

<generator class="assigned"/>

</id>

<one-to-one name="A" class="A" outer-join="true" constrained="true"/>

<property name="SomeOtherProperty" column="`SOME_OTHER_PROPERTY`"/>

</class>

</hibernate-mapping>

You can see that the id for class E is an instance of class A. Again, please disregard for now property Version.

                             

No Comments

Add a Comment

As it will appear on the website

Not displayed

Your website