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.setLastName("User");
//Creating the second object
UserInfo ob2 = new UserInfo();
ob2.setFirstName("A");
ob2.setLastName("User");
//Creating the third object
UserInfo ob3 = new UserInfo();
ob3.setFirstName("Z");
ob3.setLastName("User");
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 CriteriaBuilder from the session object
CriteriaBuilder cb = session.getCriteriaBuilder();
//Beginning the Transaction
session.beginTransaction();
//Creating the CriteriaQuery of an 'Object' type, to represent result of each query
CriteriaQuery<Object> crt1 = cb.createQuery(Object.class);
Root<UserInfo> root1 = crt1.from(UserInfo.class);
//Calling asc() method of CriteriaBuilder to give an ascending order to result of Query
crt1.orderBy(cb.asc(root1.get("firstName")));
Query query1 = session.createQuery(crt1);
//Executing Query to get particular records in a List from UserInfo
List<UserInfo> collection1 = query1.getResultList();
System.out.println("Retrieving and displaying all records sorted in an ascending order of a property 'firstName' ");
for(UserInfo object : collection1)
{
System.out.println("ID : " + object.getId());
System.out.println("First Name : " + object.getFirstName());
System.out.println("First Name : " + object.getLastName());
System.out.println();
}
//Creating the CriteriaQuery of an 'Object' type, to represent result of each query
CriteriaQuery<Object> crt2 = cb.createQuery(Object.class);
Root<UserInfo> root2 = crt2.from(UserInfo.class);
//Calling desc() method of CriteriaBuilder to give a descending order to result of Query
crt2.orderBy(cb.desc(root2.get("firstName")));
Query query2 = session.createQuery(crt2);
//Executing Query to get particular records in a List from UserInfo
List<UserInfo> collection2 = query2.getResultList();
System.out.println("Retrieving and displaying all records sorted in a descending order of a property named 'firstName' ");
for(UserInfo object : collection2)
{
System.out.println("ID : " + object.getId());
System.out.println("First Name : " + object.getFirstName());
System.out.println("First Name : " + object.getLastName());
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), Last_Name varchar2(255 char), 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, Last_Name, ID) values (?, ?, ?)
Hibernate: insert into USER_INFO (First_Name, Last_Name, ID) values (?, ?, ?)
Hibernate: insert into USER_INFO (First_Name, Last_Name, ID) values (?, ?, ?)
Hibernate: select userinfo0_.ID as ID1_0_, userinfo0_.First_Name as First_Name2_0_, userinfo0_.Last_Name as Last_Name3_0_ from USER_INFO userinfo0_ order by userinfo0_.First_Name asc
Retrieving and displaying all records sorted in an ascending order of a property 'firstName'
ID : 2
First Name : A
First Name : User
ID : 1
First Name : D
First Name : User
ID : 3
First Name : Z
First Name : User
Hibernate: select userinfo0_.ID as ID1_0_, userinfo0_.First_Name as First_Name2_0_, userinfo0_.Last_Name as Last_Name3_0_ from USER_INFO userinfo0_ order by userinfo0_.First_Name desc
Retrieving and displaying all records sorted in a descending order of a property named 'firstName'
ID : 3
First Name : Z
First Name : User
ID : 1
First Name : D
First Name : User
ID : 2
First Name : A
First Name : User
select * from User_Info
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement