Hibernate using Annotation

Hibernate Program with Oracle database using eclipse

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 and Oracle Driver jar

screenshot_20170217_202544

Writing Hibernate program with Oracle Database

1. Create Java bean class(POJO class) User_info.java In this class we will write annotations

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

3. Create Hibernate program User_creation.java to store data in database

Let’s Create Simple hibernate program save Object in database.

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
31
32
package com.spy.hibernate;
 
import javax.persistence.Entity;
import javax.persistence.Id;
 
@Entity
public class User_info 
{
 
 
 String name;
 
 @Id
 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;
 }
 
}

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">create</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 class="com.spy.hibernate.User_info"/>
 
 </session-factory>
 
</hibernate-configuration>

Hibernate program 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
package com.spy.hibernate;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
 
 
public class User_Creation 
{
 
 public static void main(String[] args)
 {
 
 User_info user1=new User_info();
 
 user1.setId(1);
 user1.setName("Bhavesh");
 
 SessionFactory sf=new AnnotationConfiguration().configure().buildSessionFactory();
 
 Session session=sf.openSession();
 
 session.beginTransaction();
 
 session.save(user1);
 
 session.getTransaction().commit();
 
 session.close();
 
 }
 
}

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

See in your Oracle Database using query: Select *from user_info;

oradb_annotation

 

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

Leave a Reply

Your email address will not be published. Required fields are marked *