-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Help mapping many-to-many using relationship tables
PostPosted: Fri Sep 30, 2005 4:03 pm 
Newbie

Joined: Fri Mar 18, 2005 6:15 pm
Posts: 3
Location: New York
Hello!

I need help implementing the following relationship. I have the following DB structure:

CREATE TABLE rate_cards
(
rate_card_id NUMBER NOT NULL (primary key)
rate_card_name
rate_card_percentage
change_date
)

CREATE TABLE advertiser_rate_cards
(
rate_card_id FK to rate_cards,
advertiser_id FK to advertiser_v,
change_date
)

CREATE OR REPLACE VIEW advertisers_v (
advertiser_id,
advertiser_name )
........
)

Advertiser can belong to many rate cards. And rate card can be assigned to multiple advertisers.

My POJO has the following desing:

public class RateCard {
private int id;
private String name;
private int percentage;
private Date changeDate;
private List<Advertiser> selectedAdvertisers;
}

public class Advertiser {
private int id;
private String name;


When retrieving a rate card, I would also like to get a list of advertisers assigned to it. Can you please explain to me how I should go about mapping these two objects to achieve desired results?

I would like to avoid creating a separate class for the relationship table advertiser_rate_cards as it does not make sense from the OO design. As this is not a single case, I would end up with a lot of classes that represent db relationships and i would like to avoid that.

So far, we have been using regular JDBC calls to retrieve assigned collections. So, RateCard is mapped using hibernate. All properties of the java class are mapped except for selectedAdvertisers. Once the object is retrieved using
(RateCard)HibernateUtil.getStandard().getBusinessObject(RateCard.class,
new Integer(rateCardId))
I execute a separate method getSelectedAdvertisers(rateCardId) inside which i execute a regular jdbc query "select id, name from advertiser_v adv, advertiser_rate_cards where rate_card_id = xxx and advertiser_id = adv.id"

I would like to use hibernate features to achieve the same. I've searched google and forums but I could not find any solutions or examples. It would be helpful if i can see some examples/code.

Thanks for the help. I would really really appreciate it.
Julia.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 4:03 pm 
Newbie

Joined: Wed Oct 19, 2005 3:45 pm
Posts: 14
u actually donĀ“t need to create an object to represent the asocciation instead you want o add some information to your association.

Due that the relation is many to many you have to add in both clasess a collection (set) of the other class.

I have two clasess, user and role that share a relation many to many and y map like that:

In USER

<set name="roles"
table="um_user_role"
lazy="true"
cascade="save-update">
<key column="user_id"/>
<many-to-many class="Role" column="role_name"/>
</set>

In ROLE

<set name="users"
table="um_user_role"
lazy="true"
inverse="true"
cascade="save-update">
<key column="role_name"/>
<many-to-many class="User" column="user_id"/>
</set>

hope it helps you
:D


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.