Sunday, 6 September 2020

How To Use Criteria in Hibernate 5

Hibernate Criteria Query API In Hibernate 5


Hibernate provide Criteria API to manipulating  the objects in type safe manner . Hibernate Criteria API introduce many changes how can you create criteria query .In Hibernate 5 To Create Criteria query you have write following code

CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<MensWear> criteriaQuery = criteriaBuilder.createQuery(MensWear.class);
Root<MensWear> root = criteriaQuery.from(MensWear.class);
criteriaQuery.select(root).where(criteriaBuilder.equal(root.get("productId"), 1));
Query<MensWear> query = session.createQuery(criteriaQuery); 


Hibernate Criteria Query example to fetch data using criteria query .

MensWear.java

package in.jk.hibernate5.criteria.api.query;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "hibernate5_mens_wears")
public class MensWear {

@Id
@Column(name = "product_id")
private int productId;
@Column(name = "product_name")
private String productName;
@Column(name = "company")
private String company;
@Column(name = "product_type")
private String productType;
@Column(name = "price")
private String price;
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getProductType() {
return productType;
}
public void setProductType(String productType) {
this.productType = productType;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
@Override
public String toString() {
return "MensWear [productId=" + productId + ", productName=" + productName + ", company=" + company
+ ", productType=" + productType + ", price=" + price + "]";
}

}


HibernateUtils.java

package in.jk.hibernate5;

import java.util.HashMap;
import java.util.Map;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Environment;

public class HibernateUtils {

private static StandardServiceRegistry standardServiceRegistry = null;
private static SessionFactory sessionFactory = null;
        
       public static SessionFactory getSessionFactory() {

return sessionFactory;

}

// SessionFactory using java config
public static SessionFactory buildSessionFactory() {
 
                StandardServiceRegistryBuilder serviceRegistryBuilder = null;

// Session Factory Service To Config and Integrate Hibernate Listener


serviceRegistryBuilder = new  StandardServiceRegistryBuilder();
Map<String, String> hibernateProperties = new HashMap<String, String>();

hibernateProperties.put(Environment.DRIVER, "org.postgresql.Driver");
hibernateProperties.put(Environment.URL, "jdbc:postgresql://localhost:5432/postgres");
hibernateProperties.put(Environment.USER, "postgres");
hibernateProperties.put(Environment.PASS, "jk123");
hibernateProperties.put(Environment.DIALECT, "org.hibernate.dialect.PostgreSQLDialect");
hibernateProperties.put(Environment.SHOW_SQL, "true");
hibernateProperties.put(Environment.HBM2DDL_AUTO, "update");

serviceRegistryBuilder.applySettings(hibernateProperties);
standardServiceRegistry = serviceRegistryBuilder.build();
MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);

                // Criteria API Query
metadataSources.addAnnotatedClass(in.jk.hibernate5.criteria.api.query.MensWear.class);
                Metadata metadata = metadataSources.getMetadataBuilder().build();
                sessionFactory = metadata.getSessionFactoryBuilder().build();
                return sessionFactory;

}

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Hibernate5withJava</groupId>
<artifactId>Hibernate5withJava</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/postgresql/postgresql -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.0-801.jdbc4</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.2.10.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

CriteriaQueryAPIHibernate5Application .java

package in.jk.hibernate5.criteria.api.query;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import in.jk.hibernate5.HibernateUtils;

public class CriteriaQueryAPIHibernate5Application {

private static SessionFactory sessionFactory;

public static void main(String[] args) {

sessionFactory = HibernateUtils.buildSessionFactory();
CriteriaQueryAPIHibernate5Application.addProduct();
System.out.println("\n ---------Find Product By ProductId --------  \n");
CriteriaQueryAPIHibernate5Application.findProdcutById();
System.out.println("\n -------  Find Product Name ---------- \n");
CriteriaQueryAPIHibernate5Application.findProdcutName();
System.out.println("\n ---------Find Product By Name and Comapny ------------ \n");
CriteriaQueryAPIHibernate5Application.findProdcutNameAndCompany();
System.out.println("\n-----------Find Product Info   -------------------\n");
CriteriaQueryAPIHibernate5Application.findProdcutDataWithMultiSelect();
System.out.println("\n-----------Find All Product  -------------------- \n");
CriteriaQueryAPIHibernate5Application.findAllProdcut();

}

private static void addProduct() {

Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

MensWear mensWear1 = new MensWear();
mensWear1.setProductId(1);
mensWear1.setProductName("Active Chill");
mensWear1.setCompany("Reebok");
mensWear1.setProductType("T-Shirts");
mensWear1.setPrice("2300");

MensWear mensWear2 = new MensWear();
mensWear2.setProductId(2);
mensWear2.setProductName("Sweet Animal");
mensWear2.setCompany("Reebok");
mensWear2.setProductType("T-Shirts");
mensWear2.setPrice("1799");

MensWear mensWear3 = new MensWear();
mensWear3.setProductId(3);
mensWear3.setProductName("Addidas ZNE");
mensWear3.setCompany("Addidas");
mensWear3.setProductType("Hoody");
mensWear3.setPrice("6999");

MensWear mensWear4 = new MensWear();
mensWear4.setProductId(4);
mensWear4.setProductName("Skull Cap");
mensWear4.setCompany("Reebok");
mensWear4.setProductType("Cap");
mensWear4.setPrice("1499");

session.persist(mensWear1);
session.persist(mensWear2);
session.persist(mensWear3);
session.persist(mensWear4);

transaction.commit();
session.close();

System.out.println("Mens Wear Prodcut add Succussfully ...");
}

private static void findProdcutById() {

Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<MensWear> criteriaQuery = criteriaBuilder.createQuery(MensWear.class);
Root<MensWear> root = criteriaQuery.from(MensWear.class);

criteriaQuery.select(root).where(criteriaBuilder.equal(root.get("productId"), 1));

Query<MensWear> query = session.createQuery(criteriaQuery);

MensWear mensWear = query.uniqueResult();
System.out.println("MensWear :: " + mensWear);

transaction.commit();
session.close();
}


private static void findProdcutName() {

Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<String> criteriaQuery = criteriaBuilder.createQuery(String.class);
Root<MensWear> root = criteriaQuery.from(MensWear.class);

criteriaQuery.select(root.get("productName"));

Query<String> query = session.createQuery(criteriaQuery);

System.out.println("MensWear Product Name :: ");

query.list().forEach(System.out::println);

transaction.commit();
session.close();
}
private static void findProdcutNameAndCompany() {

Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
Root<MensWear> root = criteriaQuery.from(MensWear.class);
Path<Object> prdouctName = root.get("productName");
Path<Object> company = root.get("company");

criteriaQuery.select(criteriaBuilder.array(prdouctName,company));

Query<Object[]> query = session.createQuery(criteriaQuery);

System.out.println("MensWear Product Name  :: ");

query.list().forEach(product->{
System.out.println("Name    ::  "+product[0]);
System.out.println("Comapny ::   "+product[1]);
});

System.out.println();
transaction.commit();
session.close();
}
private static void findProdcutDataWithMultiSelect() {

Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
Root<MensWear> root = criteriaQuery.from(MensWear.class);
Path<Object> prdouctName = root.get("productName");
Path<Object> company = root.get("company");
Path<Object> price = root.get("price");

criteriaQuery.multiselect(prdouctName,company,price);

Query<Object[]> query = session.createQuery(criteriaQuery);

System.out.println("MensWear Product Name  :: ");

query.list().forEach(product->{
System.out.println("Name    ::  "+product[0]);
System.out.println("Comapny ::   "+product[1]);
System.out.println("Price   ::   "+product[2]);
System.out.println();
});

System.out.println();
transaction.commit();
session.close();
}

private static void findAllProdcut() {

Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<MensWear> criteriaQuery = criteriaBuilder.createQuery(MensWear.class);
Root<MensWear> root = criteriaQuery.from(MensWear.class);

criteriaQuery.select(root);

Query<MensWear> query = session.createQuery(criteriaQuery);

List<MensWear> mensWears = query.getResultList();
System.out.println("All MensWear Product :: ");
mensWears.forEach(System.out::println);

transaction.commit();
session.close();
}

}


---------------------------------------------------------
Output in console

Hibernate: create table hibernate5_mens_wears (product_id int4 not null, company varchar(255), price varchar(255), product_name varchar(255), product_type varchar(255), primary key (product_id))
Sep 05, 2020 2:00:15 AM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: insert into hibernate5_mens_wears (company, price, product_name, product_type, product_id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernate5_mens_wears (company, price, product_name, product_type, product_id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernate5_mens_wears (company, price, product_name, product_type, product_id) values (?, ?, ?, ?, ?)
Hibernate: insert into hibernate5_mens_wears (company, price, product_name, product_type, product_id) values (?, ?, ?, ?, ?)
Mens Wear Prodcut add Succussfully ...

 ---------Find Product By ProductId --------  

Hibernate: select menswear0_.product_id as product_1_22_, menswear0_.company as company2_22_, menswear0_.price as price3_22_, menswear0_.product_name as product_4_22_, menswear0_.product_type as product_5_22_ from hibernate5_mens_wears menswear0_ where menswear0_.product_id=1
MensWear :: MensWear [productId=1, productName=Active Chill, company=Reebok, productType=T-Shirts, price=2300]

 -------  Find Product Name ---------- 

MensWear Product Name :: 
Hibernate: select menswear0_.product_name as col_0_0_ from hibernate5_mens_wears menswear0_
Active Chill
Sweet Animal
Addidas ZNE
Skull Cap

 ---------Find Product By Name and Comapny ------------ 

MensWear Product Name  :: 
Hibernate: select menswear0_.product_name as col_0_0_, menswear0_.company as col_1_0_ from hibernate5_mens_wears menswear0_
Name    ::  Active Chill
Comapny ::   Reebok
Name    ::  Sweet Animal
Comapny ::   Reebok
Name    ::  Addidas ZNE
Comapny ::   Addidas
Name    ::  Skull Cap
Comapny ::   Reebok


-----------Find Product Info   -------------------

MensWear Product Name  :: 
Hibernate: select menswear0_.product_name as col_0_0_, menswear0_.company as col_1_0_, menswear0_.price as col_2_0_ from hibernate5_mens_wears menswear0_
Name    ::  Active Chill
Comapny ::   Reebok
Price   ::   2300

Name    ::  Sweet Animal
Comapny ::   Reebok
Price   ::   1799

Name    ::  Addidas ZNE
Comapny ::   Addidas
Price   ::   6999

Name    ::  Skull Cap
Comapny ::   Reebok
Price   ::   1499



-----------Find All Product  -------------------- 

Hibernate: select menswear0_.product_id as product_1_22_, menswear0_.company as company2_22_, menswear0_.price as price3_22_, menswear0_.product_name as product_4_22_, menswear0_.product_type as product_5_22_ from hibernate5_mens_wears menswear0_
All MensWear Product :: 
MensWear [productId=1, productName=Active Chill, company=Reebok, productType=T-Shirts, price=2300]
MensWear [productId=2, productName=Sweet Animal, company=Reebok, productType=T-Shirts, price=1799]
MensWear [productId=3, productName=Addidas ZNE, company=Addidas, productType=Hoody, price=6999]
MensWear [productId=4, productName=Skull Cap, company=Reebok, productType=Cap, price=1499]



Friday, 4 September 2020

Second Level Cache Configuration in Hibernate 5


 Second Level Cache Configuration in Hibernate 5 


Caching is used to enhance the performance of application . Cache create an in memory storage location(memory buffer) to store recently used data item or objects . when hibernate first time fetch an object from database then hibernate store it in cache memory for future use . if same object require one more time in application then hibernate retrieved from cache memory and no database fetch required that's why it reduced the database query which exceed the performance of application.


Hibernate have three type of cache 

First Level Cache  : First level cache use by hibernate Session it by default enable .

Second Level Cache :Second level use by SessionFactory it require configuration . 
To enable second level cache hibernate require the cache provider vendor .
in our application we have used EHCache vendor implementation .

 EHCache : It can cache in memory or on disk and clustered caching and it
supports the optional Hibernate query result cache.

Second Level Query Cahche : In Hibernate we can fetch object from using hibernate method or using hibernate query language .
if you fetching object using HQL Query then create Query  by setting query cache able true .

Query<Employee> query = session.createQuery("FROM Employee" ,Employee.class); 
query.setCacheable(Boolean.TRUE);
List<Employee> employees =query.getResultList();
 
 

Maven Depedpendcy for EHCache vendor :

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.2.10.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.4.0</version>
</dependency>

Add following properties in config 

// Second Level Cache Config
Environment.USE_SECOND_LEVEL_CACHE  =  Boolean.toString(Boolean.TRUE)
Environment.CACHE_REGION_FACTORY,   =  "org.hibernate.cache.ehcache.EhCacheRegionFactory"
Environment.CACHE_PROVIDER_CONFIG =   "net.sf.ehcache.hibernate.EhCacheProvider"

Hibernate Entity Class  
Add @Cacheable annotation at class . you can specify optional cache concurrency scheme by @Cache Annotation

Employee.java

package in.jk.hibernate5.second.level.cache;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name="hibernate5_second_level_cache_employee")
@Cacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Employee {
@Id
@Column(name = "emp_id")
private int empId;
@Column(name = "name")
    private String name;
@Column(name = "company")
    private String company;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", name=" + name + ", company=" + company + "]";
}
}

Hibernate Config class :

package in.jk.hibernate5;

import java.util.HashMap;
import java.util.Map;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Environment;
import in.jk.hibernate5.example.Employee;
import in.jk.hibernate5.onetoone.mapping.Address;
import in.jk.hibernate5.onetoone.mapping.User;

public class HibernateUtils {

private static StandardServiceRegistry standardServiceRegistry = null;
private static SessionFactory sessionFactory = null;

       public static SessionFactory getSessionFactory() {

return sessionFactory;

}

// SessionFactory using java config
public static SessionFactory buildSessionFactory() {

StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
Map<String, String> hibernateProperties = new HashMap<String, String>();

hibernateProperties.put(Environment.DRIVER, "org.postgresql.Driver");
hibernateProperties.put(Environment.URL, "jdbc:postgresql://localhost:5432/postgres");
hibernateProperties.put(Environment.USER, "postgres");
hibernateProperties.put(Environment.PASS, "jk123");
hibernateProperties.put(Environment.DIALECT, "org.hibernate.dialect.PostgreSQLDialect");
hibernateProperties.put(Environment.SHOW_SQL, "true");
hibernateProperties.put(Environment.HBM2DDL_AUTO, "update");

// Second Level Cache Config
hibernateProperties.put(Environment.USE_SECOND_LEVEL_CACHE,         Boolean.toString(Boolean.TRUE));
hibernateProperties.put(Environment.USE_QUERY_CACHE, Boolean.toString(Boolean.TRUE));

hibernateProperties.put(Environment.CACHE_REGION_FACTORY, "org.hibernate.cache.ehcache.EhCacheRegionFactory");
hibernateProperties.put(Environment.CACHE_PROVIDER_CONFIG, "net.sf.ehcache.hibernate.EhCacheProvider");

serviceRegistryBuilder.applySettings(hibernateProperties);
standardServiceRegistry = serviceRegistryBuilder.build();
MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);

      // Second Level Cache
metadataSources.addAnnotatedClass(in.jk.hibernate5.second.level.cache.Employee.class);
               
                Metadata metadata = metadataSources.getMetadataBuilder().build();
               sessionFactory = metadata.getSessionFactoryBuilder().build();
               return sessionFactory;

}

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Hibernate5withJava</groupId>
<artifactId>Hibernate5withJava</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/postgresql/postgresql -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.0-801.jdbc4</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-ehcache -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.2.10.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.4.0</version>
</dependency>
             </dependencies>

<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>


SecondLevelCachaeHibernate5Application .java

package in.jk.hibernate5.second.level.cache;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import in.jk.hibernate5.HibernateUtils;

public class SecondLevelCachaeHibernate5Application {

private static SessionFactory sessionFactory;

public static void main(String[] args) {

sessionFactory = HibernateUtils.buildSessionFactory();
SecondLevelCachaeHibernate5Application.addEmployee();
SecondLevelCachaeHibernate5Application.findEmployeeByCache();
SecondLevelCachaeHibernate5Application.findEmployeeByQueryCache();

}

private static void addEmployee() {
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

Employee employee = new Employee();
employee.setEmpId(1);
employee.setName("J K");
employee.setCompany("Google");
//session.persist(employee);

transaction.commit();

session.close();

System.out.println("Employee Added ....");
}

private static void findEmployeeByCache() {
System.out.println("Hibernate Cache  :: ");

Session session = sessionFactory.openSession();

Transaction transaction1 = session.beginTransaction();

Employee emp = session.get(Employee.class, 1);

System.out.println("Employee :: 1 " + emp);

System.out.println("-------------------------------------------------");

Employee emp2 = session.get(Employee.class, 1);

System.out.println("Employee :: 2  " + emp2);

transaction1.commit();
session.close();
System.out.println("..............Session Close .........");
System.out.println();
Session session2 = sessionFactory.openSession();

Transaction transaction2 = session2.beginTransaction();

Employee emp3 = session2.get(Employee.class, 1);

System.out.println("Employee :: 3 " + emp3);

System.out.println("-------------------------------------------------");

Employee emp4 = session2.get(Employee.class, 1);

System.out.println("Employee :: 4  " + emp4);

transaction2.commit();
session2.close();
}
private static void findEmployeeByQueryCache() {
System.out.println("Hibernate Query Cache  :: ");


Session session = sessionFactory.openSession();

Transaction transaction1 = session.beginTransaction();

Query<Employee> query = session.createQuery("FROM Employee" ,Employee.class); 
query.setCacheable(Boolean.TRUE);
List<Employee> employees =query.getResultList();
employees.forEach(System.out::println);


System.out.println("-------------------------------------------------");

Query<Employee> query2 = session.createQuery("FROM Employee" ,Employee.class); 
query2.setCacheable(Boolean.TRUE);
List<Employee> employees2 =query2.getResultList();
employees2.forEach(System.out::println);
transaction1.commit();

session.close();

System.out.println("..............Session Close .........");
System.out.println();

Session session2 = sessionFactory.openSession();

Transaction transaction2 = session2.beginTransaction();

Query<Employee> query3 = session2.createQuery("FROM Employee" ,Employee.class); 
query3.setCacheable(Boolean.TRUE);
List<Employee> employees3 =query3.getResultList();
employees3.forEach(System.out::println);

System.out.println("-------------------------------------------------");

Query<Employee> query4 = session2.createQuery("FROM Employee" ,Employee.class); 
query4.setCacheable(Boolean.TRUE);
List<Employee> employees4 =query4.getResultList();
employees4.forEach(System.out::println);

transaction2.commit();

session2.close();

}

}



Ouput in Console  here ::
SQL Query execute only once  out of four call in rest case object return from cache even if we close the session .


Employee Added Succussfully ....
Fetching Object by Hibernate Cache  :: 
Hibernate: select employee0_.emp_id as emp_id1_26_0_, employee0_.company as company2_26_0_, employee0_.name as name3_26_0_ from hibernate5_second_level_cache_employee employee0_ where employee0_.emp_id=?
Employee :: 1 Employee [empId=1, name=J K, company=Google]
-------------------------------------------------
Employee :: 2  Employee [empId=1, name=J K, company=Google]
----------- Session Close ---------------

Here No SQL Query to Database Object return form Cache .
Employee :: 3  Employee [empId=1, name=J K, company=Google]
-------------------------------------------------
Employee :: 4  Employee [empId=1, name=J K, company=Google]
Hibernate Query Cache  :: 
Hibernate: select employee0_.emp_id as emp_id1_26_, employee0_.company as company2_26_, employee0_.name as name3_26_ from hibernate5_second_level_cache_employee employee0_
Employee [empId=1, name=J K, company=Google]
-------------------------------------------------
Employee [empId=1, name=J K, company=Google]
----------- Session Close ---------

Here No SQL Query to Database Object return form Cache .
Employee [empId=1, name=J K, company=Google]
-------------------------------------------------
Employee [empId=1, name=J K, company=Google]





HowTo Use Event Listener in Hibernate 5

How To Use Event Listener in Hibernate 5 


Hibernate listener is used to perform an additional modification in during when Hibernate perform various operation on object like save,update,load,evict,refresh etc  for these event you can create a listener class then can execute when these event is occur in your application 

For this application I have use two listener 


Save Listener : Invoke when an Object is Save to database when calling session.save() method .

package in.jk.hibernate5.event.listener.use;

import org.hibernate.HibernateException;
import org.hibernate.event.spi.SaveOrUpdateEvent;
import org.hibernate.event.spi.SaveOrUpdateEventListener;

public class EmployeeSaveListener implements SaveOrUpdateEventListener {

@Override
public void onSaveOrUpdate(SaveOrUpdateEvent event) throws HibernateException {

System.out.println("Employee :: Save Lisnter Invoke :: ");

Object object = event.getEntity();
if (object instanceof Employee) {
Employee employee = (Employee) object;
System.out.println("Employee at Save Listener Before Save ::  " + employee);
System.out.println();

}

}

}

Load Listener :  Invoke when an Object is load from  database when calling session.load() method .

package in.jk.hibernate5.event.listener.use;

import org.hibernate.HibernateException;
import org.hibernate.event.spi.LoadEvent;
import org.hibernate.event.spi.LoadEventListener;

public class EmployeeLoadListener implements LoadEventListener {

@Override
public void onLoad(LoadEvent event, LoadType loadType) throws HibernateException {

System.out.println("Employee :: Load Listener  Invoke :: ");

Object object = event.getResult();
if (object instanceof Employee) {
Employee employee = (Employee) object;
System.out.println("Employee  At Load Listener ::   " + employee);

}

System.out.println("Employee :: Load Listener Execution End ::");

}

}


Listener Integrator : Listener Integrator used registered listener  to hibernate Service Registry .

package in.jk.hibernate5.event.listener.use;

import org.hibernate.boot.Metadata;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.service.spi.SessionFactoryServiceRegistry;

public class HibernateEventListnerIntergarator implements Integrator {

@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {

EventListenerRegistry eventListenerRegistry =serviceRegistry
               .getSe rvice(EventListenerRegistry.class);
eventListenerRegistry.getEventListenerGroup(EventType.SAVE)
               .appendListener(new EmployeeSaveListener());
eventListenerRegistry.getEventListenerGroup(EventType.LOAD)
                .appendListener(new EmployeeLoadListener());

}

@Override
public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {

}

}

Employee.java 

package in.jk.hibernate5.event.listener.use;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="hibernate5_event_listener_employee")
public class Employee {
@Id
@Column(name = "emp_id")
private int empId;
@Column(name = "name")
        private String name;
@Column(name = "company")
        private String company;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", name=" + name + ", company=" + company + "]";
}

}

HibernateUtils.java


package in.jk.hibernate5;

import java.util.HashMap;
import java.util.Map;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Environment;

public class HibernateUtils {

private static StandardServiceRegistry standardServiceRegistry = null;
private static SessionFactory sessionFactory = null;

public static SessionFactory getSessionFactory() {

return sessionFactory;

}

// SessionFactory using java config
public static SessionFactory buildSessionFactory() {

// Session Factory Service To Config and Integrate Hibernate Listener
BootstrapServiceRegistry bootstrapServiceRegistry = new
BootstrapServiceRegistryBuilder()
.applyIntegrator(new HibernateEventListnerIntergarator()).build();
StandardServiceRegistryBuilder serviceRegistryBuilder = new
StandardServiceRegistryBuilder(bootstrapServiceRegistry);

Map<String, String> hibernateProperties = new HashMap<String, String>();

hibernateProperties.put(Environment.DRIVER, "org.postgresql.Driver");
hibernateProperties.put(Environment.URL, "jdbc:postgresql://localhost:5432/postgres");
hibernateProperties.put(Environment.USER, "postgres");
hibernateProperties.put(Environment.PASS, "jk123");
hibernateProperties.put(Environment.DIALECT, "org.hibernate.dialect.PostgreSQLDialect");
hibernateProperties.put(Environment.SHOW_SQL, "true");
hibernateProperties.put(Environment.HBM2DDL_AUTO, "update");

serviceRegistryBuilder.applySettings(hibernateProperties);
standardServiceRegistry = serviceRegistryBuilder.build();
MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);

               // Hibernate Listener
metadataSources.addAnnotatedClass(in.jk.hibernate5.event.listener.use.Employee.class);

                Metadata metadata = metadataSources.getMetadataBuilder().build();
               sessionFactory = metadata.getSessionFactoryBuilder().build();
               return sessionFactory;

}

}


Hibernate5EventListenerApplication.java

package in.jk.hibernate5.event.listener.use;

import org.hibernate.Session;
import org.hibernate.Transaction;

import in.jk.hibernate5.HibernateUtils;

public class Hibernate5EventListenerApplication {

public static void main(String[] args) {

Session session = HibernateUtils.buildSessionFactory().openSession();
Transaction transaction = session.beginTransaction();

Employee employee = new Employee();
employee.setEmpId(10);
employee.setName("J K");
employee.setCompany("Google");
session.save(employee);

transaction.commit();

System.out.println("Employee Save Succussfully ..");
System.out.println();

Transaction transaction1 = session.beginTransaction();

Employee emp = session.get(Employee.class, 1);

System.out.println("Employee :: " + emp);

transaction1.commit();
session.close();

}

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Hibernate5withJava</groupId>
<artifactId>Hibernate5withJava</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>

<!-- https://mvnrepository.com/artifact/postgresql/postgresql -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.0-801.jdbc4</version>
</dependency>
            </dependencies>

<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Output in Console 

----------------------

Employee :: Save Lisnter Invoke :: 

Employee at Save Listener Before Save ::  Employee [empId=10, name=J K, company=Google]

Hibernate: insert into hibernate5_event_listener_employee (company, name, emp_id) values (?, ?, ?)
Employee Save Succussfully ..

Hibernate: select employee0_.emp_id as emp_id1_13_0_, employee0_.company as company2_13_0_, employee0_.name as name3_13_0_ from hibernate5_event_listener_employee employee0_ where employee0_.emp_id=?
Employee :: Load Listener  Invoke :: 
Employee  At Load Listener ::   Employee [empId=1, name=J K, company=Google]
Employee :: Load Listener Execution End ::
Employee :: Employee [empId=1, name=J K, company=Google]


 

Tuesday, 18 August 2020

How to configure Spring Application using java based configuration


How to configure Spring Application using java based configuration 


Spring Application Require a Config class to define the bean definition .
The config class annotated with @Configuration annotation and @Bean annotation used for bean describe bean definition.

AnnotationConfigApplicationContext  class an implementation of ApplicationContext is used to 

initialization of  Spring application config using spring configuration class 

SpringConfig .java

package in.jk.javaconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringConfig {

@Bean(name = "user")
public User getUser() {
return new User();

}

}

User .java

package in.jk.javaconfig;

public class User {

private int userId;
private String userName;
private String password;
private String email;

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName + ", password=" + password + ", email=" + email
+ "]";
}

}

SpringApplication .java

package in.jk.javaconfig;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringApplication {

public static void main(String[] args) {

ApplicationContext applicationContext =  null;
               applicationContext =new AnnotationConfigApplicationContext(SpringConfig.class);
System.out.println("Spring Config initialize :: ");
User user = (User) applicationContext.getBean("user");

user.setUserId(1);
user.setUserName("jk");
user.setEmail("jk@gmail.com");
user.setPassword("jk123");
System.out.println(user);

}

}


Here is output in console -

Spring Config initialize :: 
User [userId=1, userName=jk, password=jk123, email=jk@gmail.com]



How HashMap Works in Java

How HashMap Works in Java 


HashMap is a class in Java as a part of collection framework . HashMap used to store object in  the form of key value pair .

where key and value both are objects. 

To store a object in HashMap it have following method 

public V put(K key V Value){}

To store a Object we have to create an object of HashMap class when we do this it create a array  .
each object strorage location in the array called bucket and in order to store object at bucket location.

in HashMap use hashcode of key object which is calculated by hashing .

hashcode value generated by hash function of key object also use provide name of bucket in which object store .
  
as we know two different object can have same hashcode in that situation same hashcode bucket location  contain a LinkList 

in that list HashMap store all same hashcode objects .

To get the value from HashMap 


To get an object from HashMap require key name of Object and use following get method

public V get(K key)

The Get method first calculate the hashcode of key object to find out the bucket address for getting object from bucket

if more the one object found for same hashcode in particuler bucket location in  the LinkList

equals method use for key object to checked the equality of object with stored object key if it return true then 
the corresponding object is return by get method .


How to Override equals and hashcode method

How to Override equals and hashcode method 

equals method: equals is a method  check equality of two object based on content of object 

if two object have same data then it return true otherwise it return false.

hashcode method: hashcode method return an integer value which represent memory location of object it can same for two unequal object . but is good practice have different value for each object .


Need of Override the equals and hashcode method 
 
If not overriding the equlas and hashcode method then we can not have or 
store non duplicate object in HashSet or we can't  use our class object as a key in HashMap 



Here is the contract, from the java.lang.Object specialization:

1.Whenever it(hashcode) is invoked on the same object more than once during an execution of a Java application, 
  the hashCode method must consistently return the same integer, provided no information used in equals 
  comparisons on the object is modified. This integer need not remain consistent from one execution of 
  an application to another execution of the same application.
2.If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the 
  two objects must produce the same integer result.

3.It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling 
  the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be 
  aware that producing distinct integer results for unequal objects may improve the performance of hashtables.


Employee  class with overridden equlas and hashcode method 

package in.jk.example;

public class Employee {
private int empId;
private String name;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// Override of hashcode
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + this.empId;
result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
return result;
}
// Override of equlas
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee employee = (Employee) obj;
if (this.empId != employee.empId)
return false;
if (this.name == null) {
if (employee.name != null)
return false;
} else if (!this.name.equals(employee.name))
return false;
return true;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", name=" + name + "]";
}

}



package in.jk.example;

import java.util.HashSet;

public class Application {

public static void main(String[] args) {

Employee emp1 = new Employee();
emp1.setEmpId(1);
emp1.setName("JK");

Employee emp2 = new Employee();
emp2.setEmpId(1);
emp2.setName("JK");

Employee emp3 = new Employee();
emp3.setEmpId(1);
emp3.setName("JK");

Employee emp4 = new Employee();
emp4.setEmpId(2);
emp4.setName("jk");

Employee emp5 = new Employee();
emp5.setEmpId(4);
emp5.setName("johny");

Employee emp6 = new Employee();
emp6.setEmpId(4);
emp6.setName("johny");

Employee emp7 = new Employee();
emp7.setEmpId(5);
emp7.setName("johny kumar");

Employee emp8 = new Employee();
emp8.setEmpId(8);
emp8.setName("john");

HashSet<Employee> employees = new HashSet<Employee>();

employees.add(emp1);
employees.add(emp2);
employees.add(emp3);
employees.add(emp4);
employees.add(emp5);
employees.add(emp6);
employees.add(emp7);
employees.add(emp8);

System.out.println("Size : "+employees.size());
employees.stream().forEach(System.out::println);

}

}












How @Qualifier and @Primary Annotation works in Spring

How @Qualifier and @Primary Annotation works in Spring


@Qualifier and @Primary Annotation both used in auto wiring of beans


@Primary Annotation

@Primary Annotation is used to mark a spring as  default primary bean for injection candidate if  container one more bean candidate for a injection bean location.

@Qualifier  Annotation

@Qualifier  Annotation allow to identify a particular bean when more the one bean implementation found for auto wiring during  resolution time of bean object injection  by spring container .

Following code example illustrate the working these annotation 
 
Employee.java

package in.jk.spring.autowired.test;

public interface Employee {

public String toString();

}

Developer.java

package in.jk.spring.autowired.test;

public class Developer implements Employee {

@Override
public String toString() {
return "Developer Bean";
}

}

DeveloperQualifier.java
package in.jk.spring.autowired.test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Qualifier;

@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface DeveloperQualifier {

}


Manager.java
package in.jk.spring.autowired.test;

public class Manager implements Employee {

@Override
public String toString() {
return "Manager Bean";
}

}

ManagerQualifier.java

package in.jk.spring.autowired.test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.beans.factory.annotation.Qualifier;

@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface ManagerQualifier {

}

BeanConfiguration.java

package in.jk.spring.autowired.test;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class SpringConfig {

@Bean
@Primary
@DeveloperQualifier()
public Employee getJavaEmp() {
return new Developer();

}

@Bean
@ManagerQualifier()
public Employee getManeger() {
return new Manager();

}

}



TestBean.java

package in.jk.spring.autowired.test;

import org.springframework.beans.factory.annotation.Autowired;

public class TestBean {

@Autowired
@DeveloperQualifier()
Employee developer;

@Autowired
@ManagerQualifier
Employee manager;

public void printEmpoyee() {

System.out.println(developer);
System.out.println(manager);

}

}

SpringApplication.java

package in.jk.spring.autowired.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringApplication {

public static void main(String[] args) {

ApplicationContext applicationContext = null;
applicationContext =new ClassPathXmlApplicationContext("context.xml");

SpringConfig configuration = (SpringConfig ) applicationContext.getBean("config");

TestBean testBean = (TestBean) applicationContext.getBean("testBean");

testBean.printEmpoyee();

}

}



context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:annotation-config />
<context:component-scan
base-package="in.jk.spring.config" />
<context:property-placeholder
location="classpath:application.properties" />

<bean id="testBean" class="in.jk.spring.autowired.test.TestBean">

</bean>

<bean id="config"
class="in.jk.spring.autowired.test.SpringConfig ">

</bean>

</beans>