Skip to main content

Hibernate using hbm Mapping file

Hibernate with oracle 12c using Eclipse IDE

Download oracle JDBC driver
Oracle Driver class

Driver class: oracle.jdbc.driver.OracleDriver
Dialect class for Oracle: org.hibernate.dialect.Oracle9Dialect

Create below file.

  1. Java Bean class (User_info.java)
  2. Hibernate Mapping file(User_info.hbm.xml)
  3. Hibernate Configuration file(Hibernate.cfg.xml)
  4. program to store data in database. (User_creation.java)

User_info.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.spy.hibernate;
 
import javax.persistence.Entity;
import javax.persistence.Id;
 
public class User_info 
{
 
 
	String name;
 
	int id;
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public int getId() {
		return id;
	}
 
	public void setId(int id) {
		this.id = id;
	}
 
}

User_info.hbm.xml

 
<?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
 
 <hibernate-mapping> 
 <class name="com.spy.hibernate.User_info" table="User_creation"> 
 <id name="id"> 
 <generator class="assigned"></generator> 
 </id> 
 
 <property name="name"></property> 
 </class> 
 
 </hibernate-mapping>

Hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
 
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 
<hibernate-configuration>
 
 <session-factory>
 
 <!-- Database connection settings -->
 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:ORCL</property>
 <property name="connection.username">sys as SYSDBA</property>
 <property name="connection.password">Blakhani2012</property>
 
 <!-- JDBC connection pool (use the built-in) -->
 <property name="connection.pool_size">10</property>
 
 <!-- Drop and re-create the database schema on startup -->
 <property name="hbm2ddl.auto">update</property>
 
 
 
 <!-- SQL dialect -->
 <property name="dialect">org.hibernate.dialect.Oracle9Dialect</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>
 
 <mapping resource="User_info.hbm.xml"/> 
 
 <!-- <mapping class="com.spy.hibernate.User_info"/> -->
 
 
 </session-factory>
 
</hibernate-configuration>

User_creation.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.spy.hibernate;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
 
public class User_Creation 
{
 
	public static void main(String[] args)
	{
 
		User_info user1=new User_info();
 
		user1.setId(2);
		user1.setName("Vivek");
 
 
 
		SessionFactory sf=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
 
		Session session=sf.openSession();
 
		session.beginTransaction();
 
		session.save(user1);
 
		session.getTransaction().commit();
 
		session.close();
 
	}
}

Output:
Hibernate: insert into User_Creation (name, id) values (?, ?)

Oracle Database:
ora_db