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 javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
//import javax.persistence.Parameter;
//import javax.persistence.Query;
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("First");
ob1.setLastName("User");
//Creating the second object
UserInfo ob2 = new UserInfo();
ob2.setFirstName("Second");
ob2.setLastName("User");
//Creating the third object
UserInfo ob3 = new UserInfo();
ob3.setFirstName("Third");
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 another session
session = sf.openSession();
//Creating the CriteriaBuilder from the session object
CriteriaBuilder cb = session.getCriteriaBuilder();
//Beginning the Transaction
session.beginTransaction();
//Creating the CriteriaQuery
CriteriaQuery<Object> crt = cb.createQuery(Object.class);
//Creating a Root of collection of UserInfo type of objects, root allows us to define attributes in the query
Root<UserInfo> root = crt.from(UserInfo.class);
//Specifying the item that is to be returned in the query result i.e. all the values in ID column,
//associated with the 'id' property of object type UserInfo
crt.select(root.get("id"));
//Using CriteriaQuery to creating a Query object to execute the query.
Query query = session.createQuery(crt);
//Executing Query to get particular records in a List from UserInfo
List<UserInfo> collection = query.getResultList();
System.out.println("Retrieving and displaying all ids in ID column having 'id' property ");
for(Integer object : collection)
{
System.out.println("Id : " + object);
System.out.println();
}
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 col_0_0_ from USER_INFO userinfo0_
Retrieving and displaying all ids in ID column having 'id' property
Id : 1
Id : 2
Id : 3
select * from User_Info
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement