Hi,
I have the following Class Hierarchy & Code
public class ObjectRelationship : IHaveAuditInformation
{
public virtual long? Id { get; set; }
public virtual string PropertyClass { get; set; }
public virtual long? ParentId { get; set; }
public virtual string Name { get; set; }
public virtual string CreatedBy { get; set; }
public virtual DateTime? CreatedOn { get; set; }
}
public class Region : ObjectRelationship, ICanBeValidated
{
private IList<RegionContact> contacts = new List<RegionContact>();
public Region()
: base()
{
this.PropertyClass =
RealProperty.Core.Domain.PropertyClass.Region;
}
public virtual string Code { get; set; }
public virtual string Description { get; set; }
public virtual IList<RegionContact> Contacts
{
get { return contacts; }
private set { this.contacts = value; }
}
public override string ToString()
{
return String.Format("{0} - {1}", this.Code,
this.Description);
}
public virtual string Content
{
get { return this.ToString(); }
}
public virtual void AddContact(RegionContact contact)
{
this.Contacts.Add(contact);
contact.Region = this;
}
public virtual void RemoveContact(RegionContact contact)
{
this.Contacts.Remove(contact);
}
}
public class Contact: ObjectRelationship, ICanBeValidated
{
public Contact():base()
{
this.PropertyClass =
RealProperty.Core.Domain.PropertyClass.Contact;
}
public virtual string LabourCode { get; set; }
public virtual string ContactName { get; set; }
public virtual LookupCode Role { get; set; }
}
public class RegionContact: Contact
{
public RegionContact()
: base()
{
}
[Required]
public virtual Region Region { get; set; }
}
And the following Mappings:
public class ObjectRelationshipMap : ClassMap<ObjectRelationship>
{
/// <summary>
/// Initializes a new instance of the <see
cref="ObjectRelationshipMap"/> class.
/// </summary>
public ObjectRelationshipMap()
{
Table("ObjectRelationship");
Id(x => x.Id)
.GeneratedBy.Native("REALPROPERTY_SEQ_ALL");
Map(x => x.ParentId);
Map(x => x.Name)
.Length(100); ;
Map(x => x.PropertyClass)
.Length(20);
Map(x => x.CreatedBy).Length(50);
Map(x => x.CreatedOn);
}
public class ContactMap: SubclassMap<Contact>
{
public ContactMap()
{
Table("Contact");
KeyColumn("Id");
References(x => x.Role).Column("RoleId");
Map(x => x.LabourCode).Length(50).Not.Nullable();
Map(x => x.ContactName, "Name").Length(50) .Not.Nullable();
}
}
public class RegionContactMap : SubclassMap<RegionContact>
{
public RegionContactMap()
{
Table("Contact");
KeyColumn("Id");
References(x => x.Role).Column("RoleId");
References<Region>(x => x.Region, "EntityId");
Map(x => x.LabourCode).Length(50).Not.Nullable();
Map(x => x.ContactName, "Name").Length(50).Not.Nullable();
}
}
public class RegionMap : SubclassMap<Region>
{
/// <summary>
/// Initializes a new instance of the <see cref="RegionMap"/>
class.
/// </summary>
public RegionMap() {
Table("REGION");
KeyColumn("Id");
HasMany<RegionContact>(x => x.Contacts)
.Cascade.AllDeleteOrphan()
.Inverse()
.KeyColumns.Add("EntityId");
Map(x =>
x.Code).Length(50).UniqueKey("REGION_CODE_CLIENT_UK");
Map(x => x.Description).Length(100);
Map(x => x.ClientId).UniqueKey("REGION_CODE_CLIENT_UK");
}
}
Hi,
I have the following Class Hierarchy & Code
public class ObjectRelationship : IHaveAuditInformation
{
public virtual long? Id { get; set; }
public virtual string PropertyClass { get; set; }
public virtual long? ParentId { get; set; }
public virtual string Name { get; set; }
public virtual string CreatedBy { get; set; }
public virtual DateTime? CreatedOn { get; set; }
}
public class Region : ObjectRelationship, ICanBeValidated
{
private IList<RegionContact> contacts = new List<RegionContact>();
public Region()
: base()
{
this.PropertyClass =
RealProperty.Core.Domain.PropertyClass.Region;
}
public virtual string Code { get; set; }
public virtual string Description { get; set; }
public virtual IList<RegionContact> Contacts
{
get { return contacts; }
private set { this.contacts = value; }
}
public override string ToString()
{
return String.Format("{0} - {1}", this.Code,
this.Description);
}
public virtual string Content
{
get { return this.ToString(); }
}
public virtual void AddContact(RegionContact contact)
{
this.Contacts.Add(contact);
contact.Region = this;
}
public virtual void RemoveContact(RegionContact contact)
{
this.Contacts.Remove(contact);
}
}
public class Contact: ObjectRelationship, ICanBeValidated
{
public Contact():base()
{
this.PropertyClass =
RealProperty.Core.Domain.PropertyClass.Contact;
}
public virtual string LabourCode { get; set; }
public virtual string ContactName { get; set; }
public virtual LookupCode Role { get; set; }
}
public class RegionContact: Contact
{
public RegionContact()
: base()
{
}
[Required]
public virtual Region Region { get; set; }
}
And the following Mappings:
public class ObjectRelationshipMap : ClassMap<ObjectRelationship>
{
/// <summary>
/// Initializes a new instance of the <see
cref="ObjectRelationshipMap"/> class.
/// </summary>
public ObjectRelationshipMap()
{
Table("ObjectRelationship");
Id(x => x.Id)
.GeneratedBy.Native("REALPROPERTY_SEQ_ALL");
Map(x => x.ParentId);
Map(x => x.Name)
.Length(100); ;
Map(x => x.PropertyClass)
.Length(20);
Map(x => x.CreatedBy).Length(50);
Map(x => x.CreatedOn);
}
public class ContactMap: SubclassMap<Contact>
{
public ContactMap()
{
Table("Contact");
KeyColumn("Id");
References(x => x.Role).Column("RoleId");
Map(x => x.LabourCode).Length(50).Not.Nullable();
Map(x => x.ContactName, "Name").Length(50) .Not.Nullable();
}
}
public class RegionContactMap : SubclassMap<RegionContact>
{
public RegionContactMap()
{
Table("Contact");
KeyColumn("Id");
References(x => x.Role).Column("RoleId");
References<Region>(x => x.Region, "EntityId");
Map(x => x.LabourCode).Length(50).Not.Nullable();
Map(x => x.ContactName, "Name").Length(50).Not.Nullable();
}
}
public class RegionMap : SubclassMap<Region>
{
/// <summary>
/// Initializes a new instance of the <see cref="RegionMap"/>
class.
/// </summary>
public RegionMap() {
Table("REGION");
KeyColumn("Id");
HasMany<RegionContact>(x => x.Contacts)
.Cascade.AllDeleteOrphan()
.Inverse()
.KeyColumns.Add("EntityId");
Map(x =>
x.Code).Length(50).UniqueKey("REGION_CODE_CLIENT_UK");
Map(x => x.Description).Length(100);
Map(x => x.ClientId).UniqueKey("REGION_CODE_CLIENT_UK");
}
}
As code mentions, ObjectRelationship is the base class and the this
class mapping uses ClassMap and the rest of the classes are using
SubclassMap. Generated Id in the ObjectRelationship will be used in
the subclasses as well.
The Contact entity can be used by multiple entities and in order to be
able to do Insert/Update/Delete operation on contacts using NHibernate
built-in method, I have to have different subclasses of Contact, as I
did for RegionContact.
What is not working right now is that by second level subclassing
(RegionContact: Contact), Fluent NHibernate throws exception and does
not do the update. But If I create RegionContact class same as Contact
class everything works.
To me this means that Fluent NHibernate does not like sub-classing of
domain object, though the mapping is correct?
I appreciate any thoughts and insights.
Mohammad
--
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.