Agenda
Agenda (Cont.)
What is Hibernate?
The Problem solved by hibernate
User Class |
---|
Id |
Name |
Address |
Phone |
Date of Birth |
Id | Name | Address | Phone | Date of Birth |
---|---|---|---|---|
The Problem solved by hibernate (Cont.)
Set up with gradle
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile('org.hibernate:hibernate-core:5.2.6.Final')
compile ('mysql:mysql-connector-java:5.1.6')
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--Database connection settings -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/poc</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"/>
<!--JDBC connection pool-->
<property name="connection.pool_size">10</property>
<!-- SQL Dialect-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--echo all executed SQL-->
<property name="show_sql">true</property>
<!--drop or re-create database schema at startup-->
<property name="hbm2ddl.auto">create</property>
<!--Annotated entity class-->
<mapping class="com.hibernate.demo.Person"/>
</session-factory>
</hibernate-configuration>
hibernate.cfg.xml
hbm2ddl configurations
Special note about update for hbm2ddl auto
update won't modify existing table column definitions.
update will add a db column that doesn't already exist.
update will not delete a db column that is removed/no longer in your entity.
update will not modify a db column that has already been created.
@Id and @Entity
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Person {
@Id
Integer id;
String name;
Integer age;
}
CRUD (Create)
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Application {
public static void main(String[] args) {
Person person=new Person();
person.setName("Hibernate");
person.setAge(27);
person.setId(2);
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(person);
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}
CRUD (Read)
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Application {
public static void main(String[] args) {
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
Person person=session.get(Person.class,1);
session.getTransaction().commit();
session.close();
System.out.println(person);
session.close();
sessionFactory.close();
}
}
CRUD (Update)
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Application {
public static void main(String[] args) {
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
Person person=session.get(Person.class,1);
person.setName("Updated Name");
session.update(person);
session.getTransaction().commit();
session.close();
System.out.println(person);
session.close();
sessionFactory.close();
}
}
CRUD (Delete)
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Application {
public static void main(String[] args) {
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
Person person=session.get(Person.class,1);
session.delete(person);
session.getTransaction().commit();
session.close();
System.out.println(person);
session.close();
sessionFactory.close();
}
}
Exercise 1
Transient, Persistent and Detached states
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Application {
public static void main(String[] args) {
Person person=new Person();
//Transient state
person.setName("Hibernate");
person.setAge(23);
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
//Persistent state
session.save(person);
person.setName("Updated");
session.getTransaction().commit();
session.close();
sessionFactory.close();
//Detached state
person.setName("Updated in detached");
}
}
Name Annotations
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity(name = "Employee")
public class Person {
@Id
Integer id;
@Column(name = "First_Name")
String name;
Integer age;
}
More Annotations
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name = "Employee")
public class Person {
@Id
Integer id;
@Column(name = "First_Name")
String name;
@Transient
Integer age;
@Temporal(TemporalType.DATE)
Date dob;
}
Automatically generate primary key value
@Entity
@Table(name = "Employee")
public class Person {
@Id @GeneratedValue(strategy =GenerationType.SEQUENCE )
Integer id;
String name;
Integer age;
@Temporal(TemporalType.DATE)
Date dob;
}
Id Generation Stratergies
Exercise 2
Embedded Type
import javax.persistence.*;
@Entity
public class Person{
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id;
String name;
Integer age;
@Embedded
Address address;
}
For embedded object we can use @Embedded annotation for the Reference type instance variable
import javax.persistence.Embeddable;
@Embeddable
public class Address {
String city;
String country;
}
Optionally we can also place @Embeddable on the class of the reference type instance variable
Embedded type (Cont.)
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Application {
public static void main(String[] args) {
Person person=new Person();
person.setName("Hibernate");
person.setAge(23);
Address address=new Address();
address.setCity("Delhi");
address.setCountry("India");
person.setAddress(address);
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(person);
session.getTransaction().commit();
session.close();
}
}
Overriding attributes
import javax.persistence.*;
@Entity
public class Person{
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
Integer id;
String name;
Integer age;
@Embedded
Address address;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "city", column = @Column(name = "CURRENT_CITY")),
@AttributeOverride(name = "country", column = @Column(name = "CURRENT_COUNTRY"))
})
Address currentAddress;
}
import javax.persistence.Column;
public class Address {
@Column(name = "HOME_CITY")
String city;
@Column(name = "HOME_COUNTRY")
String country;
}
Exercise 3
Saving Collections
import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "Employee")
public class Person {
@Id @GeneratedValue(strategy =GenerationType.SEQUENCE )
Integer id;
String name;
Integer age;
@Temporal(TemporalType.DATE)
Date dob;
@ElementCollection
Set<String> hobbies=new HashSet<String>();
}
Configuring Collections and Adding Keys
@JoinTable annotatation is used to specify the name of the table and column which will be used to join collection table with the parent table.
import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "Employee")
public class Person {
@Id @GeneratedValue(strategy =GenerationType.SEQUENCE )
Integer id;
String name;
Integer age;
@Temporal(TemporalType.DATE)
Date dob;
@JoinTable(name = "employee_hobbies",
joinColumns = @JoinColumn(name = "CUSTOM_JOIN_ID"))
@ElementCollection
Set<String> hobbies=new HashSet<String>();
}
Generate Id for Join Table
import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import javax.persistence.*;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
@Entity
@Table(name = "Employee")
public class Person {
@Id @GeneratedValue(strategy =GenerationType.SEQUENCE )
Integer id;
String name;
Integer age;
@Temporal(TemporalType.DATE)
Date dob;
@JoinTable(name = "employee_hobbies",
joinColumns = @JoinColumn(name = "CUSTOM_JOIN_ID"))
@ElementCollection
@GenericGenerator(name = "sequence-gen",strategy = "sequence")
@CollectionId(columns = {@Column(name = "hobbies_id")},
generator = "sequence-gen",type = @Type(type = "long"))
Collection<String> hobbies=new HashSet<String>();
}
@CollectionId and @Generic Generator annotaions are use to Generate Id of a Join Table in the following way:
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name = "Employee")
public class Person {
@Id @GeneratedValue(strategy =GenerationType.SEQUENCE )
Integer id;
String name;
Integer age;
@Temporal(TemporalType.DATE)
Date dob;
@JoinTable(name = "employee_hobbies",
joinColumns = @JoinColumn(name = "CUSTOM_JOIN_ID"))
@ElementCollection(fetch = FetchType.EAGER)
Collection<String> hobbies=new ArrayList<String>();
}
Proxy Objects and Eager and Fetch Types
By Default Hibernate creates a proxy object and fills first level instance variables. To fetch Collection instance variables with object we need to set FetchType.Eager in @ElementCollection.
Exercise 4
One to One mapping
import javax.persistence.*;
@Entity
@Table(name = "Employee")
public class Person {
@Id @GeneratedValue(strategy =GenerationType.SEQUENCE )
Integer id;
String name;
Integer age;
@OneToOne
@JoinColumn(name = "Vehicle_join_column")
Vehicle vehicle;
}
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Vehicle {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
Integer vehicleId;
String vehicleName;
}
One to One mapping (Cont.)
Person person=new Person();
person.setName("Hibernate");
person.setAge(27);
person.setId(1);
Vehicle vehicle=new Vehicle();
vehicle.setVehicleName("Car");
person.setVehicle(vehicle);
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(person);
session.save(vehicle);
session.getTransaction().commit();
session.close();
One to Many (Unidirectional)
import javax.persistence.*;
import java.util.Collection;
import java.util.HashSet;
@Entity
@Table(name = "Employee")
public class Person {
@Id @GeneratedValue(strategy =GenerationType.SEQUENCE )
Integer id;
String name;
Integer age;
@OneToMany
@JoinTable(joinColumns = @JoinColumn(name="USER_ID")
,inverseJoinColumns = @JoinColumn(name = "VEHICAL_ID"))
Collection<Vehicle> vehicle= new HashSet<Vehicle>();
}
import javax.persistence.*;
@Entity
public class Vehicle {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
Integer vehicleId;
String vehicleName;
}
One to Many (Unidirectional) Cont.
Person person=new Person();
person.setName("Hibernate");
person.setAge(27);
person.setId(1);
Vehicle vehicle=new Vehicle();
vehicle.setVehicleName("Car");
Vehicle vehicle2=new Vehicle();
vehicle2.setVehicleName("Bike");
person.getVehicle().add(vehicle);
person.getVehicle().add(vehicle2);
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(person);
session.save(vehicle);
session.save(vehicle2);
session.getTransaction().commit();
session.close();
One to Many (Bidirectional)
import javax.persistence.*;
import java.util.Collection;
import java.util.HashSet;
@Entity
@Table(name = "Employee")
public class Person {
@Id @GeneratedValue(strategy =GenerationType.SEQUENCE )
Integer id;
String name;
Integer age;
@OneToMany
@JoinTable(joinColumns = @JoinColumn(name="USER_ID")
,inverseJoinColumns = @JoinColumn(name = "VEHICAL_ID"))
Collection<Vehicle> vehicle= new HashSet<Vehicle>();
}
import javax.persistence.*;
@Entity
public class Vehicle {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
Integer vehicleId;
String vehicleName;
@ManyToOne
Person person;
}
Person person=new Person();
person.setName("Hibernate");
person.setAge(27);
person.setId(1);
Vehicle vehicle=new Vehicle();
vehicle.setVehicleName("Car");
vehicle.setPerson(person);
Vehicle vehicle2=new Vehicle();
vehicle2.setPerson(person);
vehicle2.setVehicleName("Bike");
person.getVehicle().add(vehicle);
person.getVehicle().add(vehicle2);
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(person);
session.save(vehicle);
session.save(vehicle2);
session.getTransaction().commit();
session.close();
One to Many (Bidirectional) Cont.
One to Many without additional table
import javax.persistence.*;
import java.util.Collection;
import java.util.HashSet;
@Entity
@Table(name = "Employee")
public class Person {
@Id @GeneratedValue(strategy =GenerationType.SEQUENCE )
Integer id;
String name;
Integer age;
@OneToMany(mappedBy = "person")
Collection<Vehicle> vehicle= new HashSet<Vehicle>();
}
import javax.persistence.*;
@Entity
public class Vehicle {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
Integer vehicleId;
String vehicleName;
@ManyToOne
Person person;
}
Many to Many
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Table(name = "Employee")
public class Person {
@Id @GeneratedValue(strategy =GenerationType.SEQUENCE )
Integer id;
String name;
Integer age;
@ManyToMany
List<Vehicle> vehicle= new ArrayList<Vehicle>();
}
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Vehicle {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
Integer vehicleId;
String vehicleName;
@ManyToMany(mappedBy = "vehicle")
List<Person> personList = new ArrayList<Person>();
}
Cascade Type
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Person{
@Id @GeneratedValue(strategy = GenerationType.TABLE)
Integer id;
String name;
Integer age;
@OneToMany(cascade = CascadeType.PERSIST)
List<Vehicle> listOfVehicles=new ArrayList<Vehicle>();
}
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Vehicle {
@Id @GeneratedValue(strategy = GenerationType.TABLE)
Integer vehicleId;
String vehicleName;
}
Cascade Type (Cont.)
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Application {
public static void main(String[] args) {
Person person=new Person();
person.setName("Hibernate");
person.setAge(23);
Vehicle vehicle=new Vehicle();
vehicle.setVehicleName("Car");
person.getListOfVehicles().add(vehicle);
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.persist(person);
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}
Exercise 5