0% found this document useful (0 votes)
55 views

Hibernate Program With Annotations

The document describes how to configure and run a basic Hibernate application using annotations without a mapping file or configuration file. The application requires 3 Java files - Emp.java which defines the entity class, HibernateSingleton.java which handles the session factory initialization, and HibernateExample.java which contains the main method to run queries. HibernateSingleton sets Hibernate properties like the driver, URL, username, password and dialect directly instead of using a configuration file.

Uploaded by

vaka
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Hibernate Program With Annotations

The document describes how to configure and run a basic Hibernate application using annotations without a mapping file or configuration file. The application requires 3 Java files - Emp.java which defines the entity class, HibernateSingleton.java which handles the session factory initialization, and HibernateExample.java which contains the main method to run queries. HibernateSingleton sets Hibernate properties like the driver, URL, username, password and dialect directly instead of using a configuration file.

Uploaded by

vaka
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Hibernate program with annotations

Files required.
1) Emp.java [our pojo class]
2) HibernateSingleton.java
3) HibernateExample.java
4) hibernate.cfg.xml
Emp.java
package com.vbr;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="EMP1")
public class Emp {
@Id
@Column(name="id")
private int id;
@Column(name="name")
private String name;
@Column(name="salary")
private double salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}

}
Hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<! -- Generated by MyEclipse Hibernate Tools.
-->
<hibernate-configuration>
<session-factory>
<property
name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property
name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">myneni</property>
<property name="connection.password">myneni</property>
<property
name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="myeclipse.connection.profile">hibdriver</property>
<mapping class="com.vbr.Emp"/>
</session-factory>
</hibernate-configuration>
HibernateSingleton.java
package com.vbr;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class HibernateSingleton {
private static SessionFactory factory=null;
static{
AnnotationConfiguration cfg=new AnnotationConfiguration();
cfg.configure();
factory=cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return factory;
}
public static Session getSession(){
return getSessionFactory().openSession();
}

}
HibernateExample.java
package com.vbr;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
public class HibernateExample {
public static void main(String[] args) {
Session session=HibernateSingleton.getSession();
String hqlQuery="from com.vbr.Emp";
Query query=session.createQuery(hqlQuery);
List list=query.list();
Iterator itr=list.iterator();
while(itr.hasNext()){
Emp emp=(Emp)itr.next();
System.out.print("emp no is : "+emp.getId()+"\t");
System.out.print("emp name is : "+emp.getName()+"\t");
System.out.println("emp salary is : "+emp.getSalary());
}
}
}
Hibernate Application without mapping file and configuration file
Files required.
1) Emp.java [our pojo class]
2) HibernateSingleton.java
3) HibernateExample.java
Emp.java
package com.vbr;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="EMP1")
public class Emp {
@Id
@Column(name="id")
private int id;
@Column(name="name")

private String name;


@Column(name="salary")
private double salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
HibernateSingleton.java
package com.vbr;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
public class HibernateSingleton {
private static SessionFactory factory=null;
static{
AnnotationConfiguration cfg=new AnnotationConfiguration();
cfg.setProperty("hibernate.connection.driver_class","oracle.jdbc.driver.Oracle
Driver");
cfg.setProperty("hibernate.connection.url","jdbc:oracle:thin:@localhost:1521:
xe");

cfg.setProperty("hibernate.connection.username","myneni");
cfg.setProperty("hibernate.connection.password","myneni");
cfg.setProperty("hibernate.dialect","org.hibernate.dialect.Oracle9Dialect");
cfg.setProperty("hibernate.show_sql","true");
cfg.addAnnotatedClass(Emp.class);
factory=cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return factory;
}
public static Session getSession(){
return getSessionFactory().openSession();
}
}
HibernateExample.java
package com.vbr;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
public class HibernateExample {
public static void main(String[] args) {
Session session=HibernateSingleton.getSession();
String hqlQuery="from com.vbr.Emp";
Query query=session.createQuery(hqlQuery);
List list=query.list();
Iterator itr=list.iterator();
while(itr.hasNext()){
Emp emp=(Emp)itr.next();
System.out.print("emp no is : "+emp.getId()+"\t");
System.out.print("emp name is : "+emp.getName()+"\t");
System.out.println("emp salary is : "+emp.getSalary());
}
}
}

You might also like