Skip to main content

CRUD operation in Hibernate

CRUD is meant for Create,Retrieve,Update and Delete.

Create, retrieve, update and delete (CRUD) refers to the four major function to perform database operation.


Create Below file.

Userdata.java
Hibernate.cfg.xml


Userdata.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
 
package com.spy.operation;
 
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
 
 
@Entity
public class Userdata
	{
		@Id @GeneratedValue(strategy=GenerationType.AUTO)
		int id;
 
		String user;
 
		public int getId() 
		{
		return id;
		}
		public void setId(int id) 
		{
		this.id = id;
		}
		public String getUser() 
		{
		return user;
		}
		public void setUser(String user) 
		{
		this.user = user;
        }
}

Hibenrate.cfg.xml

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
<?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">create</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.operation.Userdata"/>
 
	</session-factory>
</hibernate-configuration>

Create operation in Hibernate.


Create_Test.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
 
package com.spy.operation;
import javax.jws.soap.SOAPBinding.Use;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class Create_Test
{
	public static void main(String[] args)
	{
		for(int i=1;i<=10;i++)
		{
			Userdata user=new Userdata();
			user.setUser("user "+i);
		}
		SessionFactory sessionfactory=new Configuration().configure().buildSessionFactory();
 
		Session session=sessionfactory.openSession();
 
		session.beginTransaction();
 
		 //Saving object in database
		for(int i=1;i<=10;i++)
		{
			Userdata user=new Userdata();
			user.setUser("user "+i);
			session.save(user);
		}
		session.getTransaction().commit();
		session.close();
	}
}

Output:

1
2
3
4
5
6
7
8
9
10
11
 
Hibernate: insert into Userdata (user) values (?)
Hibernate: insert into Userdata (user) values (?)
Hibernate: insert into Userdata (user) values (?)
Hibernate: insert into Userdata (user) values (?)
Hibernate: insert into Userdata (user) values (?)
Hibernate: insert into Userdata (user) values (?)
Hibernate: insert into Userdata (user) values (?)
Hibernate: insert into Userdata (user) values (?)
Hibernate: insert into Userdata (user) values (?)
Hibernate: insert into Userdata (user) values (?)

Userdata Table:
user_data

Retrieve operation in Hibernate.


Retrievinguser.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
 
package com.spy.query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.spy.operation.Userdata;
 
public class RetrievingUser
{
 
	public static void main(String[] args)
	{
		SessionFactory sf=new Configuration().configure().buildSessionFactory();
 
		Session session=sf.openSession();
 
		session.beginTransaction();
 
		Userdata user=(Userdata)session.get(Userdata.class, 5);
 
		System.out.println(user.getUser());
 
		session.close();
	}
 
}

Output:

1
2
3
4
5
 
Hibernate: select userdata0_.id as id1_0_, userdata0_.user as user1_0_ from
Userdata userdata0_ where userdata0_.id=?
 
user 5

Note:
Here we have Retrieve user in user’s session.
If we retrieve user after closing the session it will give a result.
As we talk about lazy initialization , first level variable fetch and it is
Default behavior.

Example: After closing session accessing data.

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.query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.spy.operation.Userdata;
 
public class RetrievingUser
{
	public static void main(String[] args)
	{
		SessionFactory sf=new Configuration().configure().buildSessionFactory();
 
		Session session=sf.openSession();
 
		session.beginTransaction();
 
		Userdata user=(Userdata)session.get(Userdata.class, 5);
 
		session.close();
 
		//retrieving data after closing session
		 System.out.println(user.getUser());
	}
 
}
 
 
Output:
<pre lang="java" line="1">
 
Hibernate: select userdata0_.id as id1_0_, userdata0_.user as user1_0_ from
Userdata userdata0_ where userdata0_.id=?
user 5

Update operation in Hibernate.


For update operation First you need to fetch user object.
Lets see example

Updateuser.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.query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.spy.operation.Userdata;
public class Updateuser
{
	public static void main(String[] args)
	{
		SessionFactory sf=new Configuration().configure().buildSessionFactory();
 
		Session session=sf.openSession();
 
		session.beginTransaction();
 
		Userdata user=(Userdata)session.get(Userdata.class, 5);
 
		System.out.println(user.getUser());
 
		user.setUser("updated user");
 
		session.update(user);
 
		session.getTransaction().commit();
 
		session.close();
	}
}

Output:

1
2
3
4
5
 
Hibernate: select userdata0_.id as id1_0_, userdata0_.user as user1_0_ from
Userdata userdata0_ where userdata0_.id=?
user 5
Hibernate: update Userdata set user=? where id=?

See Userdata table:
update

Delete operation in Hibernate.


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
 
package com.spy.query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.spy.operation.Userdata;
 
public class Deleteuser
{
	public static void main(String[] args)
	{
		SessionFactory sf=new Configuration().configure().buildSessionFactory();
 
		Session session=sf.openSession();
 
		session.beginTransaction();
 
		//fetching user 10 data
		Userdata user=(Userdata)session.get(Userdata.class, 10);
 
		//deleting user 10
		session.delete(user);
 
		session.getTransaction().commit();
 
		session.close();
	}
}

Output:

1
2
3
4
 
Hibernate: select userdata0_.id as id1_0_, userdata0_.user as user1_0_ from
Userdata userdata0_ where userdata0_.id=?
Hibernate: delete from Userdata where id=?

After deleting user 10 Userdata table
Userdata Table
delete