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<http://ayende.com/blog/3960/nhibernate-mapping-one-to-one>
)
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.
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.