The previous article illustrated the mapping between a Java class and a database table, namelly the class Book and the table with the same name.
Let us now see how to map the class relationships, of type 1-to-N and N-to-N, to the database.
Let's start by adding to our model, which only had one class, two new classes:
- BookCopy (A Book contains the information of a book, and is related to several BookCopy, which correspond to the book copies a library has of that book) --> 1-N relationship.
- Author (A Book has several Author and vice-versa) --> relação N-N relationship.
In the Java project, developed in the previous post, let's start by adding class BookCopy and the respective Hibernate mapping file BookCopy.hbm.xml.
Class BookCopy:
Note that, in BookCopy, we must declare an instance of Book, which corresponds to the attribute, with cardinality 1, imported by the relation between Book and BookCopy.
File BookCopy.hbm.xml:
In class Book, we need to add an instance of Set<BookCopy>, corresponding to the attribute, of cardinality 0..*, imported by Book from the relation between Book and BookCopy:
File Book.hbm.xml after modification:
Note that, in the Book's mapping file, we added:
<set name="bookcopies" table="BOOKCOPY" inverse="true" lazy="true" fetch="select">
<key column="BOOKID" not-null="true"/>
<one-to-many class="javahibernateexample.beans.BookCopy" />
</set>
<key column="BOOKID" not-null="true"/>
<one-to-many class="javahibernateexample.beans.BookCopy" />
</set>
corresponding to the bookcopies associated to a given Book.
And, in BookCopy's mapping file, we put:
<many-to-one name="book" class="javahibernateexample.beans.Book" fetch="select">
<column name="BOOKID" not-null="true" />
</many-to-one>
<column name="BOOKID" not-null="true" />
</many-to-one>
corresponding to the book associated to a given BookCopy.
--------------------------------------------------------------------------------------------
Let us now add the information relative to the authors.
For that, let's create class Author:
And the respetive hibernate's mapping file Author.hbm.xml:
Class Book needs, now, to reference the book's authors, through an attribute of type Set<Author>:
In the end, the file Book.hbm.xml will look like this:
Note the attribute authors, in Book:
<set name="authors" table="BOOKAUTHOR" cascade="all">
<key column="BOOKID" />
<many-to-many column="AUTHORID" class="javahibernateexample.beans.Author" />
</set>
<key column="BOOKID" />
<many-to-many column="AUTHORID" class="javahibernateexample.beans.Author" />
</set>
and the corresponding attribute books in Author:
<set name="books" table="BOOKAUTHOR" cascade="all">
<key column="AUTHORID" />
<many-to-many column="BOOKID" class="javahibernateexample.beans.Book" />
</set>
<key column="AUTHORID" />
<many-to-many column="BOOKID" class="javahibernateexample.beans.Book" />
</set>
--------------------------------------------------------------------------------------------
It's still missing the reference to the new resources in file hibernate.cfg.xml, which we shall now add:
We can now use the created infrastructure with some test cases:
Some test results:
Finally, we may also modify, in the hibernate configuration file, property hibernate.show_sql to false, which will hide all the SQL output generated by the hibernate:
Testes output with show_sql set to false:
I hope you were able to follow this example and that you are ready to use hibernate in your future projects.
Thanks for this incredibly useful and easy to follow howto
ReplyDelete