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:
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 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!
Try dropping the 'constrained' attribute from the <one-to-many>'s in User