Professional Documents
Culture Documents
Overview of Hibernate - II
Overview of Hibernate - II
II
Objectives
In this session, you will learn to:
Mapping value type objects
Mapping collection
Mapping database relationships
Mapping class Inheritance
Mapping Value Type Objects
While developing an application, you may find that a common property
is shared among different classes of the application.
Sometimes, shared property consists of multiple constituent elements.
For example, your application consists of two classes, Employee and
Student, and both these classes have a common property named
address.
To accomplish reusability and modularity in your application, you can:
Create a Java class to represent the shared property.
Refer the Java class from any class in the application.
import java.util.Set;
public class Authors public void setAuthorName(String
{ authorName) {
private String authorID; this.authorName =
private String authorName; authorName;
private Set<Books> books; }
AUTHORS
BOOKID
AUTHORID
AUTHORID
AUTHORNAME
BOOKNAME
Identifying Mapping of Collection (Contd.)
You can map the Authors and Books class with the AUTHORS and
BOOKS tables in the Hibernate mapping file, as shown in the following
code snippet:
<hibernate-mapping>
<class name="Test.Authors" table="AUTHORS">
<id name="authorID" type="string">
<column name="AUTHORID" length="20" />
<generator class="assigned" />
</id>
<property name="authorName" type="string">
<column name="AUTHORNAME" length="40"
/>
</property>
<set cascade="all" name="BOOKS">
<key column="AUTHORID"/>
<one-to-many class="Test.Books"/>
</set>
</class>
<class name="Test.Books" table="BOOKS">
<id name="bookID" type="string">
<column name="BOOKID" length="20"/>
<generator class="assigned"/>
</id>
<property name="bookName" type="string">
<column name="BOOKNAME" length="40"/>
</property>
</class>
</hibernate-mapping>
Identifying Mapping of Collection (Contd.)
The <list> element:
Is used to represent a collection in which duplicate elements are allowed
and the elements are stored in ascending order.
Requires an additional index column in the collection table, which maintains
the position of each element in the collection.
The column values that define the position of each element in the collection
are referred to as the index values.
Contains attributes and sub elements similar to the <set> element.
Contains the <list-index> sub-element.
Identifying Mapping of Collection (Contd.)
For example:
package Test;
import
java.util.List;
public
{ . .class
. . .Authors
. .
private List<Books> books;
. . . . . . . . . .
public getBooks() {
List<Books>
return books;
public void setBooks(List<Books>
}
books) {
this.books = books;
}
}
<hibernate-mapping>
. . . . . . . .
<list cascade="all" name="BOOKS">
<key column="authorid" />
<list-index column="bookid"/>
<one-to-many class="Test.Books"/>
</list>
. . . . . . .
. . . . . . .
</hibernate-mapping>
Specifies that the Books class is mapped with the BOOKS table as a List
type collection by using the <list> element.
The bookid column of the BOOKS table is defined as the column that
stores the index values.
Identifying Mapping of Collection (Contd.)
The <bag> element:
Represents a collection of objects that can have duplicates.
Does not have any order and is similar to an ArrayList collection without
an index.
Is implemented by using either the java.util.List or
java.util.Collection collection interfaces.
Is used only when the collection used in the application may contain
duplicate values and does not maintain the order of its elements.
Is mapped using the <bag> element.
Contains attributes and sub elements similar to the <set> element.
Identifying Mapping of Collection (Contd.)
For example:
package Test;
import java.util.Collection;
public class Authors
{
. . . . . . .
private Collection<Books> books;
. . . . . . .
}
public Collection<Books> getBooks() {
return books;
}
public void setBooks(Collection<Books>
books) {
this.books = books;
}
}
The <bag> element is used to map the Books class with the BOOKS
table as a collection.
The <one-to-many> element specifies the one-to-many relationship
between the AUTHORS and BOOKS tables.
Activity: Saving a Collection in Hibernate
Consider a scenario where the student have lots of address. So in this
case you need to create more number of embedded object in the
student class, which is so tedious at certain point. Therefore, to solve this
problem you have decided to use Collection. This collection can be a list,
set, map, collection, sorted set, sorted map. Application should be based
on the following guideline.
Student table contains column, such as ID, name, and address however
member variable of an address column, such as street, city, state, and pin
code are object instead of a simple data type.
Mapping Database Relationships
Consider the following tables:
Employee Employee Employee Department Department Department Department
ID Name Address ID ID Name Manager
E1 Peter London D1 D1 Sales Tom
EMPLOYEE DEPARTMENT
In this case, the EMPLOYEE table has a relationship with the
DEPARTMENT table because the department ID entered in the EMPLOYEE
table must exist in the DEPARTMENT table.
To access and store the application data in these tables you need to
create two classes, Employee and Department.
You need to map these classes with the EMPLOYEE and DEPARTMENT
tables, respectively.
In addition, you need to map the relationship between these two tables
by defining the relationship between the Employee and Department
classes. This is referred to as mapping the database relationships in a
Web application.
Mapping Database Relationships (Contd.)
At times, you may need to manage the relationships between the
persistent classes in the application.
The persistent classes are also known as entity classes.
For this, Hibernate allows you to map relationships among entity classes
with the relationships among the tables in the mapping file.
Mapping Entity Association
Hibernate allows you to map relationships among the database tables in
the Hibernate mapping file.
These relationships can be of the following types:
One-to-one
One-to-many
Many-to-many
Mapping Entity Association
One-to-one association:
Consider a scenario where to store the details of the persons, you have
created two tables named PERSON and PERSONINFO in the database
PERSONID PERSONNAME INFOID INFOID ADDRESS JOB INCOME
PERSON PERSONINFO
For each person in the PERSON table, a unique record should be available in
the PERSONINFO table.
For each person, a unique record should be inserted in both the PERSON
and PERSONINFO tables.
You can link these tables by using one-to-one association.
Mapping Entity Association (Contd.)
To implement the required functionality you need to create the following
class that represents the person entity of the application:
<hibernate-mapping>
<class name="Test.Person" table="PERSON">
<id name="personID" type="string">
<column name="PERSONID" length="10" />
<generator class="assigned" />
</id>
<property name="personName" type="string">
<column name="PERSONNAME" length="40" />
</property>
<many-to-one name="personDetail"
class="Test.PersonInfo" column="INFOID" not-
null="true" cascade="all" unique="true" />
</class>
</hibernate-mapping>
Mapping Entity Association (Contd.)
One-to-many association:
Consider the scenario where you have created the application that stores
the details of authors and the books written by them in the database tables.
In this application, when a record is inserted in the AUTHORS table, a
corresponding record must be inserted in the BOOKS table that contains the
details of the books written by that author. In addition, an author might have
written multiple books. Therefore, for an author record, multiple books
records may exist in the BOOKS table.
Mapping Entity Association (Contd.)
You need to map the classes with the tables in the database that contains
the one-to-many relationship.
For this, you can use the <one-to-many> element in the
Authors.hbm.xml mapping file, as shown in the following code snippet:
<hibernate-mapping>
<class name="Test.Authors"
table="AUTHORS"> <class name="Test.Books"
<id table="Books">
name="authorID" <id name="bookID"
type="string">
<colum name="AUTHORID" type="string">
n length="20" /> <column name="bookid"
<generato length="20"/>
r class="assigned" /> <generator
</id> class="assigned"/>
<propert name="authorName" </id>
y type="string"> <property
<column name="bookName"
name="AUTHORNAME" length="40" type="string">
/> <column
</property> name="bookname" length="40"/>
<set </property>
cascade="all"
<key </class>
name="BOOKS">
column="authorid"/> </hibernate-mapping>
<one-to-many
class="Test.Books"/
>
</set>
</class>
Mapping Entity Association (Contd.)
Many-to-many association:
Consider a scenario where an university offers multiple courses. In each
course, several students can be registered. In addition, a student can register
himself or herself in multiple courses simultaneously. Therefore, you decide
to create three tables, STUDENT, COURSE, and REGISTRATION. The STUDENT
table contains student information by using the various fields, such as
StudentID, StudentName, and StudentAddress. The COURSE table
contains course information by using the various fields, such as CourseID,
CourseName, CourseDuration, and CourseFee. However, the
REGISTRATION table contains registration information by using the various
fields, such as StudentID and CourseID.
Mapping Entity Association (Contd.)
You can use the following code snippet to map the Student class with the
STUDENT table in the Student.hbm.xml file:
<hibernate-mapping>
<class name="Test.Student" table="STUDENT">
<id name="studentID" type="string">
<column length="10" name="STUDENTID"/>
<generator class="assigned"/>
</id>
<property name="studentName" type="string">
<column length="40" name="STUDENTNAME"/>
</property>
<property name="studentAddress" type="string">
<column length="100" name="STUDENTADDRESS"/>
</property>
<set cascade="all" name="courses" table="REGISTRATION">
<key column="STUDENTID"/>
<many-to-many class="Test.Course" column="COURSEID"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="Test.Books" table="BOOKDETAILS">
......
<discriminator column="BOOKTYPE" type= "string"/>
......
<subclass name="Test.SEBooks" discriminator-
value="SE">
......
</subclass>
<subclass name="Test.INBooks" discriminator-
value="IN">
......
</subclass> </class>
</hibernate-mapping>
<hibernate-mapping>
<class name="Test.Books" table="BOOKS">
......
<property name="bookTitle" type="string">
<column length="20" name="BOOKTITLE"/>
</property>
......
<joined-subclass name="Test.SEBooks" table="SEBOOKS">
<key column="seBookID"/>
<property name="specialFeatures" type="string">
<column length="200" name="SPECIALFEATURES"/>
</property> </joined-subclass>
......
</class>
</hibernate-mapping>
Mapping Class Inheritance (Contd.)
Key points of Table per subclass are:
In the table per subclass, the inheritance relationship is mapped by using
the foreign key associations in the tables.
You do not require separate queries for the tables that store objects of the
subclasses.
If the properties of the super classes change, you do not need to make any
change in the columns of the tables that store the objects of the subclasses.
Activity: Implementing Inheritance Mapping in Hibernate
Consider a scenario where you have been asked to create a Java
application that implement Table per Class inheritance mapping. The
Application should be based on the following guideline:
Employee is the super class for PermanentEmployee and
ContractEmployee classes, as shown in the following figure.
Employee
int empID
String EmpName
PermanentEmployee ContractEmployee
String companyName String ContractorName
Summary
To accomplish reusability and modularity in your application, you can:
Create a Java class to represent the shared property.
Refer the Java class from any class in the application.
The value types:
Are mapped as a component of the entity class.
Are mapped by using the <component> element in the Hibernate
mapping file.
The mapping of an entity class is configured under the <id> tag that is
used to create a unique identifier for its object. However, the component
class is configured under a <component> tag.
The commonly used collection mapping elements in the Hibernate
mapping file are:
<set>
<list>
<bag>
Summary (Contd.)
Hibernate allows you to map relationships among entity classes with the
relationships among the tables in the mapping file.
Hibernate allows you to map relationships in the following form:
One-to-one
One-to-many
Many-to-many