❮ Previous Next ❯

Example on Hibernate with Annotation

Example:

Files required to execute our program.

Student.java

package java8s;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name= "student123")
public class Student {
@Id
private int regdno;
private String firstName,lastName;
public int getRegdno() {
return regdno;
}
public void setRegdno(int regdno) {
this.regdno = regdno;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

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>
<property name="hbm2ddl.auto">create</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">oracle</property>
<propertyname="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping class="java8s.Student"/>
</session-factory>
</hibernate-configuration >

TestProgram.java

package java8s;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class TestProgram {
public static void main(String[] args) {
Session session=new AnnotationConfiguration()
.configure().buildSessionFactory().openSession();
Transaction t=session.beginTransaction();
Student s1=new Student();
s1.setRegdno(301);
s1.setFirstName("Trilochan");
s1.setLastName("Tarai");
Student s2=new Student();
s2.setRegdno(302);
s2.setFirstName("Akshay");
s2.setLastName("Swain");
session.persist(s1);
session.persist(s2);
t.commit();
session.close();
System.out.println("success");
}
}
❮ Previous Next ❯



Follow Us





  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved