So I see that basic problem. However, that is just the logical model,
physically B requires A (due to foreign key and method of generating
the ID) but A does not require B (although any case where B is missing
would be considered as data corruption).

So, how can this be correctly mapped with NHibernate? Also, does that
mean for one-to-one to ever work neither table involved can have a
foreign key constraint as NHibernate does not guarantee nor provide a
way to control the order in which the entities will be persisted?

I'm not even sure what alternative there is to using one-to-one since
I'm joining across primary keys so a standard one-to-many on either
side will not work?

The table's relations are essentially (if this isn't a one-to-one I'm
lost as to what is):

CREATE TABLE Foo (
    id int NOT NULL IDENTITY(1,1) PRIMARY KEY
)

CREATE TABLE Bar (
    id int NOT NULL PRIMARY KEY FOREIGN KEY (id) REFERENCES Foo (id)
)

On Jan 26, 4:48 am, Fabio Maulo <[email protected]> wrote:
> Sorry my answer was not clear:
> If A has a constraint where B must exist and B has a constraint where A must
> exist what you can do ?
>
> After read it re-read the constraints of your mapping.
>
> 2010/1/25 Chris C <[email protected]>
>
>
>
>
>
> > Even assuming I can use a different generator such as hilo or
> > guid.comb the following mapping still fails due to the foreign key and
> > an incorrect insert order, essentially on Bar's one-to-one I need
> > something like inverse="true".
>
> > I did manage to get something working using <join><component>...</
> > component></join> but it just seems messy.
>
> >    <class name="ValueMapping.Foo, ValueMapping" table="Foo">
> >         <id name="Id" type="int">
> >             <generator class="hilo" />
> >         </id>
> >        <one-to-one cascade="all" class="ValueMapping.Bar,
> > ValueMapping" constrained="true" foreign-key="none" name="Bar" />
> >        <property name="Name" type="string"/>
> >    </class>
>
> >     <class name="ValueMapping.Bar, ValueMapping" table="Bar">
> >         <id name="Id">
> >            <generator class="foreign">
> >                <param name="property">Foo</param>
> >            </generator>
> >        </id>
> >        <one-to-one name="Foo" constrained="true" foreign-
> > key="FK_BarToFoo"/>
> >        <property name="Rate"/>
> >        <property name="Value"/>
> >    </class>
>
> > On 25 Jan, 15:45, Chris C <[email protected]> wrote:
> > > I've tidied up the mapping file and switched to using the foreign key
> > > generator but the basic problem you described still remains.
> > > NHibernate keeps insisting upon trying to save B before A, when
> > > instead it needs to save A then B.
>
> > > The database in question is an older legacy schema which cannot be
> > > changed at this current point in time unfortunately.
>
> > > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
> > >     <class xmlns="urn:nhibernate-mapping-2.2" name="ValueMapping.Foo,
> > > ValueMapping" table="Foo">
> > >         <id name="Id" type="int">
> > >             <generator class="identity" />
> > >         </id>
> > >         <one-to-one cascade="all" class="ValueMapping.Bar,
> > > ValueMapping" constrained="true" foreign-key="none" name="Bar" />
> > >         <property name="Name" type="string"/>
> > >     </class>
>
> > >     <class xmlns="urn:nhibernate-mapping-2.2" name="ValueMapping.Bar,
> > > ValueMapping" table="Bar">
> > >         <id name="Id">
> > >             <generator class="foreign">
> > >                 <param name="property">Foo</param>
> > >             </generator>
> > >         </id>
> > >         <one-to-one name="Foo" constrained="true" foreign-
> > > key="FK_BarToFoo"/>
> > >         <property name="Rate"/>
> > >         <property name="Value"/>
> > >     </class>
> > > </hibernate-mapping>
>
> > > On 25 Jan, 15:18, Fabio Maulo <[email protected]> wrote:
>
> > > > If A has a constraint where B must exist and B has a constraint where A
> > must
> > > > exist and the only way to know a POID using identity is storing a
> > record how
> > > > you can do it ?
>
> > > > In some case, in NH, you can use <inverse> to define the "owner"of the
> > > > relationship.
> > > > Advise:
> > > > 1) don't send a generated mapping because it is not human-readable
> > > > 2) don't use "native" if native=identity
> > > > 3) don't use composite-key if you don't need it (in your case there is
> > only
> > > > one field)
> > > > 4) have a look to :
> > > > <generator class="foreign">
> > > > <param name="property">Foo</param>
> > > > </generator>
>
> > > > 2010/1/25 Chris C <[email protected]>
>
> > > > > If I have a pair of tables that have a one-to-one relation between
> > > > > them, how do I control the order in which they are saved (i.e. define
> > > > > which is the parent)?
>
> > > > > Currently NHibernate keeps trying to save "Bar" before it has saved
> > > > > "Foo" and thus either fails due to a foreign key violation when using
> > > > > something like "guid.comb" or null key error if using something like
> > > > > "native".
>
> > > > > The mapping I have so far is listed below, the example source is at
> > > > >http://gist.github.com/285899
>
> > > > > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
> > > > > access="property" auto-import="true" default-cascade="none" default-
> > > > > lazy="true">
> > > > >  <class xmlns="urn:nhibernate-mapping-2.2" name="ValueMapping.Foo,
> > > > > ValueMapping, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
> > > > > table="`Foo`">
> > > > >    <id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0,
> > > > > Culture=neutral, PublicKeyToken=b77a5c561934e089">
> > > > >      <column name="Id" />
> > > > >      <generator class="native" />
> > > > >    </id>
> > > > >    <one-to-one cascade="all" class="ValueMapping.Bar, ValueMapping,
> > > > > Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
> > > > > constrained="true" foreign-key="none" name="Bar" />
> > > > >    <property name="Name" type="System.String, mscorlib,
> > > > > Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
> > > > >      <column name="Name" />
> > > > >    </property>
> > > > >  </class>
>
> > > > >  <class xmlns="urn:nhibernate-mapping-2.2" name="ValueMapping.Bar,
> > > > > ValueMapping, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
> > > > > table="`Bar`">
> > > > >    <composite-id mapped="false" unsaved-value="undefined">
> > > > >      <key-many-to-one name="Foo" class="ValueMapping.Foo,
> > > > > ValueMapping, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
> > > > >        <column name="Foo" />
> > > > >      </key-many-to-one>
> > > > >    </composite-id>
> > > > >    <property name="Rate" type="System.Int32, mscorlib,
> > > > > Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
> > > > >      <column name="Rate" />
> > > > >    </property>
> > > > >    <property name="Value" type="System.Int32, mscorlib,
> > > > > Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
> > > > >      <column name="Value" />
> > > > >    </property>
> > > > >  </class>
> > > > > </hibernate-mapping>
>
> > > > > --
> > > > > You received this message because you are subscribed to the Google
> > Groups
> > > > > "nhusers" group.
> > > > > To post to this group, send email to [email protected].
> > > > > To unsubscribe from this group, send email to
> > > > > [email protected]<nhusers%[email protected]
> > > > >  >
> > <nhusers%[email protected]<nhusers%252bunsubscr...@googlegroup 
> > s.com>>
> > > > > .
> > > > > For more options, visit this group at
> > > > >http://groups.google.com/group/nhusers?hl=en.
>
> > > > --
> > > > Fabio Maulo
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected]<nhusers%[email protected] 
> > >
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/nhusers?hl=en.
>
> --
> Fabio Maulo

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to