Attempting to map an object with a type similar to it as a member

I am new to nHibernate, here is my code

public abstract class DEField : EntityWithSerializableId
{
    public virtual Boolean IsVisibilityDepended { get; set; }
    public virtual DEField VisibilityField { get; set; }
    public virtual String VisibilityExpression { get; set; }  
}

      

I am trying to figure out how to map an object that has the same type in its members ( DEField

). This object can be null.

+3


source to share


2 answers


The mapping in this case (property type is the same as Entity) is simple / standard reference mapping. You can look here for more details: Loose matching

Links / many-to-one:

 ...
 References(x => x.VisibilityField);

      

What is it. In such cases, the attitude of the parent child is usually indicated. Thus, not only is it DEField

referred to as VisibilityField

, but also needs to know (from the point of view VisibilityField

) who is referring to it. 1) Parent link and 2) Kids collection:



public abstract class DEField : EntityWithSerializableId
{
    public virtual Boolean IsVisibilityDepended { get; set; }
    public virtual String VisibilityExpression { get; set; }  

    public virtual DEField VisibilityField { get; set; } // Parent
    public virtual IList<DEField> ChildFields { get; set; } // Children
}

      

And a slightly more complex mapping example might look like this:

 public class DEFieldMap : ClassMap<DEField>
 {
  public DEFieldMap()
  {
     Table("DEFieldTable");
     LazyLoad();
     Id(x => x.Id)
      .Column("Id")
      .Not.Nullable()    
      .GeneratedBy.Identity();
     Map(x => x.IsVisibilityDepended );
     Map(x => x.VisibilityExpression);
     References(x => x.VisibilityField)
      .Class<DEField>()
      .Access.Property()
      .Cascade.None()
      .LazyLoad()
      .Column("ParentId");
     HasMany<DEField>(x => x.ChildFields) // Children     
      .Access.Property()
      .AsBag()
      .Cascade.None()
      .LazyLoad()
      .Inverse()
      .KeyColumn("ParentId");
  }
 }

      

+1


source


You must use <many-to-one> in XML, or the equivalent in other mapping systems, as you would for any other class.



+1


source







All Articles