NHibernate: (one or zero) before (one or zero) display

I have two objects Foo and Bar :

 public class Foo { public virtual Guid FooID { get; set; } public virtual Bar MyBar { get; set; } } public class Bar { public virtual Guid BarID { get; set; } public virtual Foo MyFoo { get; set; } } 

both of these entities can exist independently of each other, but sometimes they are related to each other, and when this happens, I want to make sure that they are connected in the conservation level.

I want my tables to look like:

 create table Foo ( FooID int primary key, -- other stuff ); create table Bar ( BarID int primary key, FooID int null references Foo(FooID) on delete no action on update no action ); 

... and for NHibernate to be able to create relationships between them.

How do I match this (preferred XML)?

+4
source share
1 answer

So, @JBNizet, in his snarky and sarcastic way, pointed out that the relationship of Zero-or-one to Zero-or-One is considered a one-time bidirectional relation of One to One to Hibernate and NHibernate; therefore, according to http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#assoc-bidirectional-121 , the display will be like this:

 <class name="Foo"> <id name="FooID" column="FooID"> <generator class="guid" /> </id> <one-to-one name="MyBar" property-ref="MyFoo" /> </class> <class name="Bar"> <id name="BarID" column="BarID"> <generator class="guid" /> </id> <many-to-one name="MyFoo" column="FooID" unique="true" not-null="false" /> </class> 

(... I think. Editing with clarifications is welcome.)

+6
source

All Articles