The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Hibernate

I believe there are some Hibernate gurus here. My question is, how well does Hibernate work when there is not a simple 1:1 mapping between the properties of an object and the columns in a DB table?

Here is a simple example. We have an order object, one property of which is the name/description of the thing you are ordering. This comes from a master table. The (possibly tricky) part is, the name is language-specific depending on the language of the user doing the query. Eg. the key of the master table is product code plus user language code.

How well does Hibernate deal with this sort of thing?
NetFreak Send private email
Tuesday, February 07, 2006
 
 
Regardless of whether you use Hibernate or not, I don't think "the key of the master table is product code plus user language code" is a good idea.

If I've read that correctly, product X is duplicated within the master table for every country you decide to support. What you need really is a one-to-many mapping between the product and the country specific strings.  Once you've established one, use a set with the relationship name, i.e...

<set name="relationship">
  <key_column="product_key"/>
  <composite-element class="Relationship">
      <many-to-one name="country_key" class="Country"/>
  </composite-element>
</set>

See the Hibernate FAQs on many-to-many associations.
TheDavid
Tuesday, February 07, 2006
 
 
I oversimplified, the 'names table' is actually subordinate to the 'master table'; the problem is still valid (as is your comment). Anyways, you answered my question. Cheers!
NetFreak Send private email
Tuesday, February 07, 2006
 
 
Good tip, TheDavid.  I didn't realize you could model association classes that way.  Just to clarify, you'd do your mapping something like this:

<set name="namesAndDescriptions">
  <key_column="product_key"/>
  <composite-element class="ProductNameAndDescription">
      <property name="name"/>
      <property name="description"/>
      <many-to-one name="country_key" class="Country"/>
  </composite-element>
</set>
dave
Tuesday, February 07, 2006
 
 
It's not clear how either of these sample mappings would incorporate the notion of user's language preference, eg. a field not directly related to the primary table, which was the point of my original question.
NetFreak Send private email
Wednesday, February 08, 2006
 
 
My impression - and I admit there's a good chance I'm wrong - that the property tags in dave's example, are the extraneous columns you want to include but not join upon.

I haven't poked at the Hibernate source code so I don't know how Hibernate deals with two seperate tables having the same column name. For example, TableA.name and TableB.name may contain different data and you want TableB.name to be included in the combined "view".
TheDavid
Wednesday, February 08, 2006
 
 
As I understand it, the OP's data is structured like this:

The Order table has a FK to user, which in turn has an FK to country.  The order table also has an FK to product_master.

The product_names_and_descriptions table has a FK to product_master, an FK to countries, a name, and a description.

Given this data, in Hibernate I'd model it just like the tables:

Order has a one-to-one to user, which has a one-to-one to country.  Order also has a one-to-one to product, which has a one-to-many to NamesAndDescriptions.

So in order to get the proper name and description for the user's locale, you'd have to do it programmatically:

Set descriptions = theOrder.getProduct().getNamesAndDescriptions();
Country theCountry = theOrder.getUser().getCountry();

and iterate through the set till you find the right one.

AFAIK there's no way to embed that country-lookup logic in your hibernate mappings, but I could be wrong.
dave
Wednesday, February 08, 2006
 
 
The more that I think about it, the worse Hibernate sounds. For example, in dave's scenario, users can have more than one order - it shouldn't be a one to one relationship.

Sometimes its easier to just write the SQL yourself.  :)

For the record, I don't use Hibernate. I never started using it aggressively because I didn't want to be dependent upon yet another library; the benefits didn't seem worth it then, and if we don't find a solution here, probably still not worth it.
TheDavid
Wednesday, February 08, 2006
 
 
> The more that I think about it, the worse Hibernate sounds. For example, in dave's scenario, users can have more than one order - it shouldn't be a one to one relationship.

> Sometimes its easier to just write the SQL yourself.  :)

I just made a mistake in that post.  The user-order relationship would be a one-to-many.
dave
Wednesday, February 08, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz