Advertisement
SessionFactory sf= new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<Object> crtQ = cb.createQuery(Object.class);
Root<UserInfo> root = crtQ.from(UserInfo.class);
crtQ.select(root);
Query query = session.createQuery(crtQ);
List<UserInfo> collection2 = query.getResultList();
Object object = query.getSingleList();
Advertisement
package decodejava;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Column;
@Entity
@Table (name ="USER_INFO")
public class UserInfo {
@Id
@GeneratedValue
@Column (name = "ID")
private int id;
@Column (name = "First_Name")
private String firstName;
@Column (name = "Last_Name")
private String lastName;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
}
package decodejava;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import org.hibernate.query.Query;
import javax.persistence.criteria.Root;
public class Hiber
{
public static void main(String[] args)
{
SessionFactory sf= new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
//Creating the first object
UserInfo ob1 = new UserInfo();
ob1.setFirstName("D");
ob1.setSalary(40000);
//Creating the second object
UserInfo ob2 = new UserInfo();
ob2.setFirstName("A");
ob2.setSalary(20000);
//Creating the third object
UserInfo ob3 = new UserInfo();
ob3.setFirstName("Z");
ob3.setSalary(35000);
session.beginTransaction();
session.save(ob1); //Saving the first object
session.save(ob2); //Saving the second object
session.save(ob3); //Saving the third object
session.getTransaction().commit();
session.close();
//Opening a new Session
session = sf.openSession();
//Creating the CritetiaBuilder from the session object
CriteriaBuilder cb = session.getCriteriaBuilder();
//Beginning the Transaction
session.beginTransaction();
//Creating the CriteriaQuery of an 'Object' type to represent result of this query
CriteriaQuery<Object> crt1 = cb.createQuery(Object.class);
Root<UserInfo> root1 = crt1.from(UserInfo.class);
//Finding the average of a column mapping the property named "salary"
crt1.select(cb.avg(root1.get("salary")));
Query query1 = session.createQuery(crt1);
//Executing Query to get a particular record in an Object from UserInfo
Object average = query1.getSingleResult();
System.out.println("Average of salaries : " + average);
System.out.println();
//Creating the CriteriaQuery of an 'Object' type to represent result of this query
CriteriaQuery<Object> crt2 = cb.createQuery(Object.class);
Root<UserInfo> root2 = crt2.from(UserInfo.class);
crt2.select(cb.count(root2)); //Finding the total count of records
Query query2 = session.createQuery(crt2);
Object count = query2.getSingleResult();
System.out.println("Total count of records : " + count);
System.out.println();
//Creating the CriteriaQuery of an 'Object' type to represent result of each query
CriteriaQuery<Object> crt3 = cb.createQuery(Object.class);
Root<UserInfo> root3 = crt3.from(UserInfo.class);
//Finding the minimum salary from a column mapping the property "salary"
crt3.select(cb.max(root3.get("salary")));
Query query3 = session.createQuery(crt3);
Object maxSalary = query3.getSingleResult();
System.out.println("Maximum Salary : " + maxSalary);
System.out.println();
//Creating the CriteriaQuery of an 'Object' type to represent result of each query
CriteriaQuery<Object> crt4 = cb.createQuery(Object.class);
Root<UserInfo> root4 = crt4.from(UserInfo.class);
//Finding the minimum salary from a column mapping the property "salary"
crt4.select(cb.min(root4.get("salary")));
Query query4 = session.createQuery(crt4);
Object minSalary = query4.getSingleResult();
System.out.println("Minimum Salary : " + minSalary);
System.out.println();
//Committing the Transaction
session.getTransaction().commit();
//closing the session
session.close();
}
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">promila21</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- Names the mapping entity class -->
<mapping class ="decodejava.UserInfo"/>
</session-factory>
</hibernate-configuration>
Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Hibernate: create table USER_INFO (ID number(10,0) not null, First_Name varchar2(255 char), Salary number(10,0), primary key (ID))
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into USER_INFO (First_Name, Salary, ID) values (?, ?, ?)
Hibernate: insert into USER_INFO (First_Name, Salary, ID) values (?, ?, ?)
Hibernate: insert into USER_INFO (First_Name, Salary, ID) values (?, ?, ?)
Hibernate: select avg(userinfo0_.Salary) as col_0_0_ from USER_INFO userinfo0_
Average of salaries : 31666.666666666668
Hibernate: select count(userinfo0_.ID) as col_0_0_ from USER_INFO userinfo0_
Total count of records : 3
Hibernate: select max(userinfo0_.Salary) as col_0_0_ from USER_INFO userinfo0_
Maximum Salary : 40000
Hibernate: select min(userinfo0_.Salary) as col_0_0_ from USER_INFO userinfo0_
Minimum Salary : 20000
select * from User_Info
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement