Example of a composition mapping

Nov 14, 2009 at 10:22 PM

Can you guys help me create a case where 2 entitites map and save in the same table and use a reference of composition type instead of an aggregate.

Thanks,

cristian

Coordinator
Nov 18, 2009 at 4:05 PM

Should this one of these entities inherit from the other ?

Nov 20, 2009 at 2:49 PM

Hey Sebastien. Thanks for answering!

Here's the deal. A lot of legacy databases very poorly designed would map more than 1 entity per table. Now the funny part they don't have the same primary key so you end up with a lot of denormalization. 

Looking at code I can trick it for the persisted ones using a mapping like the one bellow since you update info with WHERE CLAUSE Where name = ?:

 

<Entity type="QuickStart:Domain:Customer" table="Customer">
  <Id field="CustomerId">
    <Generator name="guid" />
  </Id>
  <Attribute name="Name" field="Name" db-type="String" size="50" />
  <Attribute name="City" field="city" db-type="String" size="50" />
</Entity>
<Entity type="QuickStart:Domain:Customer" table="Customer">
  <Id field="Name">
    <Generator name="business" />
  </Id>
  <Attribute name="City" field="city" db-type="String" size="50" />
</Entity>

<Entity type="QuickStart:Domain:Customer" table="Customer">

  <Id field="CustomerId">

    <Generator name="guid" />

  </Id>

  <Attribute name="Name" field="Name" db-type="String" size="50" />

  <Attribute name="City" field="city" db-type="String" size="50" />

</Entity>

 

<Entity type="QuickStart:Domain:Customer" table="Customer">

  <Id field="Name">

    <Generator name="business" />

  </Id>

  <Attribute name="City" field="city" db-type="String" size="50" />

</Entity>

 

Obviously the insert case would try to create two new records. So there I probably need to write something.

Since I got your attention, I tried to run the test cases and I got lot of errors. Do I do anything wrong or I run the wrong code?. Debugging just to get acquainted with code I fixed one for the case with Vehicles and Passengers. The problem occurs when the name of the foreign keys are not identical with primary keys names. Here's the mapping from your example:

 

<Entity type="NUnit:Mapping:Domain:Vehicule" table="Vehicule" >

 

    <Id field="VehiculeId1">

      <Generator name="business" />

    </Id>

 

    <Id field="VehiculeId2">

      <Generator name="business" />

    </Id>

 

    <Attribute name="VehiculeId1" field="VehiculeId1" db-type="String" size="36" scale="0" precision="0" not-null="false" />

    <Attribute name="VehiculeId2" field="VehiculeId2" db-type="String" size="36" scale="0" precision="0" not-null="false" />

 

    <Reference name="Passagers" entityChild="NUnit:Mapping:Domain:Passager">

      <Rule parentField="VehiculeId1;VehiculeId2" childTable="Passager" childField="FK_VehiculeId1;FK_VehiculeId2" />

    </Reference>

 

  </Entity>

 

I'm not sure how late initialization works. Looking at the code found an example for LazyLoadingOneToMany but what about plain domain classes? 

Last, for synchronizing the foreign keys the framework emits an UPDATE clause after the insert of the record occurred, which is not going to work when foreign keys that cannot be NULL by column definition.

 

Regards,

cristian

 

 

 

Nov 20, 2009 at 3:06 PM

 Sorry I pasted a wrong  mapping file !

 

 

<Entity type="QuickStart:Domain:Customer" table="Customer">
  <Id field="CustomerId">
    <Generator name="guid" />
  </Id>
  <Attribute name="Name" field="Name" db-type="String" size="50" />
</Entity>
<Entity type="QuickStart:Domain:CustomerCity" table="Customer">
  <Id field="Name">
    <Generator name="business" />
  </Id>
  <Attribute name="Name" field="Name" db-type="String" size="50" />
  <Attribute name="City" field="city" db-type="String" size="50" />
</Entity>

<Entity type="QuickStart:Domain:Customer" table="Customer">

 

  <Id field="CustomerId">

 

    <Generator name="guid" />

 

  </Id>

 

  <Attribute name="Name" field="Name" db-type="String" size="50" />

 

</Entity>

 

 

 

<Entity type="QuickStart:Domain:CustomerCity" table="Customer">

 

  <Id field="Name">

 

    <Generator name="business" />

 

  </Id>

 

  <Attribute name="Name" field="Name" db-type="String" size="50" />

 

  <Attribute name="City" field="city" db-type="String" size="50" />

 

</Entity>