Hibernate framework Introduction

Hibernate Introduction

  • Hibernate is an ORM(Object Relation Mapping)  tool.
  • As we Know that in Java everything is an object so Hibernate map that object into Database so it is called as Object Relation mapping Tool.
  • It Help java Application to Interact with Database.

hibernate

Advantage of  Hibernate
  • Hibernate is Open source and lightweight.
  • Hibernate support database Independent query.(Hibernate query language)
  • It simplifies the interaction between java app and Database.
  • Performance of Hibernate is is High due to first level and second level cache.

Hibernate environment set up in Eclipse

Hibernate in eclipse

Download latest version of Hibernate

Click Here to Download

1. Create Java Project.

2. Right click on java Project and select Build path and Select Add external jar

3. Add All Hibernate  Required jar file.

screenshot_20170217_202544

Writing Hibernate program

1. Create configuration file Hibernate.cfg.xml
(Make sure You created this file under src folder

2. Create Mapping class(POJO class)
3. Create Main class
Let’s Create Simple hibernate program save Object in database.

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">com.mysql.jdbc.Driver</property>
 <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
 <property name="connection.username">root</property>
 <property name="connection.password">root</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.MySQLDialect</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 class="com.spy.hibernate.First.Myphonebook"/>
 
 </session-factory>
 
 </hibernate-configuration>

Java Bean class( POJO class)

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.spy.hibernate.First;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
 
@Entity
@Table(name="Contactbook")
public class Myphonebook 
{
 
	@Id @GeneratedValue(strategy=GenerationType.AUTO)
 
    int id;
 
	String name;
	String mobilenumber;
 
 
 
	public int getId() 
	{
		return id;
	}
 
	public void setId(int id) 
	{
		this.id = id;
	}
 
	public String getName() 
	{
		return name;
	}
	public void setName(String name) 
	{
		this.name = name;
	}
 
	public String getMobilenumber() 
	{
		return mobilenumber;
	}
 
	public void setMobilenumber(String mobilenumber)
	{
		this.mobilenumber = mobilenumber;
	}
}

Main class to Save object into database.

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
35
package com.spy.hibernate.First;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class CreatePhonebook 
{
 
 
	public static void main(String[] args) 
	{
 
	   Myphonebook contact=new Myphonebook();
 
	   contact.setMobilenumber("8866766995");
	   contact.setName("Bhavesh");
 
	    SessionFactory sf=new Configuration().configure().buildSessionFactory();
 
	    Session session=sf.openSession();
 
	    session.beginTransaction();
 
	    session.save(contact);
 
	    session.getTransaction().commit();
 
 
	    session.close();
 
 
	}
 
}

Output:
Output: Hibernate: insert into Myphonebook (mobilenumber, name) values (?, ?)

See in your mysql Database using query: Select *from Myphonebook;

createphonebook

Let’s understand Some of Hibernate annotation

@Entity
This annotation used to mark class as Entity.when You mark class as an Entity it Creates database table and Gives table name same as a class name.


@Entity(name=”Contactbook”)
It has only one Attribute called name.


@Table(name=”tablename”)
This annotation used to give a table name.


@Id
This annotation used to mark field as a primary key.


@Id @GeneratedValue(strategy=GenerationType.AUTO)
This annotation used to Generate Auto increment primary key.


@Transient
Hibernate Automatically create table by Seeing your member Variable of Class But if you want that
Some Variable not Save in Database Then Mark those field as a Transient.


@Temporal(TemporalType.Date)
This annotation used change Date format
Default it display time, date
But if you want only date will display then you have to use above Annotation.


@Lob
It means large object.
When you save object in database there are some Default size.
Suppose for varchar have 255 char.
But some field like Address, description require More space so we mark those a large object using above
annotation