In the previous article we saw how to use Hibernate as a middleware layer between the class structure of our Java program and the representation of instances of these classes in a database.
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.
Tuesday, April 17, 2012
Sunday, April 15, 2012
Using Hibernate for Java - 1
Hibernate is a framework for object-relational mapping with Java language. It facilitates mapping classes, attributes and class relationships between an application class model and its implementation in a relational database. For defining the mappings, XML files or Java anotations can be used.
Hibernate works as a middleware layer between the Java objects and the relational database.
In this article, and in the next one, we will use Hibernate version 3 to ilustrate, through a small example, the utilization of Hibernate in a Java program.
As database system we will be using Oracle 10g.
To start with, we need to crate the database tables. In the article Example of Java Swing Application with Oracle Database - 1 this process is explained.
In this article, we will only be mapping class BOOK.
We create a package for the classes corresponding to database tables, and create class Book:
Subscribe to:
Posts (Atom)