Professional Documents
Culture Documents
An ORM tool simplifies the data creation, data manipulation and data
access. It is a programming technique that maps the object to the data
stored in the database.
Advantages of Hibernate Framework
SessionFactory
The SessionFactory is a factory of session and client of ConnectionProvider. It holds second level cache
(optional) of data. The SessionFactory interface provides factory method to get the object of Session.
Session
The session object provides an interface between the application and data stored in the database. It is a
short-lived object and wraps the JDBC connection. It is factory of Transaction, Query and Criteria. It holds a first-
level cache (mandatory) of data. The Session interface provides methods to insert, update and delete the object.
It also provides factory methods for Transaction, Query and Criteria.
Transaction
The transaction object specifies the atomic unit of work. It is optional. The org.hibernate.Transaction
interface provides methods for transaction management.
Hibernate using Annotation
The hibernate application can be created with annotation. There are many annotations that can be used to
create hibernate application such as @Entity, @Id, @Table etc.
Hibernate Annotations are based on the JPA 2 specification and supports all the features.
All the JPA annotations are defined in the javax.persistence package. Hibernate EntityManager
implements the interfaces and life cycle defined by the JPA specification.
Entity
Entities in JPA are nothing but POJOs representing data that can be persisted to the database. An entity
represents a table stored in a database. Every instance of an entity represents a row in the table.
The Entity Annotation
Let's say we have a POJO called Student which represents the data of a student and we would like to
store it in the database.
In order to do this, we should define an entity so that JPA is aware of it.
So let's define it by making use of the @Entity annotation. We must specify this annotation at the class level. We
must also ensure that the entity has a no-arg constructor and a primary key:
The entity name defaults to the name of the class. We can change its name using the name element.
The Id Annotation
Each JPA entity must have a primary key which uniquely identifies it. The @Id annotation defines the
primary key. We can generate the identifiers in different ways which are specified by
the @GeneratedValue annotation.
We can choose from four id generation strategies with the strategy element. The value can be AUTO,
TABLE, SEQUENCE, or IDENTITY.
AUTO Generation
If we're using the default generation type, the persistence provider will determine values based on the type
of the primary key attribute. This type can be numerical or UUID.
For numeric values, the generation is based on a sequence or table generator, while UUID values will use
the UUIDGenerator.
Let's see an example of mapping an entity primary key using AUTO generation strategy:
In this case, the primary key values will be unique at the database level.
An interesting feature introduced in Hibernate 5 is the UUIDGenerator. To use
this, all we need to do is declare an id of
type UUID with @GeneratedValue annotation:
The TableGenerator uses an underlying database table that holds segments of identifier generation values.
Let's customize the table name using the @TableGenerator annotation:
In most cases, the name of the table in the database and the name of the entity will not be the same. In
these cases, we can specify the table name using the @Table annotation:
If we do not use the @Table annotation, the name of the entity will be considered the
name of the table.
The Column Annotation
Just like the @Table annotation, we can use the @Column annotation to mention the details of a column in
the table.
The @Column annotation has many elements such as name, length, nullable, and unique.
We still have to define an @Id field in the Address class. But note that this references
the user_id column, and it no longer uses the @GeneratedValue annotation. Also, on the
field that references the User, we've added the @MapsId annotation, which indicates
that the primary key values will be copied from the User entity.
@OneToOne relationship using a Join Table
Now let's imagine that our employees get associated with a workstation. It's
one-to-one, but sometimes an employee might not have a workstation and vice
versa.
@OneToMany relationship
one-to-many mapping means that one row in a table is mapped to multiple rows in another table.
Unidirectional @OneToMany
by default, that’s how the
unidirectional @OneToMany
association works, and this is
how it looks from a database
perspective:
Unidirectional @OneToMany with @JoinColumn
Bidirectional @OneToMany
Thank you :)