views:

712

answers:

1

Lets say we have an Employee entity composed of a few other entities, such as a one-to-many addresses and contacts, and a few fields (name, age, etc). We mapped this entity out and can use it just fine, saving each piece out into "Employee", "EmployeeAddresses", and "EmployeeContacts" tables.

However, we use pretty much all of this employee's information for a big calculation and have a separate "EmployeeInput" object composed of the same Address and Contact object lists (i.e. both the Employee and EmployeeInputs object has a list of Address and Contact entities). We need to save of this information when we preform the calculation for later auditing purposes. We'd like to save this EmployeeInput entity to an "EmployeeInput" table in the database.

The problem we're running into is how to save the Address and Contact lists? We'd like to stick them into something like "EmployeeInputAddresses" and "EmployeeInputContacts", but the Address and Contact entites are already mapped to "EmployeeAddresses" and "EmployeeContacts", respectively.

What's the easiest way to accomplish this without creating a new "EmployeeInputAddress" and "EmployeeInputContact" entity and separate mapping files for each (as the fields would literally be duplicated one by one). Put another way, how can we map a single entity, Address, to two different tables depending on the parent object it belongs to (EmployeeAddresses table if it's saving from an Employee object, and EmployeeInputAddresses table if it's saving from an EmployeeInput object).

+3  A: 

The easiest way would be to have addresses and contacts mapped as composite elements. That way you could map your collection differently for employe and for employeinput since the mapping is owned by the container.

For exemple:

public class Employe
{
    public List<Address> Addresses{get; set;}
}


public class EmployeInput
{
    public List<Address> Addresses{get; set;}
}

public class Address
{
    public string Street{get;set;}
    public string City{get; set;}
}

Would have the folloying mapping:

<class name="Employe" table="Employes">
    <id name="id">
        <generator class="native"/?
    </id>
    <list name="Addresses" table="EmployesAddresses">
        <key column="Id" />
        <index column="Item_Index" />
        <composite-element class="Address">
            <property name="Street" />
            <property name="City" />
        </composite-element>
    </list>
</class>

<class name="EmployeInput" table="EmployesInput">
    <id name="id">
        <generator class="native"/?
    </id>
    <list name="Addresses" table="EmployesInputAddresses">
        <key column="Id" />
        <index column="Item_Index" />
        <composite-element class="Address">
            <property name="Street" />
            <property name="City" />
        </composite-element>
    </list>
</class>
Simon Laroche