-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Mapping to an existing object at insert/Save time?
PostPosted: Fri Jul 22, 2005 4:34 pm 
Beginner
Beginner

Joined: Fri Jul 22, 2005 4:08 pm
Posts: 28
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.