File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes many-to-many association problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "many-to-many association problem" Watch "many-to-many association problem" New topic

many-to-many association problem

Raj Gowda

Joined: Sep 18, 2007
Posts: 4
hi all, i am newbei to Hibernate, i am working on a project, it has two masters one is Being and another one is Debit. in Being master i am storing just beingName, in Debit master i am storing debitName and all the associated beingIds.

i am using three tables to store the content:
beingId long primary key, auto generated
beingName varchar(100)

debitId long primary key, auto generated
debitName varchar(100)

debitId long forieg key
beingId long forieg key

to persist being details i am using Being.hbm.xml
while user adding a being he will just enter beingName and submits, that will store in being table through Being.hbm.xml file

to persist debit details i am using debit.hbm.xml
while user adding a debit he will enter debitName and select multiple beingNames from the selection list and submits, now the debitName is storing to debit table, then newly generated debitId and associated beingIds are storing in debit_being table.

now when i am listing the debits i need to display debits with associated beingNames rather beingIds so, is it possible to define in the mapping document (i,e Debit.hbm.xml) so that i can retrieve all the associated Being objects of perticular Debit object.
do i have to write hql query to join the tables.

any help appriciated.
thanks in advance...
John Grath

Joined: Sep 18, 2007
Posts: 15
If I understand your question correctly, you most certainly can. In Hibernate you must map the domains using an association. This will enable you to use the session.find or better still the Query object to return the values, i.e session.get(..) and that the objects will be contain references to each other.

An association can be performed using a idbag, set, map, bag for example. Toplink works differently though, where the 'container' maintains the references between objects.

Hope this helps.
I agree. Here's the link:
subject: many-to-many association problem
It's not a secret anymore!