One-to-one is a legacy mapping type, don't use if you avoid. How can nhibernate know that there is an object in table B without joining it in the query (because it has the same identifier), lazy in a one-to-one is not advised, one-to-one is just to split tables up nothing more nothing less
Kind regards On 26 jan, 15:52, Chris C <[email protected]> wrote: > Ideally what I want is a pair of one-to-one relationships that support > lazy loading via a proxy. The only way I currently know how to achieve > that would be by specifying constrained. Logically the relationship is > constrained and thus I want to tell NHibernate that so it knows it can > always generate a proxy for B rather than having to load B > immediately. So if not constrained perhaps there needs to be a way to > tell NHibernate it is always allowed to use a proxy for B and assume B > exists, without the side-effects constrained has on the insert order. > Ignoring the use of identity for the POID (which we aim to phase out), > it can be said that this is just to work around MSSQL's lack of > deferred foreign key constraints. > > Anyway thanks for your time, as far as I can currently see: If you > don't need lazy loading then setting constrained to false on the > parent side, and true on the child side will work. If you do need lazy > loading then it becomes a bit of a hack and you have to use a > collection and one-to-many on the parent side, and a constrained one- > to-one on the child side. > > On Jan 26, 2:23 pm, Fabio Maulo <[email protected]> wrote: > > > NH is for persistence matters. With NH what your are describing is how > > persist/retrieve something and not which are the rules of your business. > > > 2010/1/26 Chris C <[email protected]> > > > > Ah, I see, so constrained deals with the physical structure, so what > > > about the logical structure though? As normally if you use > > > constraint="false" nhibernate can no longer generate a proxy for B to > > > allow lazy loading (e.g. for use with vertical partitioning) despite > > > the fact that A will always have a B (at least logically). The only > > > time an A with out a B should occur is when NHibernate is in the > > > middle of a flush but this should be handled with in a transaction so > > > to any observer the rule wouldn't have been invalidated. > > > > On Jan 26, 2:08 pm, Fabio Maulo <[email protected]> wrote: > > > > if A does not require B then A should have the property B with > > > > constrained="false" > > > > > 2010/1/26 Chris C <[email protected]> > > > > > > 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 > > ... > > meer lezen » -- 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.
