Example hibernate mapping xml for one to many by a join table

We have table trade_summary and table trades_comment, each entry in trade_summary has multiple comments in trades_comment table. However, the relationship between these two tables are stored in another join table trade_summary_comment. Each row in trade_summary_comment has two columns: trade_id and comment_id which are primary keys in those two tables. The hibernate mapping goes as follows:

<set name=”comments” table=”trade_summary_comment”>
<key column=”trade_id”/>
<many-to-many column=”comment_id” unique=”true” class=”bmo.hub.idp.entity.TradesComment” order-by=”updated desc”/>
</set>

In java code, we use set in TradeSummary class to store its comments:

private Set<TradesComment> comments = new TreeSet<TradesComment>();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s