Hibernate Select Annotation Example
Hibernate select annotation example:
Dowload the Required JAR files:
Table Structure:
EMPNAME TABLE (empid int, empname varchar);
EMPSALARY TABLE (empid int, empsalary int); and table holds the below sample values like below,
EMPNAME: EMPSALARY
EMPID EMPNAME EMPID EMPSALARY
100 NAVEEN 100 5000
101 DIVYA 101 9000
102 HEMS 102 6000
103 RVS 103 11000
104 CHUDAR 104 7000
Project Structure:
Step 1: Create a hibernate configuration file “hibernate.cfg.xml” which has the connection url, username, password, dialect, driver and other informations.
Note: File name can be anything, but extension must be .cfg.xml
hibernate.cfg.xml:
[xml]
<?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 configurations –>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<!– Making autocommit false –>
<property name="hibernate.connection.autocommit">false</property>
<!– Displaying sql queries when running the program –>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<!– For JDBC connections –>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<!– Mapping file –>
<strong><mapping class="com.ngdeveloper.com.hibernate.bean.EmpName" />
<mapping class="com.ngdeveloper.com.hibernate.bean.EmpSalary" /></strong>
</session-factory>
</hibernate-configuration>
[/xml]
Step 2: Create a bean (pojo) classes EmpName and EmpSalary under the package com.ngdeveloper.com.hibernate.bean to map with tables,
EmpName.java:
[java]
package com.ngdeveloper.com.hibernate.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="EMPNAME")
public class EmpName {
public EmpName(){}
@Id
@GeneratedValue
@Column(name="EMPID")
private int empId;
@Column(name="EMPNAME")
private String empName;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
}
[/java]
EmpSalary.java:
[java]
package com.ngdeveloper.com.hibernate.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="EMPSALARY")
public class EmpSalary {
public EmpSalary(){}
@Id
@GeneratedValue
@Column(name="EMPID")
private int empId;
@Column(name="EMPSALARY")
private int empSalary;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public int getEmpSalary() {
return empSalary;
}
public void setEmpSalary(int empSalary) {
this.empSalary = empSalary;
}
}
[/java]
Step 3: Create a main (business) class EmpNameSalaryMainClass under com.ngdeveloper.com.hibernate.business and paste the below code,
[java]
package com.ngdeveloper.com.hibernate.business;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import com.ngdeveloper.com.hibernate.bean.EmpName;
import com.ngdeveloper.com.hibernate.bean.EmpSalary;
public class EmpNameSalaryMainClass {
public static void main(String[] args) {
AnnotationConfiguration af = new AnnotationConfiguration();
af.configure("hibernate.cfg.xml");
SessionFactory sf = af.buildSessionFactory();
Session session = sf.openSession();
Transaction trnsact = session.beginTransaction();
List employeeNames = session.createQuery("FROM EmpName").list();
for (Iterator idNameIter = employeeNames.iterator(); idNameIter
.hasNext();) {
EmpName empNam = (EmpName) idNameIter.next();
System.out.println("Employee Id : " + empNam.getEmpId());
System.out.println("Employee Name : " + empNam.getEmpName());
}
List employeeSalary = session.createQuery("FROM EmpSalary").list();
for (Iterator idSalIter = employeeSalary.iterator(); idSalIter
.hasNext();) {
EmpSalary empSal = (EmpSalary) idSalIter.next();
System.out.println("Employee Id : " + empSal.getEmpId());
System.out.println("Employee Salary : " + empSal.getEmpSalary());
}
trnsact.commit();
session.close();
}
}
[/java]
Step 4: Run the above main class.
Output:
Hibernate:
/*
FROM
EmpName */ select
empname0_.EMPID as EMPID0_,
empname0_.EMPNAME as EMPNAME0_
from
EMPNAME empname0_
Employee Id : 100
Employee Name : naveen
Employee Id : 101
Employee Name : divya
Employee Id : 102
Employee Name : hems
Employee Id : 103
Employee Name : rvs
Employee Id : 104
Employee Name : chudar
Hibernate:
/*
FROM
EmpSalary */ select
empsalary0_.EMPID as EMPID1_,
empsalary0_.EMPSALARY as EMPSALARY1_
from
EMPSALARY empsalary0_
Employee Id : 100
Employee Salary : 5000
Employee Id : 101
Employee Salary : 9000
Employee Id : 102
Employee Salary : 6000
Employee Id : 103
Employee Salary : 11000
Employee Id : 104
Employee Salary : 7000
Download the Source code:
Recommended Hibernate Books: