Here's my setup. I've got an Item class, and an ItemDefinition class. Item instances really represent instances of Items, ItemDefinition contains some shared properties (name, Description).
I load Items from an XML file generated by a 3rd party program.
My classes look like this:
Code:
class Item
{
public int Id = -1;
public int Count;
public ItemDefinition Definition;
}
class ItemDefinition
{
public int Id = -1;
public string Name;
public string Definition;
}
And my nhibernate mappings look like this:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Roe.Util.Item, Roe.Util" table="items">
<id name="Id" column="Id" type="Int32" unsaved-value="-1">
<generator class="identity" />
</id>
<property name="Count" />
<many-to-one name="Definition" column="ItemDefId" cascade="all" />
</class>
</hibernate-mapping>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Roe.Util.ItemDefinition, Roe.Util" table="item_definitions">
<id name="Id" column="Id" type="Int32" unsaved-value="-1">
<generator class="identity" />
</id>
<property name="Name" length="64" />
<property name="Description" length="256" />
</class>
</hibernate-mapping>
So seudo-code for what I do when parsing this XML file looks like this:
Code:
Item item = new Item(nCount, new ItemDefinition(sItemName, sItemDesc));
The problem is, the ItemDefinition might already exist in the database, and rather than do a query to look up the ItemDefinition by the Name, then attach that to the Item, is there any way to map this so just doing a Save() will figure out that there's a conflict (item_definitions.Name is UNIQUE) and do a Get() for the existing ItemDefinition?
I'm guessing the answer to that question is no, and I don't like the idea of using Name as the key for ItemDefinitions, mainly because of the size and that there are going to be many items, and the whole point of splitting the definition out from the item was to save space.
Does anyone have any suggestions or advice? Have I messed up somewhere in my design (I am pretty new to database design and O/R mappers)? If I've got to do a query to find the Id of an existing ItemDefinition, that's not the end of the world... I figure I can cache the values... I was thinking maybe write a custome Id generator for ItemDefinition?
Thanks in advance.