0% found this document useful (0 votes)
619 views15 pages

Hibernate Demo With XML

Hibernate is an ORM tool that maps Java objects to database tables. The document provides steps to create a sample Hibernate application that stores an Employee object in an Oracle database table. It includes creating Java classes for the Employee entity and StoreData DAO class, mapping files, and a configuration file. The StoreData class saves a new Employee object to the database using Hibernate APIs and commits the transaction.

Uploaded by

Dinesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
619 views15 pages

Hibernate Demo With XML

Hibernate is an ORM tool that maps Java objects to database tables. The document provides steps to create a sample Hibernate application that stores an Employee object in an Oracle database table. It includes creating Java classes for the Employee entity and StoreData DAO class, mapping files, and a configuration file. The StoreData class saves a new Employee object to the database using Hibernate APIs and commits the transaction.

Uploaded by

Dinesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 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