Sunday, December 16, 2012

Hibernate Basic example For Simple persistance entity

Here I describe How to write basic Hibernate persistence class   and How to configure it.


1st it is need to configure hibernate properties.This can be done in 3 ways.
1.hibernate property file
2.hibernate configuration file
3.Set configurations programmatically.
2. Employee object relationally Mapped in to EmployeeInfo Table.
Mapping can be done using annotation or creating Hibernate managing files(hbm)
@Entity-declare class is an Entity
@Id-Id
@Table(name="EmployeeInfo") -table and table name declaration
@Column(name="EmployeeID")-column and column name declaration
@Column(nullable=false)-column contain values declaration
@Transient-If you dont want to create a column for property you need to mark property with transient anotation
@Temporal(TemporalType.DATE)  Temporal data can have DATE, TIME, or TIMESTAMP precision (ie the actual date, only the time, or both). Use the @Temporal annotation to fine tune that.
@Basic -By default, the @Basic annotation is used on every field


Employee Bean class



import java.util.Calendar;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name="EmployeeInfo")
public class Employee {
private int empId;
private String empName;
private String empEmailAddress;
private String empPassword;
private boolean isPermanent;
private Calendar empJoinDate;
private Date empLoginTime;

@Id
@Column(name="EmployeeID")
public int getEmpId() {
    return empId;
}
public void setEmpId(int empId) {
    this.empId = empId;
}
@Column(name="EmployeeName")
public String getEmpName() {
    return empName;
}
public void setEmpName(String empName) {
    this.empName = empName;
}
@Column(nullable=false)
public String getEmpEmailAddress() {
    return empEmailAddress;
}
public void setEmpEmailAddress(String empEmailAddress) {
    this.empEmailAddress = empEmailAddress;
    }

@Transient
public String getEmpPassword() {
    return empPassword;
}
public void setEmpPassword(String empPassword) {
    this.empPassword = empPassword;
}
@Basic
public boolean isPermanent() {
    return isPermanent;
}
public void setPermanent(boolean isPermanent) {
    this.isPermanent = isPermanent;
}

@Temporal(TemporalType.DATE)
public Calendar getEmpJoinDate() {
    return empJoinDate;
}
public void setEmpJoinDate(Calendar empJoinDate) {
    this.empJoinDate = empJoinDate;
}

@Temporal(TemporalType.TIMESTAMP)
public Date getEmpLoginTime() {
    return empLoginTime;
}
public void setEmpLoginTime(Date empLoginTime) {
    this.empLoginTime = empLoginTime;
}


}


Following Main method contains how to load Hibernate configuration and create hibernate session and How to Persist entity in to Data source using that session. 




Main method

public static void main(String[] args) {

        Configuration cfg = new Configuration();
        cfg.addAnnotatedClass(Employee.class);
        cfg.configure("hibernate.cfg.xml");
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                .applySettings(cfg.getProperties()).buildServiceRegistry();
        SessionFactory sessionFactory = cfg
                .buildSessionFactory(serviceRegistry);
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        Employee test = new Employee();
        test.setEmpId(1);
        test.setEmpName("Kalana Sandhana");
        test.setEmpEmailAddress("adxsaxax@a.com");
        test.setEmpJoinDate(new GregorianCalendar(2012, 11, 05));
        test.setEmpLoginTime(Date.valueOf("2012-06-10"));
        session.save(test);
        session.getTransaction().commit();

    }



Hibernate Log-
Hibernate: select sequence_next_hi_value from HIBERNATETESTSCHEMA.empPKtb where empkey = 'empvalue' for update with rs
Hibernate: update HIBERNATETESTSCHEMA.empPKtb set sequence_next_hi_value = ? where sequence_next_hi_value = ? and empkey = 'empvalue'
Hibernate: insert into HIBERNATETESTSCHEMA.EmployeeInfo (empEmailAddress, empJoinDate, empLoginTime, EmployeeName, permanent, EmployeeID) values (?, ?, ?, ?, ?, ?)

No comments:

Post a Comment