Using reference as id in fluentnhibernate

I have a child table containing the parent id. This is a one-to-one mapping, but values ​​may not be present in the child table. I have problems displaying this without getting errors, though ... I tried several things; display of the same column with different properties, etc.

 Parent table
   int id

 Child table
   int parentid

 Parent class
   int id

 Child class
   Parent parent // note I'm referencing parent, not using an int id ..

Mapping

Id(x => x.Parent) .Column("parentid"); // fails Id(x => x.Parent.Id) .Column("parentid"); // fails References(x => x.Parent) .Column("parentid"); // fails - missing id // Adding an id field in addition to parent for // child class (id is then the same as parent.id) // fails on save Id( x => x.Id ) .Column("parentid"); References(x => x.Parent) .Column("parentid"); 

I would like the child class to not have a separate Id field, but rather a reference to the parent, since there can never be a child without a parent. However, in the database, I just want to save the parent id.

Any ideas how I can do this?

+7
fluent-nhibernate- nhibernate-mapping
source share
3 answers

The following works:

 Id(x => x.Parent.Id).Column("MemberID"); References(x => x.Parent).Column("MemberID").ReadOnly(); 

ReadOnly for the link it is important not to get an exception

EDIT: It wasn't that easy ...

My child class still had the Id property. The identifier reference for Parent.Id seems to confuse nhibernate, and instead tries to call child.Id. I added the following to the child, and now it seems to be working. Nice ugly hack.

 public virtual int Id { get { return Parent.Id; } set { Debug.Assert(value == Parent.Id); } } 
+5
source share

The FluentNHibernate API has changed over the years, so I'm not sure if this syntax was available when this question was originally asked, but now you can use the link as an identifier if you match it as a composite identifier. I would not call it hacking, but it’s a little strange that you need to map the reference to the parent object as part of a compound identifier. Here is a complete example:

 public class ParentMap : ClassMap<Parent> { public ParentMap() { Table( "StackOverflowExamples.dbo.Parent" ); Id( x => x.ParentId ); Map( x => x.FirstName ); Map( x => x.LastName ); } } public class OnlyChildOfParentMap : ClassMap<OnlyChildOfParent> { public OnlyChildOfParentMap() { Table( "StackOverflowExamples.dbo.OnlyChildOfParent" ); CompositeId().KeyReference( x => x.Parent, "ParentId" ); Map( x => x.SomeStuff ); Map( x => x.SomeOtherStuff ); } } public class Parent { public virtual int ParentId { get; set; } public virtual string FirstName { get; set; } public virtual string LastName { get; set; } } public class OnlyChildOfParent { public virtual Parent Parent { get; set; } public virtual string SomeStuff { get; set; } public virtual string SomeOtherStuff { get; set; } #region Overrides public override bool Equals( object obj ) { if ( obj == null || GetType() != obj.GetType() ) return false; var child = obj as OnlyChildOfParent; if ( child != null && child.Parent != null ) { return child.Parent.ParentId == Parent.ParentId; } return false; } public override int GetHashCode() { return Parent.ParentId; } #endregion Overrides } 
0
source share

Maybe this post can help.
I used the .Cascade.SaveUpdate() annotation.
My business was with hasone in parent putting annotation on both sides

Discussion: PT-BR Language

0
source share

All Articles