By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,843 Members | 864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,843 IT Pros & Developers. It's quick & easy.

Hibernate relationship with a join table

P: n/a
Hello all,

I've recently started playing with Hibernate, and have a question
regarding many-to-many relationships using a join or cross table (I've
also heard these referred to as a transition tables).

Given the following table structures :

|------| |-------| |------|
|a | | a_b | | b |
|------| |-------| |------|
|id | | a_id | |id |
|name | | b_id | |name |
|------| |-------| |------|

The purpose of table a_b is to contain many-to-many relationship data
between table a and table b.

How would I write an hbm xml file to express this? The Hibernate java
objects I want out of this would look like this :

public class a {
public void setId(int id);
public void setName(String name);
public void setBs(Collection Bs); // This method writes to table
a_b
public Collection getBs(); // This method reads table a_b to get
b's
etc.
}

public class b {
public void setId(int id);
public void setName(String name);
public void setAs(Collection As); // This method writes to table
a_b
public Collection getAs(); // This method reads table a_b to get
a's
etc.
}

I've been using MiddleGen with the Hibernate plugin to generate hbm
files, and it doesn't seem to understand the purpose of the table a_b
(it just creates an hbm file for class a_b instead).

I also can't seem to find any hibernate examples that employ a join
table.

Any help is much appreciated,

Keith
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi,

* ks*********@yahoo.com (Keith Simeon) [08 Sep 2003]:
Given the following table structures :

|------| |-------| |------|
|a | | a_b | | b |
|------| |-------| |------|
|id | | a_id | |id |
|name | | b_id | |name |
|------| |-------| |------| [...]
How would I write an hbm xml file to express this? The Hibernate
java objects I want out of this would look like this :

public class a {
public void setId(int id);
public void setName(String name);
public void setBs(Collection Bs); // This method writes to
table
a_b
public Collection getBs(); // This method reads table a_b to
get
b's
etc.
}


<class name="com.example.a" table="a">
<id name="id" type="integer">...</id>
<property name="name" column="name" type="string/>
<set name="bs" table="a_b">
<key column="a_id"/>
<many-to-many class="com.example.b" column="b_id"/>
</set>
</class>

The same for b.

Regards
Lutz
--
no sig
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.