SlideShare a Scribd company logo
1 of 49
Download to read offline
© People Strategists www.peoplestrategists.com Slide 1 of 49
Overview of Hibernate -II
© People Strategists www.peoplestrategists.com Slide 2 of 49
In this session, you will learn to:
Mapping value type objects
Mapping collection
Mapping database relationships
Mapping class Inheritance
Objectives
© People Strategists www.peoplestrategists.com Slide 3 of 49
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.
This Java class is referred to as the value type.
The value types:
Are mapped as a component of the entity class.
Are mapped by using the <component> element in the Hibernate
mapping file.
Mapping Value Type Objects
© People Strategists www.peoplestrategists.com Slide 4 of 49
Consider a scenario where you are developing an application that stores
information about the orders placed by different customers of an
organization in a database. For this, you have created the ORDERS table
by using the following query:
To map the ORDERS table, a Java class named OrderDetails is created
by using the following code snippet:
Mapping Value Type Objects (Contd.)
CREATE TABLE ORDERS ( ORDER_ID INT PRIMARY KEY,
ONLINE_RECIPIENT VARCHAR(40), ONLINE_ADDRESS
VARCHAR(100), OFFLINE_RECIPIENT VARCHAR(40),
OFFLINE_ADDRESS VARCHAR(100))
ORDERS table
public class OrderDetails{
private int orderID;
private String onlineRecipient;
private String onlineAddress;
private String offlineRecipient;
private String offlineAddress;
//getter and setter methods to set the properties
. . . . .
}
OrderDetails class
© People Strategists www.peoplestrategists.com Slide 5 of 49
A solution to the preceding problem is to create a new class that
encapsulates the contact details of the customers.
A separate class, ContactDetails is created to encapsulate the
address of the customers, as shown in the following code snippet:
Mapping Value Type Objects (Contd.)
public class ContactDetails
{
private String recipient;
private String address;
// getter and setter methods
. . . . . .
}
© People Strategists www.peoplestrategists.com Slide 6 of 49
Two different instances of the ContactDetails class for the online and
offline contact details can be created.
The OrderDetails class can use these instances to specify the online
and offline contact details, as shown in the following code snippet:
The contact details are dependent on the OrdersDetails class. This
implies that you cannot model the ContactDetails class as an
independent persistent class.
It needs to be associated as a component of an entity class that has a
database identity.
Therefore, the OrderDetails class is an entity class and the
ContactDetails class is modeled as its component.
Mapping Value Type Objects (Contd.)
public class OrderDetails {
private int orderID;
private ContactDetails onlineContact;
private ContactDetails offlineContact;
// getter and setter methods
. . . . . . .
}
© People Strategists www.peoplestrategists.com Slide 7 of 49
In Hibernate applications, the mapping of a component class is different
from the mapping of an entity class.
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 following code snippet shows the mapping of the OrderDetails
and ContactDetails classes in the Hibernate mapping file:
Mapping Value Type Objects (Contd.)
<class name="OrderDetails" table="ORDER">
<id name="orderID" type="int" column="ORDER_ID">
<generator class="native" />
</id>
<component name="onlineContact" class="ContactDetails">
<property name="recipient" type="string" column="ONLINE_RECIPIENT" />
<property name="address" type="string" column="ONLINE_ADDRESS"/>
</component>
<component name="offlineContact" class="ContactDetails">
<property name="recipient" type="string" column="OFFLINE_RECIPIENT" />
<property name="address" type="string" column="OFFLINE_ADDRESS" />
</component>
. . . . . . . . . .
</class>
</hibernate-mapping>
© People Strategists www.peoplestrategists.com Slide 8 of 49
Consider a scenario where you need to create a Java application that
implement mapping of the value type object. 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.
Application should list following tasks as an option:
 1. for Insert
 2. for List
 3. for Update
 4. for Delete
Activity: Mapping Value Type Object
© People Strategists www.peoplestrategists.com Slide 9 of 49
When a property of a class is declared as a collection, you need to
perform collection mapping in the Hibernate mapping file.
The commonly used collection mapping elements in the Hibernate
mapping file are:
<set>
<list>
<bag>
Identifying Mapping of Collection
© People Strategists www.peoplestrategists.com Slide 10 of 49
The <set> element:
Is used when the duplicate elements are not required in the collection.
Does not maintain the order of its elements in the collection.
Is used to map an attribute of the java.util.Set collection type in the
Hibernate mapping file.
Uses the following sub elements and attributes to achieve mapping
between the application and the database tables:
 cascade attribute: Specifies that the operations performed on an entity update
the related entities also.
 name attribute: Specifies the name of the database table that stores the
collection objects
 <key> element: Specifies the properties that associate the entity and the
collection objects
 <one-to-many> element: Specifies a one-to-many relationship between the
entity and the collection objects. It contains the class attribute. This attribute
contains the name of the collection object.
Identifying Mapping of Collection (Contd.)
© People Strategists www.peoplestrategists.com Slide 11 of 49
Consider a scenario, you are developing an application that stores the
details of authors and the books written by them. For this, you have
created the Authors class that contains the following code snippet:
Identifying Mapping of Collection (Contd.)
import java.util.Set;
public class Authors
{
private String authorID;
private String authorName;
private Set<Books> books;
public String getAuthorID()
{
return authorID;
}
public void
setAuthorID(String authorID) {
this.authorID =
authorID;
}
public String getAuthorName()
{
return authorName;
}
public void setAuthorName(String
authorName) {
this.authorName =
authorName;
}
public Set<Books> getBooks()
{
return books;
}
public void
setBooks(Set<Books> books) {
this.books = books;
}
}
© People Strategists www.peoplestrategists.com Slide 12 of 49
In the preceding slide, the books property is a collection of instances of
the Books class. The Authors class is called an entity which owns the
collection of the Books class.
Identifying Mapping of Collection (Contd.)
package Test;
public class Books {
private String bookID;
private String bookName;
public String getBookID() {
return bookID;
}
public void setBookID(String
bookID) {
this.bookID = bookID;
}
public String getBookName() {
return bookName;
}
public void setBookName(String
bookName) {
this.bookName = bookName;
}
}
© People Strategists www.peoplestrategists.com Slide 13 of 49
The collection objects are distinguished in the database by the foreign
key of the entity that owns the collection.
This foreign key is referred to as the collection key column. The collection
key column is mapped by the <key> element in the mapping file.
The following figure shows how the author and book details are stored in
the database.
Identifying Mapping of Collection (Contd.)
AUTHORS
AUTHORID
AUTHORNAME
Books
BOOKID
AUTHORID
BOOKNAME
© People Strategists www.peoplestrategists.com Slide 14 of 49
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:
Identifying Mapping of Collection (Contd.)
<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>
© People Strategists www.peoplestrategists.com Slide 15 of 49
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.)
© People Strategists www.peoplestrategists.com Slide 16 of 49
For example:
The Authors class declares the books property as a collection of type,
List.
Identifying Mapping of Collection (Contd.)
package Test;
import java.util.List;
public class Authors
{ . . . . . . .
private List<Books> books;
. . . . . . . . . .
public List<Books> getBooks() {
return books;
}
public void setBooks(List<Books>
books) {
this.books = books;
}
}
© People Strategists www.peoplestrategists.com Slide 17 of 49
Now, consider the following code snippet:
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.)
<hibernate-mapping>
. . . . . . . .
<list cascade="all" name="BOOKS">
<key column="authorid" />
<list-index column="bookid"/>
<one-to-many class="Test.Books"/>
</list>
. . . . . . .
. . . . . . .
</hibernate-mapping>
© People Strategists www.peoplestrategists.com Slide 18 of 49
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.)
© People Strategists www.peoplestrategists.com Slide 19 of 49
For example:
The preceding code, declares the books property of the Authors
class as a collection of type, Collection.
Identifying Mapping of Collection (Contd.)
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;
}
}
© People Strategists www.peoplestrategists.com Slide 20 of 49
To map the Books class as a bag collection in the Hibernate mapping
file, you need to use the <bag> element, as shown in the following code
snippet:
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.
Identifying Mapping of Collection (Contd.)
<hibernate-mapping>
. . . . . .
<bag cascade="all" name="BOOKS">
<key column="authorid" />
<one-to-many class="Test.Books"/>
</bag>
. . . . . .
</hibernate-mapping>
© People Strategists www.peoplestrategists.com Slide 21 of 49
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.
Activity: Saving a Collection in Hibernate
© People Strategists www.peoplestrategists.com Slide 22 of 49
Consider the following tables:
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
Department
ID
Department
Name
Department
Manager
D1 Sales Tom
D2 HR Sam
Employee
ID
Employee
Name
Employee
Address
Department
ID
E1 Peter London D1
E2 Tom New York D1
EMPLOYEE DEPARTMENT
© People Strategists www.peoplestrategists.com Slide 23 of 49
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 Database Relationships (Contd.)
© People Strategists www.peoplestrategists.com Slide 24 of 49
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
© People Strategists www.peoplestrategists.com Slide 25 of 49
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
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
PERSONID PERSONNAME INFOID
P1 Peter E1
P2 Tom E2
PERSON PERSONINFO
INFOID ADDRESS JOB INCOME
E1 New York Teacher 5000
E2 Shanghai Doctor 6000
© People Strategists www.peoplestrategists.com Slide 26 of 49
To implement the required functionality you need to create the following
class that represents the person entity of the application:
Mapping Entity Association (Contd.)
package Test;
public class Person {
private String personID;
private String personName;
private PersonInfo
personDetail;
public Person() { }
public Person(String
personID) {
this.personID = personID;
}
public Person(String
personID, String personName,
PersonInfo personDetail) {
this.personID = personID;
this.personName =
personName;
this.personDetail =
personDetail;
}
public String getPersonID() {
return this.personID;
}
public void setPersonID(String
personID) {
this.personID = personID;
}
public String getPersonName()
{
return this.personName;
}
public void
setPersonName(String personName)
{
this.personName =
personName;
}
public PersonInfo
getPersonDetail() {
return this.personDetail;
}
public void
setPersonDetail(PersonInfo
personDetail) {
this.personDetail =
personDetail;
}
}
© People Strategists www.peoplestrategists.com Slide 27 of 49
The personDetail property refers to an instance of the PersonInfo class.
You can create the PersonInfo class by using the following code snippet:
Mapping Entity Association (Contd.)
package Test;
public class PersonInfo {
private String infoID;
private String address;
private String job;
private Integer income;
public PersonInfo() {
}
public PersonInfo(String infoID)
{
this.infoID = infoID;
}
public PersonInfo(String infoID,
String address, String job, Integer
income) {
this.infoID = infoID;
this.address = address;
this.job = job;
this.income = income;
}
public String getInfoID() {
return this.infoID;
}
public void setInfoID(String
infoID) {
this.infoID = infoID;
}
public String getAddress() {
return this.address;
}
public void setAddress(String
address) {
this.address = address;
}
public String getJob() {
return this.job;
}
public void setJob(String job) {
this.job = job;
}
public Integer getIncome() {
return this.income;
}
public void setIncome(Integer
income) {
this.income = income;
}
}
© People Strategists www.peoplestrategists.com Slide 28 of 49
you need to map the one-to-one association between the classes in the
Hibernate mapping file. For this, you can use the <many-to-one> element
in the Hibernate mapping file. This element contains the following
attributes:
 name: Specifies the property name that is used to associate the classes.
 class: Specifies the fully qualified name of the associated class that provides
value for the property specified by the name attribute.
 column: Specifies the name of the foreign key column that stores the value of
the property specified by the name attribute.
 not-null: Specifies that the foreign key column cannot contain null values, if
set to true.
 cascade: Specifies that the operations performed on one table will also affect
the other table with similar updates.
 unique: Specifies that the foreign key column contains unique values. This
attribute makes the link between the tables as a one-to-one association.
Mapping Entity Association (Contd.)
© People Strategists www.peoplestrategists.com Slide 29 of 49
To map the PERSON table with the Person class, you can use the following
code snippet in the Person.hbm.xml file:
Mapping Entity Association (Contd.)
<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>
© People Strategists www.peoplestrategists.com Slide 30 of 49
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.)
© People Strategists www.peoplestrategists.com Slide 31 of 49
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:
Mapping Entity Association (Contd.)
<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>
© People Strategists www.peoplestrategists.com Slide 32 of 49
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.)
© People Strategists www.peoplestrategists.com Slide 33 of 49
You can use the following code snippet to map the Student class with the
STUDENT table in the Student.hbm.xml file:
In the preceding code snippet, the <many-to-many> element is used to map
the association between the Student and Course classes.
Mapping Entity Association (Contd.)
<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>
© People Strategists www.peoplestrategists.com Slide 34 of 49
Consider a scenario where you need to implement one to one associate
in hibernate application. Your Application should be based on the
following guide lines:
Student table contains column, such as ID, name, and address however
member variable of an address column, such as street, city, state, and zip
code are object instead of a simple data type.
Activity: Implementing One-to-One Association in Hibernate
Application
© People Strategists www.peoplestrategists.com Slide 35 of 49
Consider a scenario where you need to develop an application that
stores the details of the books sold by a book store. Note that book store
sells different types of books, such as special edition books and books
that are published in multiple languages. Therefore, you decide to create
three classes, Books, SEBooks, and INBooks in the application. The
Books class defines the common properties of different types of books,
such as bookID, bookTitle, author, and cost. However, the SEBooks
and INBooks classes extend the Books class and define their specific
properties, such as specialfeatures and booklanguage.
Mapping Class Inheritance
© People Strategists www.peoplestrategists.com Slide 36 of 49
The Java classes are associated with each other with an inheritance
relationship.
However, the inheritance relationship cannot be achieved between the
tables of a database.
Therefore, you need to explicitly map the inheritance relationship of
classes in the Hibernate mapping file while mapping the classes with the
database tables.
Hibernate provides the following types of mapping to support the
inheritance hierarchy:
Table per concrete class
Table per subclass
Table per class
Mapping Class Inheritance (Contd.)
© People Strategists www.peoplestrategists.com Slide 37 of 49
Table per concrete class:
In the table per concrete class mapping, a table is created for each class in
the inheritance hierarchy.
Each table defines columns for all the properties of the class including the
inherited properties.
After creating the tables and the classes, you need to map them in the
Hibernate mapping file.
You can use the <union-subclass> element to map the subclasses in the
mapping file.
This element contains the following commonly used attributes:
 name: Specifies the name of the subclass.
 table: Specifies the name of the table that is to be mapped with the subclass.
Mapping Class Inheritance (Contd.)
© People Strategists www.peoplestrategists.com Slide 38 of 49
For example:
The <class> element provides mapping information of the super class,
Book.
The <union-subclass> element is used to map each subclass with
the corresponding tables in the database.
Mapping Class Inheritance (Contd.)
<hibernate-mapping>
<class name="Test.Books" table="BOOKS">
......
<union-subclass name="Test.SEBooks"
table="SEBOOKS">
<property name="specialFeatures" type="string">
<column length="200" name="SPECIALFEATURES"/>
</property> </union-subclass>
<union-subclass name="Test.INBooks"
table="INBOOKS">
<property name="bookLanguage" type="string">
<column length="20" name="BOOKLANGUAGE"/>
</property> </union-subclass> </class>
</hibernate-mapping>
© People Strategists www.peoplestrategists.com Slide 39 of 49
Implementing the table per concrete class mapping strategy has the
following drawbacks:
For each property mapped in the super class, the column name must be the
same in all the subclass tables.
Each table defines columns for all properties of the class including inherited
properties. This results in duplication of values.
A separate query is needed to retrieve data from each table that stores the
data of the concrete class.
A change in the super class property results in the change of the various
columns of the subclass tables.
Mapping Class Inheritance (Contd.)
© People Strategists www.peoplestrategists.com Slide 40 of 49
Table per class:
In the table per class hierarchy mapping strategy, a single table is created for
all the classes in the inheritance hierarchy.
It eliminates the repetitive column names in the tables created for each
class in the class hierarchy.
However, you need to create an additional column in the table that
distinguishes the objects of the subclasses.
This column is referred to as the discriminator.
The values stored in this column are used to identify the rows represented
by the subclasses.
Mapping Class Inheritance (Contd.)
© People Strategists www.peoplestrategists.com Slide 41 of 49
The following elements are used to achieve table per class hierarchy
mapping:
<discriminator>:
 Specifies the settings about the discriminator column with the help of the
following attributes:
 column: Specifies the name of the discriminator column.
 type: Specifies the data type of the values stored in the discriminator column.
<subclass>:
 Specifies the settings about the subclasses with the help of the following
attributes:
 name: Specifies the name of the subclass.
 discriminator-value: Specifies a discriminator value that is used for the
current subclass.
Mapping Class Inheritance (Contd.)
© People Strategists www.peoplestrategists.com Slide 42 of 49
For example:
The BOOKTYPE column of the BOOKDETAILS table is specified as the
discriminator column.
IN and SE are specified as the discriminator values that distinguish the
objects of the INBooks and SEBooks subclasses.
Mapping Class Inheritance (Contd.)
<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>
© People Strategists www.peoplestrategists.com Slide 43 of 49
Key points of Table per class are:
The table per class hierarchy simplifies the mapping and you do not need to
create multiple tables with repetitive column names.
However, if the inheritance hierarchy increases and contains multiple
classes, the size of the table increases.
In addition, when a single table is created for all the classes in the
inheritance hierarchy, the columns that are not common among classes
contain null values.
Therefore, if the class hierarchy increases, the number of null values stored
in the table also increases. This affects the data integrity of the database.
Mapping Class Inheritance (Contd.)
© People Strategists www.peoplestrategists.com Slide 44 of 49
Table per subclass:
In the table per subclass mapping strategy, a separate table is created for
each class.
However, columns are not repeated for the inherited properties in the table
that stores the objects of the subclass.
The table that stores the objects of the super class contains the primary key,
which is used as a foreign key in the tables that stores the objects of the
subclasses.
To achieve the table per subclass mapping, you need to use the <joined-
subclass> element in the hibernate mapping file.
Mapping Class Inheritance (Contd.)
© People Strategists www.peoplestrategists.com Slide 45 of 49
For example:
Mapping Class Inheritance (Contd.)
<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>
© People Strategists www.peoplestrategists.com Slide 46 of 49
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.
Mapping Class Inheritance (Contd.)
© People Strategists www.peoplestrategists.com Slide 47 of 49
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.
Activity: Implementing Inheritance Mapping in Hibernate
Employee
int empID
String EmpName
PermanentEmployee
String companyName
ContractEmployee
String ContractorName
© People Strategists www.peoplestrategists.com Slide 48 of 49
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
© People Strategists www.peoplestrategists.com Slide 49 of 49
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
Hibernate provides the following types of mapping to support the
inheritance hierarchy:
Table per concrete class
Table per subclass
Table per class
Summary (Contd.)

More Related Content

What's hot

Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentationManav Prasad
 
Hibernate
HibernateHibernate
HibernateAjay K
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Examplekamal kotecha
 
Learn about dot net attributes
Learn about dot net attributesLearn about dot net attributes
Learn about dot net attributessonia merchant
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate pptAneega
 
Spring (1)
Spring (1)Spring (1)
Spring (1)Aneega
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepGuo Albert
 
How do i connect to that
How do i connect to thatHow do i connect to that
How do i connect to thatBecky Bertram
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate TutorialRam132
 
Hibernate Developer Reference
Hibernate Developer ReferenceHibernate Developer Reference
Hibernate Developer ReferenceMuthuselvam RS
 

What's hot (19)

JSP Technology I
JSP Technology IJSP Technology I
JSP Technology I
 
Spring Framework -I
Spring Framework -ISpring Framework -I
Spring Framework -I
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
 
Hibernate in Action
Hibernate in ActionHibernate in Action
Hibernate in Action
 
Hibernate
HibernateHibernate
Hibernate
 
Hibernate tutorial
Hibernate tutorialHibernate tutorial
Hibernate tutorial
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
 
Learn about dot net attributes
Learn about dot net attributesLearn about dot net attributes
Learn about dot net attributes
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
 
Spring (1)
Spring (1)Spring (1)
Spring (1)
 
TY.BSc.IT Java QB U4
TY.BSc.IT Java QB U4TY.BSc.IT Java QB U4
TY.BSc.IT Java QB U4
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
Hibernate
HibernateHibernate
Hibernate
 
How do i connect to that
How do i connect to thatHow do i connect to that
How do i connect to that
 
TY.BSc.IT Java QB U5
TY.BSc.IT Java QB U5TY.BSc.IT Java QB U5
TY.BSc.IT Java QB U5
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
 
Hibernate Developer Reference
Hibernate Developer ReferenceHibernate Developer Reference
Hibernate Developer Reference
 
Spring framework DAO
Spring framework  DAOSpring framework  DAO
Spring framework DAO
 
TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3TY.BSc.IT Java QB U3
TY.BSc.IT Java QB U3
 

Similar to Mapping value types and collections in Hibernate

Joel Landis Net Portfolio
Joel Landis Net PortfolioJoel Landis Net Portfolio
Joel Landis Net Portfoliojlshare
 
Drupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.comDrupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.comJD Leonard
 
Evolving your Data Access with MongoDB Stitch - Drew Di Palma
Evolving your Data Access with MongoDB Stitch - Drew Di PalmaEvolving your Data Access with MongoDB Stitch - Drew Di Palma
Evolving your Data Access with MongoDB Stitch - Drew Di PalmaMongoDB
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5Mahmoud Ouf
 
Learning .NET Attributes
Learning .NET AttributesLearning .NET Attributes
Learning .NET AttributesPooja Gaikwad
 
Learn dot net attributes
Learn dot net attributesLearn dot net attributes
Learn dot net attributessonia merchant
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfoliomwillmer
 
Web-Dev Portfolio
Web-Dev PortfolioWeb-Dev Portfolio
Web-Dev Portfolionwbgh
 
ADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael PizzoADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael PizzoHasnain Iqbal
 
Assignment Examples Final 07 Oct
Assignment Examples Final 07 OctAssignment Examples Final 07 Oct
Assignment Examples Final 07 OctSriram Raj
 
Il 09 T3 William Spreitzer
Il 09 T3 William SpreitzerIl 09 T3 William Spreitzer
Il 09 T3 William Spreitzerwspreitzer
 
]project-open[ Data-Model “Categories”
]project-open[ Data-Model “Categories”]project-open[ Data-Model “Categories”
]project-open[ Data-Model “Categories”Klaus Hofeditz
 
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad NicheTech Com. Solutions Pvt. Ltd.
 
Graph db as metastore
Graph db as metastoreGraph db as metastore
Graph db as metastoreHaris Khan
 

Similar to Mapping value types and collections in Hibernate (20)

Joel Landis Net Portfolio
Joel Landis Net PortfolioJoel Landis Net Portfolio
Joel Landis Net Portfolio
 
Drupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.comDrupal 7 entities & TextbookMadness.com
Drupal 7 entities & TextbookMadness.com
 
Evolving your Data Access with MongoDB Stitch - Drew Di Palma
Evolving your Data Access with MongoDB Stitch - Drew Di PalmaEvolving your Data Access with MongoDB Stitch - Drew Di Palma
Evolving your Data Access with MongoDB Stitch - Drew Di Palma
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
 
Learning .NET Attributes
Learning .NET AttributesLearning .NET Attributes
Learning .NET Attributes
 
Learn dot net attributes
Learn dot net attributesLearn dot net attributes
Learn dot net attributes
 
.NET Portfolio
.NET Portfolio.NET Portfolio
.NET Portfolio
 
Web-Dev Portfolio
Web-Dev PortfolioWeb-Dev Portfolio
Web-Dev Portfolio
 
ADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael PizzoADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
 
Actionview
ActionviewActionview
Actionview
 
Entity framework1
Entity framework1Entity framework1
Entity framework1
 
Assignment Examples Final 07 Oct
Assignment Examples Final 07 OctAssignment Examples Final 07 Oct
Assignment Examples Final 07 Oct
 
Ch03
Ch03Ch03
Ch03
 
Ch03
Ch03Ch03
Ch03
 
Il 09 T3 William Spreitzer
Il 09 T3 William SpreitzerIl 09 T3 William Spreitzer
Il 09 T3 William Spreitzer
 
Django wrapper
Django wrapperDjango wrapper
Django wrapper
 
]project-open[ Data-Model “Categories”
]project-open[ Data-Model “Categories”]project-open[ Data-Model “Categories”
]project-open[ Data-Model “Categories”
 
CAD Report
CAD ReportCAD Report
CAD Report
 
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
 
Graph db as metastore
Graph db as metastoreGraph db as metastore
Graph db as metastore
 

More from People Strategists (20)

MongoDB Session 3
MongoDB Session 3MongoDB Session 3
MongoDB Session 3
 
MongoDB Session 2
MongoDB Session 2MongoDB Session 2
MongoDB Session 2
 
MongoDB Session 1
MongoDB Session 1MongoDB Session 1
MongoDB Session 1
 
Android - Day 1
Android - Day 1Android - Day 1
Android - Day 1
 
Android - Day 2
Android - Day 2Android - Day 2
Android - Day 2
 
Overview of web services
Overview of web servicesOverview of web services
Overview of web services
 
Identifing Listeners and Filters
Identifing Listeners and FiltersIdentifing Listeners and Filters
Identifing Listeners and Filters
 
Exploring Maven SVN GIT
Exploring Maven SVN GITExploring Maven SVN GIT
Exploring Maven SVN GIT
 
Agile Dev. II
Agile Dev. IIAgile Dev. II
Agile Dev. II
 
Agile Dev. I
Agile Dev. IAgile Dev. I
Agile Dev. I
 
XML Schemas
XML SchemasXML Schemas
XML Schemas
 
JSON and XML
JSON and XMLJSON and XML
JSON and XML
 
Ajax and Jquery
Ajax and JqueryAjax and Jquery
Ajax and Jquery
 
CSS
CSSCSS
CSS
 
HTML/HTML5
HTML/HTML5HTML/HTML5
HTML/HTML5
 
JDBC
JDBCJDBC
JDBC
 
RDBMS with MySQL
RDBMS with MySQLRDBMS with MySQL
RDBMS with MySQL
 
Java Day-7
Java Day-7Java Day-7
Java Day-7
 
Java Day-6
Java Day-6Java Day-6
Java Day-6
 
Final Table of Content
Final Table of ContentFinal Table of Content
Final Table of Content
 

Recently uploaded

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 

Recently uploaded (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 

Mapping value types and collections in Hibernate

  • 1. © People Strategists www.peoplestrategists.com Slide 1 of 49 Overview of Hibernate -II
  • 2. © People Strategists www.peoplestrategists.com Slide 2 of 49 In this session, you will learn to: Mapping value type objects Mapping collection Mapping database relationships Mapping class Inheritance Objectives
  • 3. © People Strategists www.peoplestrategists.com Slide 3 of 49 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. This Java class is referred to as the value type. The value types: Are mapped as a component of the entity class. Are mapped by using the <component> element in the Hibernate mapping file. Mapping Value Type Objects
  • 4. © People Strategists www.peoplestrategists.com Slide 4 of 49 Consider a scenario where you are developing an application that stores information about the orders placed by different customers of an organization in a database. For this, you have created the ORDERS table by using the following query: To map the ORDERS table, a Java class named OrderDetails is created by using the following code snippet: Mapping Value Type Objects (Contd.) CREATE TABLE ORDERS ( ORDER_ID INT PRIMARY KEY, ONLINE_RECIPIENT VARCHAR(40), ONLINE_ADDRESS VARCHAR(100), OFFLINE_RECIPIENT VARCHAR(40), OFFLINE_ADDRESS VARCHAR(100)) ORDERS table public class OrderDetails{ private int orderID; private String onlineRecipient; private String onlineAddress; private String offlineRecipient; private String offlineAddress; //getter and setter methods to set the properties . . . . . } OrderDetails class
  • 5. © People Strategists www.peoplestrategists.com Slide 5 of 49 A solution to the preceding problem is to create a new class that encapsulates the contact details of the customers. A separate class, ContactDetails is created to encapsulate the address of the customers, as shown in the following code snippet: Mapping Value Type Objects (Contd.) public class ContactDetails { private String recipient; private String address; // getter and setter methods . . . . . . }
  • 6. © People Strategists www.peoplestrategists.com Slide 6 of 49 Two different instances of the ContactDetails class for the online and offline contact details can be created. The OrderDetails class can use these instances to specify the online and offline contact details, as shown in the following code snippet: The contact details are dependent on the OrdersDetails class. This implies that you cannot model the ContactDetails class as an independent persistent class. It needs to be associated as a component of an entity class that has a database identity. Therefore, the OrderDetails class is an entity class and the ContactDetails class is modeled as its component. Mapping Value Type Objects (Contd.) public class OrderDetails { private int orderID; private ContactDetails onlineContact; private ContactDetails offlineContact; // getter and setter methods . . . . . . . }
  • 7. © People Strategists www.peoplestrategists.com Slide 7 of 49 In Hibernate applications, the mapping of a component class is different from the mapping of an entity class. 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 following code snippet shows the mapping of the OrderDetails and ContactDetails classes in the Hibernate mapping file: Mapping Value Type Objects (Contd.) <class name="OrderDetails" table="ORDER"> <id name="orderID" type="int" column="ORDER_ID"> <generator class="native" /> </id> <component name="onlineContact" class="ContactDetails"> <property name="recipient" type="string" column="ONLINE_RECIPIENT" /> <property name="address" type="string" column="ONLINE_ADDRESS"/> </component> <component name="offlineContact" class="ContactDetails"> <property name="recipient" type="string" column="OFFLINE_RECIPIENT" /> <property name="address" type="string" column="OFFLINE_ADDRESS" /> </component> . . . . . . . . . . </class> </hibernate-mapping>
  • 8. © People Strategists www.peoplestrategists.com Slide 8 of 49 Consider a scenario where you need to create a Java application that implement mapping of the value type object. 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. Application should list following tasks as an option:  1. for Insert  2. for List  3. for Update  4. for Delete Activity: Mapping Value Type Object
  • 9. © People Strategists www.peoplestrategists.com Slide 9 of 49 When a property of a class is declared as a collection, you need to perform collection mapping in the Hibernate mapping file. The commonly used collection mapping elements in the Hibernate mapping file are: <set> <list> <bag> Identifying Mapping of Collection
  • 10. © People Strategists www.peoplestrategists.com Slide 10 of 49 The <set> element: Is used when the duplicate elements are not required in the collection. Does not maintain the order of its elements in the collection. Is used to map an attribute of the java.util.Set collection type in the Hibernate mapping file. Uses the following sub elements and attributes to achieve mapping between the application and the database tables:  cascade attribute: Specifies that the operations performed on an entity update the related entities also.  name attribute: Specifies the name of the database table that stores the collection objects  <key> element: Specifies the properties that associate the entity and the collection objects  <one-to-many> element: Specifies a one-to-many relationship between the entity and the collection objects. It contains the class attribute. This attribute contains the name of the collection object. Identifying Mapping of Collection (Contd.)
  • 11. © People Strategists www.peoplestrategists.com Slide 11 of 49 Consider a scenario, you are developing an application that stores the details of authors and the books written by them. For this, you have created the Authors class that contains the following code snippet: Identifying Mapping of Collection (Contd.) import java.util.Set; public class Authors { private String authorID; private String authorName; private Set<Books> books; public String getAuthorID() { return authorID; } public void setAuthorID(String authorID) { this.authorID = authorID; } public String getAuthorName() { return authorName; } public void setAuthorName(String authorName) { this.authorName = authorName; } public Set<Books> getBooks() { return books; } public void setBooks(Set<Books> books) { this.books = books; } }
  • 12. © People Strategists www.peoplestrategists.com Slide 12 of 49 In the preceding slide, the books property is a collection of instances of the Books class. The Authors class is called an entity which owns the collection of the Books class. Identifying Mapping of Collection (Contd.) package Test; public class Books { private String bookID; private String bookName; public String getBookID() { return bookID; } public void setBookID(String bookID) { this.bookID = bookID; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } }
  • 13. © People Strategists www.peoplestrategists.com Slide 13 of 49 The collection objects are distinguished in the database by the foreign key of the entity that owns the collection. This foreign key is referred to as the collection key column. The collection key column is mapped by the <key> element in the mapping file. The following figure shows how the author and book details are stored in the database. Identifying Mapping of Collection (Contd.) AUTHORS AUTHORID AUTHORNAME Books BOOKID AUTHORID BOOKNAME
  • 14. © People Strategists www.peoplestrategists.com Slide 14 of 49 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: Identifying Mapping of Collection (Contd.) <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>
  • 15. © People Strategists www.peoplestrategists.com Slide 15 of 49 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.)
  • 16. © People Strategists www.peoplestrategists.com Slide 16 of 49 For example: The Authors class declares the books property as a collection of type, List. Identifying Mapping of Collection (Contd.) package Test; import java.util.List; public class Authors { . . . . . . . private List<Books> books; . . . . . . . . . . public List<Books> getBooks() { return books; } public void setBooks(List<Books> books) { this.books = books; } }
  • 17. © People Strategists www.peoplestrategists.com Slide 17 of 49 Now, consider the following code snippet: 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.) <hibernate-mapping> . . . . . . . . <list cascade="all" name="BOOKS"> <key column="authorid" /> <list-index column="bookid"/> <one-to-many class="Test.Books"/> </list> . . . . . . . . . . . . . . </hibernate-mapping>
  • 18. © People Strategists www.peoplestrategists.com Slide 18 of 49 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.)
  • 19. © People Strategists www.peoplestrategists.com Slide 19 of 49 For example: The preceding code, declares the books property of the Authors class as a collection of type, Collection. Identifying Mapping of Collection (Contd.) 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; } }
  • 20. © People Strategists www.peoplestrategists.com Slide 20 of 49 To map the Books class as a bag collection in the Hibernate mapping file, you need to use the <bag> element, as shown in the following code snippet: 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. Identifying Mapping of Collection (Contd.) <hibernate-mapping> . . . . . . <bag cascade="all" name="BOOKS"> <key column="authorid" /> <one-to-many class="Test.Books"/> </bag> . . . . . . </hibernate-mapping>
  • 21. © People Strategists www.peoplestrategists.com Slide 21 of 49 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. Activity: Saving a Collection in Hibernate
  • 22. © People Strategists www.peoplestrategists.com Slide 22 of 49 Consider the following tables: 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 Department ID Department Name Department Manager D1 Sales Tom D2 HR Sam Employee ID Employee Name Employee Address Department ID E1 Peter London D1 E2 Tom New York D1 EMPLOYEE DEPARTMENT
  • 23. © People Strategists www.peoplestrategists.com Slide 23 of 49 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 Database Relationships (Contd.)
  • 24. © People Strategists www.peoplestrategists.com Slide 24 of 49 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
  • 25. © People Strategists www.peoplestrategists.com Slide 25 of 49 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 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 PERSONID PERSONNAME INFOID P1 Peter E1 P2 Tom E2 PERSON PERSONINFO INFOID ADDRESS JOB INCOME E1 New York Teacher 5000 E2 Shanghai Doctor 6000
  • 26. © People Strategists www.peoplestrategists.com Slide 26 of 49 To implement the required functionality you need to create the following class that represents the person entity of the application: Mapping Entity Association (Contd.) package Test; public class Person { private String personID; private String personName; private PersonInfo personDetail; public Person() { } public Person(String personID) { this.personID = personID; } public Person(String personID, String personName, PersonInfo personDetail) { this.personID = personID; this.personName = personName; this.personDetail = personDetail; } public String getPersonID() { return this.personID; } public void setPersonID(String personID) { this.personID = personID; } public String getPersonName() { return this.personName; } public void setPersonName(String personName) { this.personName = personName; } public PersonInfo getPersonDetail() { return this.personDetail; } public void setPersonDetail(PersonInfo personDetail) { this.personDetail = personDetail; } }
  • 27. © People Strategists www.peoplestrategists.com Slide 27 of 49 The personDetail property refers to an instance of the PersonInfo class. You can create the PersonInfo class by using the following code snippet: Mapping Entity Association (Contd.) package Test; public class PersonInfo { private String infoID; private String address; private String job; private Integer income; public PersonInfo() { } public PersonInfo(String infoID) { this.infoID = infoID; } public PersonInfo(String infoID, String address, String job, Integer income) { this.infoID = infoID; this.address = address; this.job = job; this.income = income; } public String getInfoID() { return this.infoID; } public void setInfoID(String infoID) { this.infoID = infoID; } public String getAddress() { return this.address; } public void setAddress(String address) { this.address = address; } public String getJob() { return this.job; } public void setJob(String job) { this.job = job; } public Integer getIncome() { return this.income; } public void setIncome(Integer income) { this.income = income; } }
  • 28. © People Strategists www.peoplestrategists.com Slide 28 of 49 you need to map the one-to-one association between the classes in the Hibernate mapping file. For this, you can use the <many-to-one> element in the Hibernate mapping file. This element contains the following attributes:  name: Specifies the property name that is used to associate the classes.  class: Specifies the fully qualified name of the associated class that provides value for the property specified by the name attribute.  column: Specifies the name of the foreign key column that stores the value of the property specified by the name attribute.  not-null: Specifies that the foreign key column cannot contain null values, if set to true.  cascade: Specifies that the operations performed on one table will also affect the other table with similar updates.  unique: Specifies that the foreign key column contains unique values. This attribute makes the link between the tables as a one-to-one association. Mapping Entity Association (Contd.)
  • 29. © People Strategists www.peoplestrategists.com Slide 29 of 49 To map the PERSON table with the Person class, you can use the following code snippet in the Person.hbm.xml file: Mapping Entity Association (Contd.) <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>
  • 30. © People Strategists www.peoplestrategists.com Slide 30 of 49 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.)
  • 31. © People Strategists www.peoplestrategists.com Slide 31 of 49 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: Mapping Entity Association (Contd.) <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>
  • 32. © People Strategists www.peoplestrategists.com Slide 32 of 49 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.)
  • 33. © People Strategists www.peoplestrategists.com Slide 33 of 49 You can use the following code snippet to map the Student class with the STUDENT table in the Student.hbm.xml file: In the preceding code snippet, the <many-to-many> element is used to map the association between the Student and Course classes. Mapping Entity Association (Contd.) <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>
  • 34. © People Strategists www.peoplestrategists.com Slide 34 of 49 Consider a scenario where you need to implement one to one associate in hibernate application. Your Application should be based on the following guide lines: Student table contains column, such as ID, name, and address however member variable of an address column, such as street, city, state, and zip code are object instead of a simple data type. Activity: Implementing One-to-One Association in Hibernate Application
  • 35. © People Strategists www.peoplestrategists.com Slide 35 of 49 Consider a scenario where you need to develop an application that stores the details of the books sold by a book store. Note that book store sells different types of books, such as special edition books and books that are published in multiple languages. Therefore, you decide to create three classes, Books, SEBooks, and INBooks in the application. The Books class defines the common properties of different types of books, such as bookID, bookTitle, author, and cost. However, the SEBooks and INBooks classes extend the Books class and define their specific properties, such as specialfeatures and booklanguage. Mapping Class Inheritance
  • 36. © People Strategists www.peoplestrategists.com Slide 36 of 49 The Java classes are associated with each other with an inheritance relationship. However, the inheritance relationship cannot be achieved between the tables of a database. Therefore, you need to explicitly map the inheritance relationship of classes in the Hibernate mapping file while mapping the classes with the database tables. Hibernate provides the following types of mapping to support the inheritance hierarchy: Table per concrete class Table per subclass Table per class Mapping Class Inheritance (Contd.)
  • 37. © People Strategists www.peoplestrategists.com Slide 37 of 49 Table per concrete class: In the table per concrete class mapping, a table is created for each class in the inheritance hierarchy. Each table defines columns for all the properties of the class including the inherited properties. After creating the tables and the classes, you need to map them in the Hibernate mapping file. You can use the <union-subclass> element to map the subclasses in the mapping file. This element contains the following commonly used attributes:  name: Specifies the name of the subclass.  table: Specifies the name of the table that is to be mapped with the subclass. Mapping Class Inheritance (Contd.)
  • 38. © People Strategists www.peoplestrategists.com Slide 38 of 49 For example: The <class> element provides mapping information of the super class, Book. The <union-subclass> element is used to map each subclass with the corresponding tables in the database. Mapping Class Inheritance (Contd.) <hibernate-mapping> <class name="Test.Books" table="BOOKS"> ...... <union-subclass name="Test.SEBooks" table="SEBOOKS"> <property name="specialFeatures" type="string"> <column length="200" name="SPECIALFEATURES"/> </property> </union-subclass> <union-subclass name="Test.INBooks" table="INBOOKS"> <property name="bookLanguage" type="string"> <column length="20" name="BOOKLANGUAGE"/> </property> </union-subclass> </class> </hibernate-mapping>
  • 39. © People Strategists www.peoplestrategists.com Slide 39 of 49 Implementing the table per concrete class mapping strategy has the following drawbacks: For each property mapped in the super class, the column name must be the same in all the subclass tables. Each table defines columns for all properties of the class including inherited properties. This results in duplication of values. A separate query is needed to retrieve data from each table that stores the data of the concrete class. A change in the super class property results in the change of the various columns of the subclass tables. Mapping Class Inheritance (Contd.)
  • 40. © People Strategists www.peoplestrategists.com Slide 40 of 49 Table per class: In the table per class hierarchy mapping strategy, a single table is created for all the classes in the inheritance hierarchy. It eliminates the repetitive column names in the tables created for each class in the class hierarchy. However, you need to create an additional column in the table that distinguishes the objects of the subclasses. This column is referred to as the discriminator. The values stored in this column are used to identify the rows represented by the subclasses. Mapping Class Inheritance (Contd.)
  • 41. © People Strategists www.peoplestrategists.com Slide 41 of 49 The following elements are used to achieve table per class hierarchy mapping: <discriminator>:  Specifies the settings about the discriminator column with the help of the following attributes:  column: Specifies the name of the discriminator column.  type: Specifies the data type of the values stored in the discriminator column. <subclass>:  Specifies the settings about the subclasses with the help of the following attributes:  name: Specifies the name of the subclass.  discriminator-value: Specifies a discriminator value that is used for the current subclass. Mapping Class Inheritance (Contd.)
  • 42. © People Strategists www.peoplestrategists.com Slide 42 of 49 For example: The BOOKTYPE column of the BOOKDETAILS table is specified as the discriminator column. IN and SE are specified as the discriminator values that distinguish the objects of the INBooks and SEBooks subclasses. Mapping Class Inheritance (Contd.) <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>
  • 43. © People Strategists www.peoplestrategists.com Slide 43 of 49 Key points of Table per class are: The table per class hierarchy simplifies the mapping and you do not need to create multiple tables with repetitive column names. However, if the inheritance hierarchy increases and contains multiple classes, the size of the table increases. In addition, when a single table is created for all the classes in the inheritance hierarchy, the columns that are not common among classes contain null values. Therefore, if the class hierarchy increases, the number of null values stored in the table also increases. This affects the data integrity of the database. Mapping Class Inheritance (Contd.)
  • 44. © People Strategists www.peoplestrategists.com Slide 44 of 49 Table per subclass: In the table per subclass mapping strategy, a separate table is created for each class. However, columns are not repeated for the inherited properties in the table that stores the objects of the subclass. The table that stores the objects of the super class contains the primary key, which is used as a foreign key in the tables that stores the objects of the subclasses. To achieve the table per subclass mapping, you need to use the <joined- subclass> element in the hibernate mapping file. Mapping Class Inheritance (Contd.)
  • 45. © People Strategists www.peoplestrategists.com Slide 45 of 49 For example: Mapping Class Inheritance (Contd.) <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>
  • 46. © People Strategists www.peoplestrategists.com Slide 46 of 49 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. Mapping Class Inheritance (Contd.)
  • 47. © People Strategists www.peoplestrategists.com Slide 47 of 49 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. Activity: Implementing Inheritance Mapping in Hibernate Employee int empID String EmpName PermanentEmployee String companyName ContractEmployee String ContractorName
  • 48. © People Strategists www.peoplestrategists.com Slide 48 of 49 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
  • 49. © People Strategists www.peoplestrategists.com Slide 49 of 49 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 Hibernate provides the following types of mapping to support the inheritance hierarchy: Table per concrete class Table per subclass Table per class Summary (Contd.)