Skip to main content

Posts

Showing posts from November 12, 2018

Hibernate - Basic Annotations

Greetings!

@Entity - Add in class level. Declares that this class is an entity @Entity public class Employee { // ... }
@Table - Add in class level. Define table, schema, catalog. Use name property to add the name of the table unless class name is used as the table name. @Entity @Table(name = "tbl_employee") public class Employee { // ... }
@Column - Add column properties. @Column(name = "first_name") private String firstName;
@Id - Declares property as the identifier of this class. @Id private Long id;
@GeneratedValue - use along with @Id to generate primary key automatically. JPA defines 5 strategies. @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;
AUTO - select depending on the underlying database.
IDENTITY
SEQUENCE
TABLE
identity copy - identity is copied form another entity.

@Version - Adding this will add optimistic locking capability. For an update query Hibernate will automatically add version number validity in where clause.…

Hibernate - Value Types

Greetings!

When we design our domain models we can have more important classes and less important classes like Address, String, etc. In other word we have fine-grained object model which means more classes than tables.
For an example User can have an Address. We can create separate class for Address fields and add it as a property in User using composition.

public class User { private Long id; private String name; private Integer age; private Address address; }
Here User is an entity. Name, Age, Address are value types.

Entity classes need an identifier.Value type doesn't have an identifier because instances are identified through owning entity.
What is an Entity?Has a database identity.Has it's own life cycle.Object reference is persisted in the database.
What is a Value Type?Doesn't have own identity.Embedded into owning entity.Represent table columns.
How to map?Basic types can be directly mapped using @Column. (String, Integer, etc)Collection value type…

Hibernate - Identity

Greetings!

Identity is the fact that being who or what a thing is.

In Java two objects are identical if they are referring to the same memory location.
object1 == object2
Equals means that they have the same value but may not have same memory location.
object1.equals(object2)
In relational databases, objects are identical if they share the same table and have same primary key. This is known as database identity in Java side.
@Id @GeneratedValue private Long id; Because @Id on the field, hibernate use field access.If we don't provide @GeneratedValue, JPA provider doesn't provide a key for us. We have to assign ourselves. These are called application-assigned identifiers.
Candidate Key Column(s) we could use to identify a database row is called candidate keys.

Never null.Unique.Never changes.
Natural Key A key with a business meaning is called a natural key. For an Employee table can have a employee number to represent employee's identity.
Practically, this will have side e…

Hibernate - Introduction

Greetings!

Problem in hand Almost all the applications need to save data. This persistence storage most probably is a database. This storage is independence system which is called data independence. Here comes the problem. How do we convert relational data to Java objects? Sure we can convert one by one which is not convenient due to;
It takes lot of time to write code.Difficult to maintain.Need to write all SQL queries.Need to handle transactions.Have to maintain database connections.Not portable. To solve these kind of problems, Object Relational Mapping frameworks are born. Hibernate is the most popular among them.

In an era of Spring dominating most part of the application, Hibernate is almost hidden. Thanks to Spring Data most of the time we do not have write any SQL query. Spring help us to generate those.

JPA, Hibernate, Spring Data JPA Java Persistence API is the specification. Hibernate is one of the implementations of JPA. Spring Data provides us another layer to hide JPA by …