1

I'm looking to do the following, imagine a scenario where you have an object:

public class ObjectA {
    public virtual Guid Id;
    public virtual string PropertyA;
    public virtual string PropertyB;
    public virtual string PropertyC;
    public virtual string PropertyD;
}

And for reasons I wont go into, the database NEEDS to look like this:

----
Table: ObjectABase
----
Column Id
Column PropertyA
Column PropertyB
----

----
Table: ObjectAExtended
----
Column Id
Column PropertyC
Column PropertyD
----

The object is split between both tables and is referenced by the same Id so when you call Session.Save(... it will save to both tables.

Anyone any ideas how to do this or if its even possible?

Cheers.

EDIT: I have figured out the answer but can't post it for another 7 hours apparently. Will update tomorrow.

1

2 Answers 2

2

I figured it out. In NHibernate 3.2 using map by code...

public class ObjectAMap : ClassMapping<ObjectA> {
    public ObjectAMap () {
        Table("ObjectABase");
        Id<Guid>(x => x.Id, m => { m.Column("Id"); });
        Property(x => x.PropertyA, map => { map.Column("PropertyA"); });
        Property(x => x.PropertyB, map => { map.Column("PropertyB"); });
        Join("ObjectA",
             m => {
                 m.Table("ObjectAExtended");
                 m.Key(x => { x.Column("Id"); });
                 m.Property<string>(x => x.PropertyC, map => { map.Column("PropertyC"); });
                 m.Property<string>(x => x.PropertyD, map => { map.Column("PropertyD"); });
             });

        }); 
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

One possibility would be to use a joined-subclass element in the nhibernate mapping. You could lay out your objects as follows:

public class ObjectABase
{
   public virtual int Id {get; set;}
   public virtual string PropertyA {get; set}  
   public virtual string PropertyB {get; set}  
}

public class ObjectA : ObjectABase
{
   public virtual string PropertyC {get; set}  
   public virtual string PropertyD {get; set}    
}

and then you could set up a mapping as follows:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DomainModel" namespace="Company.DomainModel">
  <joined-subclass name="ObjectA" table="ObjectAExtended" extends="Company.DomainModel.ObjectABase" lazy="false">
    <key column="Id" />
    <property name="PropertyC" />
    <property name="PropertyD" />
  </joined-subclass>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Company.DomainModel" assembly="DomainModel">
  <class name="ObjectABase" table="ObjectABase" lazy="false">
    <id name="Id" column="Id">
      <generator class="identity"/>
    </id>
    <property name="PropertyA" />
    <property name="PropertyB" />
  </class>
</hibernate-mapping>

By turning off lazy-loading you can get everything from the database in one shot.

This isn't exactly what you specified in your question, since the properties of ObjectA are defined both in ObjectA and the inherited base object, but there might not be a practical difference for your application.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.