Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / March 2007

Tip: Looking for answers? Try searching our database.

[Hibernate] How to map "decode" tables?

Thread view: 
Jim Garrison - 08 Mar 2007 23:22 GMT
Many database schemas contain "code" tables that map
internal attribute values to external display strings.
For instance:

create table CODE_OS_TYPE
(
   OS_TYPE             varchar2(4)     not null primary key,
   DESCRIPTION         varchar2(30)    not null
);

Then in your entity tables you store only OS_TYPE code

create table CR_ASSET
(
   ASSET_ID            integer                 not null,
   OS_TYPE             varchar2(4)             not null,
   ....

When mapping such a schema in Hibernate, what's the
recommended way of mapping these "code" relationships
from within the entity so that the Java object has
access to both the code and description?

In the above example, the CR_ASSET mapping might look like this:

<class name="Asset">
 <id name="id" column="ASSET_ID" type="long">
   ...
 </id>

 <property name="osType"     column="OS_TYPE" />
 <property name="osDescription"  ???what goes here??? />

This is one common use-case that doesn't appear to be covered in
the otherwise excellent (if poorly indexed) "Java Persistence
with Hibernate".

One option I considered was defining osDescription as a
derived property using the formula= attribute with a
correlated subselect.  I'm concerned that this would result
in greatly reduced query efficiency...  then again, maybe Oracle's
optimizer is smart enough to convert the subselect to a join.

Anybody have suggestions on how to implement this, or pointers
to where this is described?
Robert Klemme - 09 Mar 2007 08:55 GMT
> Many database schemas contain "code" tables that map
> internal attribute values to external display strings.
[quoted text clipped - 41 lines]
> Anybody have suggestions on how to implement this, or pointers
> to where this is described?

IMHO the straight forward solution is to make CODE_OS_TYPE an Enum
(there is some documentation to get enums working with Hibernate) or
just make it an ordinary class with an int and String field and apply
the usual mapping.

Kind regards

    robert
Axel Hallez - 09 Mar 2007 09:06 GMT
I guess it's not covered because it'is not possible in the way you want
to do it.

The most obvious way to do this is the create a class (and mapping) that
wraps the OS_TYPE and the description and use this class for the
attribute type in Asset.

BTW the term you'll want to use for searching is 'lookup table'.

Hope this helps,
Axel

> Many database schemas contain "code" tables that map
> internal attribute values to external display strings.
[quoted text clipped - 41 lines]
> Anybody have suggestions on how to implement this, or pointers
> to where this is described?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.