Hibernate Many-to-many Relationships
Hibernate Many-to-many Relationships - Many to many example in Hibernate. In this example we have used xml metadata.
Now we will learn many-to-many relationships. Let's try many-to-many example. The many-to-many tag is used to define the relationships. The extra table is required in the database to persist the relationship, we will use author_book table to persist the relationship between Author and Book entities. Let's get started with Many-to-many relationship.
Many-to-Many Relationships
In case of many-to-many relationships, each row in table A linked to multiple rows in table B and vice versa. Here we will implement many-to-many relationship between two entries Author and Book. A book might be authored by multiple author and one author might author many books.
We are using two tables author and book and java entities Author and Book maps respectively. Here is the ER diagram
Java Persistence Objects:
The code for Author.java is given below:
package roseindia;
|
The code for Book.java is given below:
package roseindia;
|
Mapping xml file (author.hbm.xml:
This file contains the mapping for both the entities Author and Book which maps with the tables author and group respectively.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="roseindia.Author" table="author">
<id name="id" type="int" column="id" unsaved-value="0">
<generator class="increment" />
</id>
<property name="authorName" type="java.lang.String" column="authorName"
not-null="true" length="50" />
<set name="books" table="author_book" cascade="all">
<key column="authorId" />
<many-to-many column="bookId" class="roseindia.Book" />
</set>
</class>
<class name="roseindia.Book" table="book">
<id name="id" type="int" column="id" unsaved-value="0">
<generator class="native" />
</id>
<property name="bookName" type="java.lang.String" column="bookName"
not-null="true" length="250" />
<set name="authors" table="author_book" cascade="all">
<key column="bookId" />
<many-to-many column="authorId" class="roseindia.Author" />
</set>
</class>
</hibernate-mapping>
The following mapping tag defines the many-to-many mapping between Author and Book entities from the Author entity.
<set name="books" table="author_book" cascade="all">
<key column="authorId" />
<many-to-many column="bookId" class="roseindia.Book" />
</set>
Here table="author_book" is the name of table which is used to define the relationships between the two entities and authorId is the foreign key of table author. Similarly bookId is the foreign key of book table. Following code defines the many-to-many relationships from the Book entity.
<set name="authors" table="author_book" cascade="all">
<key column="bookId" />
<many-to-many column="authorId" class="roseindia.Author" />
</set>
In Group.java we have stories variable of the list type, so we have used the <list../> tag here. The <one-to-many ../> specifies the one to many relationships to Story entity.
Running the Program:
To run and test the program you have to execute the ManyToManyRelation.java. Here is the full code of ManyToManyRelation.java file:
package roseindia;
|
The above code is self explanatory, we are creating the objects of Author:
Author author = new Author();
Then set the author name:
author.setAuthorName("John");
The create Book set and add many books:
Set bookSet = new HashSet();
//Solaris
Book bookSolaris = new Book();
bookSolaris.setBookName("Solaris in a week");
bookSet.add(bookSolaris);
//Linux
Book bookLinux = new Book();
bookLinux.setBookName("Linux in a week");
bookSet.add(bookLinux);
//Oracle
Book bookOracle = new Book();
bookOracle.setBookName("Oracle in a week");
bookSet.add(bookOracle);
Finally add the book set to the author object and save the author:
//Add book set to the author object
author.setBooks(bookSet);
session.save(author);
The above code will save author and the books authored by the author. You can also un-comment the comment the code in ManyToManyRelation.java and try more examples such as many Authors authoring the same book. Here is output of the program when executed in Eclipse IDE.
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select max(id) from author
Hibernate: insert into author (authorName, id) values (?, ?)
Hibernate: insert into book (bookName) values (?)
Hibernate: insert into book (bookName) values (?)
Hibernate: insert into book (bookName) values (?)
Hibernate: insert into author_book (authorId, bookId) values (?, ?)
Hibernate: insert into author_book (authorId, bookId) values (?, ?)
Hibernate: insert into author_book (authorId, bookId) values (?, ?)
Done
In this section we learned about the Many-to-one relationships in Hibernate.