Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes Hibernate Bags & Composite IDs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hibernate Bags & Composite IDs" Watch "Hibernate Bags & Composite IDs" New topic

Hibernate Bags & Composite IDs

Cagan Senturk

Joined: Jun 14, 2004
Posts: 1
I am new to Hibernate and trying to convert my existing web application's data layer to using it.
One of the problems I've run into is the following:
I have a Portal class, that has an Id, Name, URL, etc as properties. It's mapped to a portal table.
In the database I have another table that maintains a mapping of portal_ids to zipcodes.
So the table has the following structure:

where (portal_id, zipcode) makes up the primary key.

What I'm trying to achieve is to load the list of zipcodes a portal covers everytime I load a portal object in my application.

So I have a List object within my Portal class that will contain the list of zipcodes (string objects).

Here are my questions:
1) Should I represent the portal-2-zipcode mapping table as a java object?
2) Or is there another method of loading the zipcode information when I have a portal loaded?
3) In case the portal-2-zipcode is a class, what's the best approach to represent the composite id?
4) Should I use bag or map?

This is only a read-only operation. I don't need to add/remove any zipcodes after the portal object is loaded.

Thanks in advance for all your help.

Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

This is only a read-only operation

Here's your way out - since it is a read only operation you can use a view, and map the view rather than the more complex table structure as it stands, particularly since composite ids are a little awkward in Hibernate.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
I agree. Here's the link:
subject: Hibernate Bags & Composite IDs
It's not a secret anymore!