473,473 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Hibernate relationship with a join table

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
1 18506
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Eric | last post by:
I use Mysql and SQLyog (trial version), I created 2 tables but I get this message when I try to create relationship between tables: "The selected table does not have InnoDB Table Handler. You...
2
by: bodleyhenley | last post by:
Please help me with this! :) I have a database of servers, one of the things I am recording information about is what software is on each server. Obviously this is a many to many so I have a...
6
by: davegb | last post by:
I'm trying to create a self-join table to show the relationship between employee and supervisor. In another thread, I was advised to create a SupervisorID in the employee table, a separate...
3
by: CanFlightSim | last post by:
I am not very strong in databases, so you must forgive any obvious stupidity. After having a good look at our Sales Managemend database, we decided there is something basially wrong. Our Agents...
3
by: ssims | last post by:
I've been thinking about this problem for awhile, and can't seem to come up with a valid answer. I've got two tables, Surveys and Instructors. When a survey is completed there are one or two...
9
by: koehlerc14 | last post by:
I'm trying to create a form that imports a text file and join zip code fields to our "Master" database x2. I import the txt file and name it based on a "strTableName" which includes the date, a...
6
by: BD | last post by:
Hi, all. I need to enforce a one-to-many relationship on 2 tables, with a join table. Say the join table contains account information. It has cust_no and acct_no. Both cust_no and acct_no are...
3
by: DaveRook | last post by:
Hi I think I'm just after advice on best practice with this! I have a database which has 2 tables. The first table has ID, Name, Address and the second table has ID and JobType. Table 02...
0
by: Todd Nichols | last post by:
I have the main form for the "one" side of the relationship and a subform for the "many" side, linked by the primary key of the main table to the foreign key of the join table (the many-side table). ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.