wood burning stoves 2.0*
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Hibernate Bags & Composite IDs
Similar Threads
Problem in composite key as foreign key mapping w/ hibernate v3.2.6 and jdk-6
Not able to view the data persisted in the database.
Inverse Question
How to retrieve detached @ManyToOne entities with relationship
how to get primary key after doing an insert