Hibernate Demo With XML

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

HIBERNATE

DEMO
By Dinesh Kumar
Hibernate

ORM Tool

It is a programming technique that maps the


object to the data stored in the database.
Create java Project
Download and link the jar files
• Download Hibernate Jar Files.
• https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.4.3.Final/hibernate-release-
5.4.3.Final.zip/download

• Download the ojdbc jar:


• http://www.java2s.com/Code/Jar/o/Downloadojdbc14jar.htm
Add
libraries to
build path
Add libraries to build path
Employee.java

package com.studyskymate.hib;

public class Employee {


private int id;
private String firstName, lastName;
public void setFirstName(String firstName) {
public int getId() {
this.firstName = firstName;
return id;
}
}
public String getLastName() {
public void setId(int id) {
return lastName;
this.id = id;
}
}
public void setLastName(String lastName) {
public String getFirstName() {
this.lastName = lastName;
return firstName;
}
}
}
StoreData.java
package com.studyskymate.hib;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class StoreData {


public static void main(String[] args) {

StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();


Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();
StoreData.java
SessionFactory factory = meta.getSessionFactoryBuilder().build();
Session session = factory.openSession();
Transaction t = session.beginTransaction();

Employee e1 = new Employee();


//e1.setId(1);
e1.setFirstName("Dinesh");
e1.setLastName("Kumar");

session.save(e1);
t.commit();
System.out.println("successfully saved");
factory.close();
session.close();
}
}
employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 5.3//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">

<hibernate-mapping>
<class name="com.studyskymate.hib.Employee" table="emp1000">
<id name="id">
<generator class="increment"></generator>
</id>

<property name="firstName"></property>
<property name="lastName"></property>

</class>

</hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 5.3//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">

<hibernate-configuration>

<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="connection.url">jdbc:oracle:thin:@CEIT-SRV-
1.fnu.local:1521/orcl.fnu.local</property>
<property name="connection.username">hr</property>
<property name="connection.password">hr</property>
<property
name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="employee.hbm.xml"/>
</session-factory>

</hibernate-configuration>
Checked in Table
Thankyou

You might also like