0

Assume I have this domain object...

public class SpansMultipleTables
{
     public int CommonID {get; set;}

     public string Table1Value {get; set;}

     public int Table2Value {get; set;}

     public float Table3Value {get; set;}
}
  • The CommonID property maps to the "ID" column on all tables.
  • The Table1Value property maps to the "Value" column in the table "Table1"
  • The Table2Value property maps to the "Value" column in the table "Table2"
  • The Table3Value property maps to the "Value" column in the table "Table3"

Using FluentNHibernate, how can I set up a map for this object that really doesn't have a central table as it's home?

2 Answers 2

5

Try Join, but I would recommend changing your design.

public class SpansMultipleTablesMap : ClassMap<SpansMultipleTables>
{
  public SpansMultipleTablesMap()
  {
    Id(x => x.CommonID);
    Join("Table1", m =>
    {
      m.Map(x => x.Table1Value, "Value");
    });
    Join("Table2", m =>
    {
      m.Map(x => x.Table2Value, "Value");
    });
    Join("Table3", m =>
    {
      m.Map(x => x.Table3Value, "Value");
    });
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 I may be forced to go this route because the senior developer on my team refuses to accept that this is a bad and unmaintainable design.
/// <summary> /// Links this entity to another table, to create a composite entity from two or /// more tables. This only works if you're in a table-per-inheritance-hierarchy /// strategy. /// </summary> ................................. Read the intellisense documentation for you try this.
0

I'm not an expert, but I'm not sure if NHibernate can easily handle a mapping like this. If you are able to modify the schema, you might be able to define a "master" table, which just has the CommonID as the primary key. The class for the master table can then map the other tables as properties by their relationship to the master table. Another option might be to designate one of the tables as the master, and have the others mapped as properties on the master class. In any case, it would probably be a good idea to create some FK relationships between these tables to guarantee that the combined "entity" cannot have its parts removed separately.

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.