These are the 3 patterns to map a one-to-one relation http://fabiomaulo.blogspot.com/2010/03/conform-mapping-one-to-one.html
On Thu, Dec 16, 2010 at 1:57 PM, dazed3confused <[email protected]>wrote: > Hey think I had just thought I was doing it properly as I have seen it > that way before, and vaguely remembered my NHibernate in action book > (should buy the new NH3 cookbook, but havent got round to it yet) > talking about many-to-one with unique="true" to do one-to-one. However > on taking a look back at the book after your suggestion, it says that > it should be done with many-to-one with unique="true" on the parent > end, and a one-to-one one the child end for the bidirectional > relationship. Just wanted to post this up incase anyione else comes > against a similar prob, or think this is still wrong. > > So User relates to account in much the same way- > > <!-- Relationship with Account --> > <join table="UsersAccounts" optional="true"> > <key> > <column name="UserId" unique="true" not-null="true" /> > </key> > <many-to-one name="Account" column="AccountId" unique="true" > cascade="all" /> > </join> > > And Account to User- > <one-to-one name="User" class="User" foreign-key="none" /> > > Thanks :-) > > On Dec 15, 7:04 pm, Fabio Maulo <[email protected]> wrote: > > IMO you are trying to represent a bidirectional-one-to-one but you are > using > > a wrong mapping-pattern. > > > > On Tue, Dec 14, 2010 at 12:57 PM, dazed3confused <[email protected] > >wrote: > > > > > > > > > Hi People > > > > > I have upgraded to NH3.0.0GA, and unfortunately I am experiencing > > > some problems with my mappings, specifically the join property. Im not > > > sure if this has changed in the new version since 2.1.2, or if its > > > broken. > > > The error I'm getting is- > > > > > NHibernateSessionFactory.Instance.GetSession().SaveOrUpdate(entity); > > > A first chance exception of type 'NHibernate.PropertyValueException' > > > occurred in NHibernate.dll > > > 'NHibernateSessionFactory.Instance.GetSession().SaveOrUpdate(entity)' > > > threw an exception of type 'NHibernate.PropertyValueException' > > > base {NHibernate.HibernateException}: {"not-null property > > > references a null or transient > > > valueBlog.Core.Model.Entities.Account.User"} > > > EntityName: "Blog.Core.Model.Entities.Account" > > > entityName: "Blog.Core.Model.Entities.Account" > > > Message: "not-null property references a null or transient > > > valueBlog.Core.Model.Entities.Account.User" > > > PropertyName: "User" > > > propertyName: "User" > > > > > I think this might be happening because it is trying to save the > > > account before it saves the user (which should be saved to db before > > > account). > > > > > My classes and mappings that are producing this error are > > > classes- > > > > > public class User : BaseEntity > > > { > > > > > public virtual string Forename { get; set; } > > > > > public virtual string Surname { get; set; } > > > > > public virtual Account Account { get; protected set; } > > > > > public virtual void SetAccount(Account account) > > > { > > > this.Account = account; > > > } > > > > > } > > > > > public class Account : BaseEntity > > > { > > > public virtual string Username { get; set; } > > > > > public virtual string Password { get; set; } > > > > > public virtual UserSecurityLevel SecurityLevel { get; set; } > > > > > public virtual User User { get; protected set; } > > > > > public virtual void SetUser(User user) > > > { > > > this.User = user; > > > } > > > > > } > > > > > mappings- > > > > > <class name="User" table="Users"> > > > > > <id name="Id" type="Int32"> > > > <generator class="native" /> > > > </id> > > > > > <version name="Version" type="Int32" unsaved-value="-1"/> > > > <property name='Active' /> > > > > > <property name="Forename" type="String" column="Forename"/> > > > > > <property name="Surname" type="String" column="Surname"/> > > > > > <!-- Relationship with Account--> > > > <join table="UsersAccounts" optional="true"> > > > <key> > > > <column name="UserId" unique="true" /> > > > </key> > > > <many-to-one name="Account" column="AccountId" not- > > > null="true" unique="true" > > > cascade="all-delete-orphan" /> > > > </join> > > > > > </class> > > > > > <class name="Account" table="Accounts"> > > > > > <id name="Id" type="Int32"> > > > <generator class="native" /> > > > </id> > > > > > <version name="Version" type="Int32" unsaved-value="-1"/> > > > <property name='Active' /> > > > > > <property name="Username" type="String" column="Username"/> > > > > > <property name="Password" type="String" column="Password"/> > > > > > <property name="SecurityLevel" column="SecurityLevel"/> > > > > > <!-- Relationship with User --> > > > <join table="UsersAccounts" inverse="true"> > > > <key> > > > <column name="AccountId" unique="true" /> > > > </key> > > > <many-to-one name="User" column="UserId" not-null="true" > > > unique="true" /> > > > </join> > > > > > </class> > > > > > Please let me know if I should be providing more details? > > > > > Thanks > > > > > -- > > > 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%[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]<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.
