Hibernate - Annotations Demo
Hibernate - Annotations Demo
package com.wipro.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="STUDENT_TBL")
public class Student {
@Id
@Column(name="stdid")
private int studentId;
@Column(name="stdName",length=20,nullable=false)
private String studentName;
private String course;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(int studentId, String studentName, String course) {
super();
this.studentId = studentId;
this.studentName = studentName;
this.course = course;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
<hibernate-configuration>
<session-factory>
<!-- Oracle Dialect -->
<property
name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Database Connection Settings -->
<property
name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property
name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">scott</property>
<property name="hibernate.connection.password">tiger</property>
<!-- To echo / show all executed queries on server output -->
<property name="hibernate.show_sql">true</property>
<!-- Used to Create or Alter Table -->
<!-- Given class structure not present in the database -->
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.enable_lazy_load_no_trans">true</property>
<!-- Mapping file entry -->
<!-- We need to map the class and not the mapping file -->
<mapping class="com.wipro.bean.Student"/>
</session-factory>
</hibernate-configuration>
5. Create the main class called StudentTester to test the created application
a. Since we have created configuration with custom name called myconfig.xml we have to
pass it as a parameter to the configure() method
package com.wipro.service;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.wipro.bean.Student;