Transiant ,Temporaland table Annotation in Hibernate
Model Class:
package com.learnjavabyanand;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
/**
* @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;
//ignore this variable and not create column for us
@Transient
private String employeeNumber;
@Column(name="employee_location")
private String employeeLocation ;
//Used because to store Date not full time stamp
@Temporal(TemporalType.DATE)
private Date todayDate;
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;
}
public Date getTodayDate() {
return todayDate;
}
public void setTodayDate(Date todayDate) {
this.todayDate = todayDate;
}
}
HiberNate Client :-
package com.learnjavabyanand;
import java.util.Date;
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");
employee.setTodayDate(new Date());
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();
}
}
}
import java.util.Date;
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");
employee.setTodayDate(new Date());
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