• Post Reply Bookmark Topic Watch Topic
  • New Topic

[Hibernate] Look Up table retrieve  RSS feed

Jas Jas
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a table with three columns.


It's a look up table. So type +code make the primary key. I want to retrieve this data in a way that can be stored in hashmap. The key of map is type and value is list of objects . (The object is basically a bean with properties Code and Description)

The way I am doing it right now

1. get a list of all types then
2. Get the list of object based on each type

So if I have total 14 distinct types, I am making 15 queries to retrieve the data. Is that the right approach. Here is my mapping xml look like

DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
<!-- This is designed keeping in mind the retrieve -->
<hibernate-mapping >
<class name="GenTable" table="GenTable">
<id name="type" type="string" column="GTType" >
<generator class="assigned"/>
<component name="entry" class="Entry">
<property name="code" >
<column name="CODE" />
<property name="desc" >
<column name="DESC" />

David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can simplify the Hibernate and database usage by creating a single-column primary key, even if it's just the combination of the type and code columns. This isn't mandatory, but composite keys are a little more difficult in Hibernate, especially in this case since you'll be searching on half of the PK.

Another good reason for doing this is that you can then define foreign keys in the database without having to store the type values in each table with lookup codes. You can also define a check constraint on each lookup column to ensure that it has a valid code for the column's lookup type. You can add the check constraint even if you don't create a surrogate PK. Now that I think of that, the constraint is sufficient and obviates the need for the foreign key.

Even if you don't do this, code and description should be regular properties of your GenTable object rather than a component. A better example of a component would be a MailingAddress belonging to a Person. And if you don't do the above, you need to make the PK include both type and code since type isn't unique.

Finally, though you'll likely load the lookup table only once at application startup, you can speed up the loading by issuing a single "select all" query and building your Map-based internal lookup table as you go. Since this has little impact, I don't know that I'd actually recommend it unless you really need to tune your startup time. Sorting by type would make the iteration code pretty trivial.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!