NHibernate pure one-to-one mapping

1,411 views
Skip to first unread message

Marcello Esposito

unread,
Jan 30, 2013, 12:14:48 PM1/30/13
to nhu...@googlegroups.com

Hi all.

I'm trying to create a one-to-one NHibernate mapping between three entities (in my actual scenario entities are much more):

User
|
+--Employee
|
+--BankAccount

For each User there is exactly one Employee and exactly one BankAccount.

These are my mappings.

<class name="User" table="Users">
  <id name="Id">
    <generator class="guid.comb" />
  </id>

  <property name="Name" not-null="true" />
  <one-to-one name="Employee" cascade="all-delete-orphan" constrained="true" foreign-key="none" />
  <one-to-one name="BankAccount" cascade="all-delete-orphan" constrained="true" foreign-key="none" />
</class>

<class name="Employee" table="Employees">
  <id name="Id" column="IDUser">
    <generator class="foreign">
      <param name="property">User</param>
    </generator>
  </id>

  <property name="HireDate" not-null="true" />
  <one-to-one name="User" constrained="true" />
</class>

<class name="BankAccount" table="BankAccounts">
  <id name="Id" column="IDUser">
    <generator class="foreign">
      <param name="property">User</param>
    </generator>
  </id>

  <property name="AccountNumber" not-null="true" />
  <one-to-one name="User" constrained="true" />
</class>

This solution allows to target the following objectives:

  • Primary key for the three entities are the same (strict one-to-one relation).
  • Employees and BankAccounts tables have a foreign-key on Users table.
  • Loading a user does not join on other two tables and correctly creates a lazy-ready proxy (see famous Ayende post)

Any other configuration I've tried misses some of the above points.

The problem is that, while inserting a user into the DB, NHibernate inserts Employee and BankAccount before User, thus raising a foreign-key exception.

Currently I am using foreign-key="none" on both sides of the relation, thus giving up on the referential integrity constraint.

Any help about the correct way to address this mapping?

Thanks.

Pete Appleton

unread,
Jan 30, 2013, 2:53:30 PM1/30/13
to nhu...@googlegroups.com

You seem to have "constrained" at both ends of the relationships, it should only be on the dependent entity (Employee and BankAccount)

 

Current:

(user) <one-to-one name="Employee" cascade="all-delete-orphan" constrained="true" foreign-key="none" />

(employee) <one-to-one name="User" constrained="true" />

Try dropping the 'constrained' attribute from the <one-to-many>'s in User

 

/Pete

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+u...@googlegroups.com.
To post to this group, send email to nhu...@googlegroups.com.
Visit this group at http://groups.google.com/group/nhusers?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
!DSPAM:1,5109551f1871750812013!

Marcello Esposito

unread,
Jan 31, 2013, 2:06:39 AM1/31/13
to nhu...@googlegroups.com
Hi Pete,


Try dropping the 'constrained' attribute from the <one-to-many>'s in User

if I remove 'contrained' attribute on User side, NHibernate would force fetch=join when loading users, just to discover whether each related entity actually exists (so to build a lazy-ready proxy) or not (set related attribute to null). This allows to preserve a NHibernate invariance, as well explained by Ayende.
In my case, when fetching a User I would get a (often useless) join on 6 tables more. I would like to avoid this behaviour.

Thanks for your answer,
Marcello.
Reply all
Reply to author
Forward
0 new messages