Saturday, April 21, 2012

Hibernate Annotation Examples


package com.hibernate.venkat;

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

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



/**
 * @author venkat
 *
 */
@Entity
@Table(name="EmployeeInfo")
public class Employee {

/**
* @param args
*/
private int empId;
private String empName;
private String empPassword;
private String empEmailAddress;
private boolean permanent;
private Calendar empJoinDate;
private Date empLoginTime;

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

@Column(nullable=false)
public String getEmpEmailAddress() {
return empEmailAddress;
}
public void setEmpEmailAddress(String empEmailAddress) {
this.empEmailAddress = empEmailAddress;
}
@Basic
public boolean isPermanent() {
return permanent;
}
public void setPermanent(boolean permanent) {
this.permanent = permanent;
}
@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;
}


@Id
@Column(name="EmployeeId")
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;
}


}
---------------------------------------------------------------------------------------------
package com.hibernate.venkat;

import java.sql.Date;
import java.util.GregorianCalendar;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * @author venkat
 *
 */
public class TestEmployee {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(Employee.class);
config.configure("hibernate.cfg.xml");

//new SchemaExport(config).create(true, true);

SessionFactory factory=config.buildSessionFactory();
Session session=factory.getCurrentSession();
session.beginTransaction();
Employee emp=new Employee();
emp.setEmpId(102);
emp.setEmpName("Sunil Narsipura");
emp.setEmpEmailAddress("narsip@att.com");
emp.setEmpJoinDate(new GregorianCalendar(2012,03,21));
emp.setEmpLoginTime(Date.valueOf("2012-03-21"));
emp.setEmpPassword("sunil");
emp.setPermanent(false);
session.save(emp);
session.getTransaction().commit();


}

}
---------------------------------------------------------------------------------------------
package com.hibernate.venkat;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

@Entity
@Table(name="Student",schema="venkat")
public class Student {

private String Sname;
private int Sno;
public String getSname() {
return Sname;
}
public void setSname(String sname) {
Sname = sname;
}
@Id
@TableGenerator(name="studentId",table="studentpktb"
,pkColumnName="stukey",pkColumnValue="studentValue",allocationSize=1)
@GeneratedValue(strategy=GenerationType.TABLE,generator="studentId")
public int getSno() {
return Sno;
}
public void setSno(int sno) {
Sno = sno;
}
}
---------------------------------------------------------------------------------------------
/**
 * 
 */
package com.hibernate.venkat;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * @author Venkat
 *
 */
public class TestStudent {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(Student.class);
config.configure();
new SchemaExport(config).create(true, true);
SessionFactory sf= config.buildSessionFactory();
Session s=sf.getCurrentSession();
s.beginTransaction();
Student s1=new Student();
s1.setSname("satya");
Student s2=new Student();
s2.setSname("krishna");
Student s3=new Student();
s3.setSname("pavan");
s.save(s1);
s.save(s2);
s.save(s3);
s.getTransaction().commit();

}

}
---------------------------------------------------------------------------------------------
<?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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">venkat</property>
        <property name="connection.password">venkat</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
<!--         <property name="hbm2ddl.auto">create</property> -->

<!--         <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
<!--         <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->

    </session-factory>

</hibernate-configuration>


No comments:

Post a Comment