[1] - CriteriaQueryParametersClientTest.java
package com.ishaan.client;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;
import org.hibernate.Session;
import org.hibernate.query.Query;
import com.infotech.entities.Person;
import com.infotech.util.HibernateUtil;
public class CriteriaQueryParametersClientTest {
public static void main(String[] args) {
String nickName="Sam";
try(Session session = HibernateUtil.getSessionFactory().openSession()) {
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Person> criteriaQuery = builder.createQuery(Person.class);
Root<Person> root = criteriaQuery.from(Person.class);
ParameterExpression<String> nickNameParameter = builder.parameter(String.class);
criteriaQuery.where(builder.equal(root.get("nickName"), nickNameParameter));
Query<Person> query = session.createQuery(criteriaQuery);
query.setParameter(nickNameParameter, nickName);
List<Person> resultList = query.getResultList();
for (Person person : resultList) {
System.out.println(person);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
[2]- SaveDataClientTest.java:
package com.ishaan.client;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.infotech.entities.Person;
import com.infotech.util.HibernateUtil;
public class SaveDataClientTest {
public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = null;
try {
session = sf.openSession();
session.beginTransaction();
Person person1 = new Person();
person1.setName("Mark Bingel");
person1.setNickName("Mac");
person1.setAddress("Alameda Street Los Angeles");
person1.setCreatedOn(new Date());
person1.setVersion(1);
Person person2 = new Person();
person2.setName("Sean Murphy");
person2.setNickName("Sam");
person2.setAddress("Bank of Canada,234 Wellington Street");
person2.setCreatedOn(new Date());
person2.setVersion(1);
session.save(person1);
session.save(person2);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(session != null){
session.close();
}
}
}
}
[3] - Person.java
package com.ishaan.entities;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
@Entity
@Table(name="Person")
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Long id;
@Column(name="name")
private String name;
@Column(name="nickName")
private String nickName;
@Column(name="address")
private String address;
@Temporal(TemporalType.TIMESTAMP )
@Column(name="createdOn")
private Date createdOn;
@Version
@Column(name="version")
private int version;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", nickName=" + nickName + ", address=" + address
+ ", createdOn=" + createdOn + ", version=" + version + "]";
}
}
[4] - HibernateUtil.java
package com.ishaan.util;
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;
/**
*
* @author Ishaan
*
*/
public class HibernateUtil {
private static StandardServiceRegistry standardServiceRegistry;
private static SessionFactory sessionFactory;
static{
if (sessionFactory == null) {
try {
// Create StandardServiceRegistry
standardServiceRegistry = new StandardServiceRegistryBuilder()
.configure()
.build();
// Create MetadataSources
MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);
// Create Metadata
Metadata metadata = metadataSources.getMetadataBuilder().build();
// Create SessionFactory
sessionFactory = metadata.getSessionFactoryBuilder().build();
} catch (Exception e) {
e.printStackTrace();
if (standardServiceRegistry != null) {
StandardServiceRegistryBuilder.destroy(standardServiceRegistry);
}
}
}
}
//Utility method to return SessionFactory
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
[5] - hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:4406/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="com.ishaan.entities.Person"/>
</session-factory>
</hibernate-configuration>
click for more
package com.ishaan.client;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;
import org.hibernate.Session;
import org.hibernate.query.Query;
import com.infotech.entities.Person;
import com.infotech.util.HibernateUtil;
public class CriteriaQueryParametersClientTest {
public static void main(String[] args) {
String nickName="Sam";
try(Session session = HibernateUtil.getSessionFactory().openSession()) {
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Person> criteriaQuery = builder.createQuery(Person.class);
Root<Person> root = criteriaQuery.from(Person.class);
ParameterExpression<String> nickNameParameter = builder.parameter(String.class);
criteriaQuery.where(builder.equal(root.get("nickName"), nickNameParameter));
Query<Person> query = session.createQuery(criteriaQuery);
query.setParameter(nickNameParameter, nickName);
List<Person> resultList = query.getResultList();
for (Person person : resultList) {
System.out.println(person);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
[2]- SaveDataClientTest.java:
package com.ishaan.client;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.infotech.entities.Person;
import com.infotech.util.HibernateUtil;
public class SaveDataClientTest {
public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = null;
try {
session = sf.openSession();
session.beginTransaction();
Person person1 = new Person();
person1.setName("Mark Bingel");
person1.setNickName("Mac");
person1.setAddress("Alameda Street Los Angeles");
person1.setCreatedOn(new Date());
person1.setVersion(1);
Person person2 = new Person();
person2.setName("Sean Murphy");
person2.setNickName("Sam");
person2.setAddress("Bank of Canada,234 Wellington Street");
person2.setCreatedOn(new Date());
person2.setVersion(1);
session.save(person1);
session.save(person2);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(session != null){
session.close();
}
}
}
}
[3] - Person.java
package com.ishaan.entities;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
@Entity
@Table(name="Person")
public class Person {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Long id;
@Column(name="name")
private String name;
@Column(name="nickName")
private String nickName;
@Column(name="address")
private String address;
@Temporal(TemporalType.TIMESTAMP )
@Column(name="createdOn")
private Date createdOn;
@Version
@Column(name="version")
private int version;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", nickName=" + nickName + ", address=" + address
+ ", createdOn=" + createdOn + ", version=" + version + "]";
}
}
[4] - HibernateUtil.java
package com.ishaan.util;
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;
/**
*
* @author Ishaan
*
*/
public class HibernateUtil {
private static StandardServiceRegistry standardServiceRegistry;
private static SessionFactory sessionFactory;
static{
if (sessionFactory == null) {
try {
// Create StandardServiceRegistry
standardServiceRegistry = new StandardServiceRegistryBuilder()
.configure()
.build();
// Create MetadataSources
MetadataSources metadataSources = new MetadataSources(standardServiceRegistry);
// Create Metadata
Metadata metadata = metadataSources.getMetadataBuilder().build();
// Create SessionFactory
sessionFactory = metadata.getSessionFactoryBuilder().build();
} catch (Exception e) {
e.printStackTrace();
if (standardServiceRegistry != null) {
StandardServiceRegistryBuilder.destroy(standardServiceRegistry);
}
}
}
}
//Utility method to return SessionFactory
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
[5] - hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:4406/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="com.ishaan.entities.Person"/>
</session-factory>
</hibernate-configuration>
[6] - 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>com.ishaan</groupId>
<artifactId>CriteriaQueryParametersExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- MySQL connector dependency -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>
<!-- Hibernate 5.2.12 Final dependency-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
click for more