HiberNate Simple Application Using Annotation
Model Class:
package com.learnjavabyanand;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
/**
* @author ananddw
*
*/
//@Entity will create table as per class name By if you specify name then table name must be the name what you will provide inside Entity
@Entity(name="employee_Details")
public class EmployeeDetails {
//we can put this annotation above getter as well
@Id
@Column(name="employee_name")
private String employeeName;
@Column(name="employee_number")
private String employeeNumber;
@Column(name="employee_location")
private String employeeLocation ;
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(String employeeNumber) {
this.employeeNumber = employeeNumber;
}
public String getEmployeeLocation() {
return employeeLocation;
}
public void setEmployeeLocation(String employeeLocation) {
this.employeeLocation = employeeLocation;
}
}
HiberNate Client :-
package com.learnjavabyanand;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
/**
*
* @author ananddw
*
*/
public class HibernateClient {
public static void main(String[] args) {
// TODO Auto-generated method stub
EmployeeDetails employee= new EmployeeDetails();
employee.setEmployeeName("Anand Dwivedi");
employee.setEmployeeNumber("MER-12");
employee.setEmployeeLocation("Bangalore");
try{
//get the configuration and create a session factory once in out application
SessionFactory factory=new AnnotationConfiguration().configure().buildSessionFactory();
//create session from session factory to save/retrive or other operation
Session session=factory.openSession();
//new transaction and save object inside trans
session.beginTransaction();
//save Object
session.save(employee);
//commit the session
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
}
}
}
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">
<hibernate-configuration>
<session-factory>
<!-- Postgres DB Details -->
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1:5432/hibernate</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">password</property>
<!-- Postgres DB Details End -->
<!-- Postgres Dialect has information of list of queries avil in Postgres DB -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Postgres Dialect End -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo show Query -->
<property name="show_sql">true</property>
<!-- Echo show Query End -->
<property name="format_sql">true</property>
<!-- Create every time table and insert Data -->
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- Create every time table and insert Data End -->
<!-- Define Package for mapping class -->
<mapping class="com.learnjavabyanand.EmployeeDetails"/>
<!-- Define Package for mapping class End -->
</session-factory>
</hibernate-configuration>
Folder Structure :
PS: Please Add all the hibernate required jars as well as Database jars
Comments
Post a Comment