Nearby lessons

12 of 19

Hibernate - Transaction Management

📌 What You Will Learn
  • Understand Hibernate - Transaction Management
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Hibernate - Transaction Management step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

EXAMPLE

Employee.java

EXAMPLE

Example02
JCode Cell
1 
2package com.durgasoft.hbn.pojo;
3

EXAMPLE

Example03
JCode Cell
1 
2import javax.persistence.Column;
3import javax.persistence.Entity;
4import javax.persistence.GeneratedValue;
5import javax.persistence.GenerationType;
6import javax.persistence.Id;
7import javax.persistence.SequenceGenerator;
8import javax.persistence.Table;
9import javax.persistence.TableGenerator;
10

EXAMPLE

Example04
JCode Cell
1 
2import org.hibernate.annotations.Generated;
3import org.hibernate.annotations.GenericGenerator;
4import org.hibernate.annotations.Parameter;
5

EXAMPLE

Value="10", valueColumnName="next_hi")

Example05
JCode Cell
1 
2@Entity
3@Table(name="emp1")
4public class Employee {
5@Id
6@Column(name="ENO")
7/*
8@SequenceGenerator(name="seqGen", sequenceName="my_sequence")
9@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seqGen")
10*/
11//@GeneratedValue(strategy=GenerationType.IDENTITY)
12//@GeneratedValue(strategy=GenerationType.AUTO)
13/*
14@TableGenerator(name="tableGen", table="my_table", pkColumnName="id", pkColumn
15

EXAMPLE

Example06
JCode Cell
1 
2@GeneratedValue(strategy=GenerationType.TABLE, generator="tableGen")
3*/
4@GenericGenerator(name="incrementGen", strategy="increment")
5@GeneratedValue(generator="incrementGen")
6

EXAMPLE

Example07
JCode Cell
1 
2private int eno;
3@Column(name="ENAME")
4private String ename;
5@Column(name="ESAL")
6private float esal;
7@Column(name="EADDR")
8private String eaddr;
9

EXAMPLE

Example08
JCode Cell
1 
2public int getEno() {
3return eno;
4}
5public void setEno(int eno) {
6this.eno = eno;
7}
8public String getEname() {
9return ename;
10}
11public void setEname(String ename) {
12this.ename = ename;
13}
14public float getEsal() {
15return esal;
16}
17public void setEsal(float esal) {
18this.esal = esal;
19}
20public String getEaddr() {
21return eaddr;
22}
23public void setEaddr(String eaddr) {
24this.eaddr = eaddr;
25}
26

EXAMPLE

hibernate.cfg.xml

Example09
JCode Cell
1 
2}
3

EXAMPLE

Example10
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<!DOCTYPE hibernate-configuration PUBLIC
4"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
5"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
6<hibernate-configuration>
7

EXAMPLE

y>

Example11
JCode Cell
1 
2<session-factory>
3<property name="connection.driver_Class">oracle.jdbc.OracleDriver</property>
4<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
5<property name="connection.username">system</property>
6<property name="connection.password">durga</property>
7<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</propert
8

EXAMPLE

Example12
JCode Cell
1 
2<property name="show_sql">true</property>
3<mapping class="com.durgasoft.hbn.pojo.Employee"/>
4</session-factory>
5

EXAMPLE

ClientApp.java

Example13
JCode Cell
1 
2<!--
3<session-factory>
4<property name="connection.driver_Class">com.mysql.jdbc.Driver</property>
5<property name="connection.url">jdbc:mysql://localhost:3306/durgadb</property>
6<property name="connection.username">root</property>
7<property name="connection.password">root</property>
8<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
9<property name="show_sql">true</property>
10<mapping class="com.durgasoft.hbn.pojo.Employee"/>
11</session-factory>
12-->
13</hibernate-configuration>
14

EXAMPLE

Example14
JCode Cell
1 
2package com.durgasoft.hbn.test;
3

EXAMPLE

Example15
JCode Cell
1 
2import org.hibernate.Session;
3import org.hibernate.SessionFactory;
4import org.hibernate.Transaction;
5import org.hibernate.boot.registry.StandardServiceRegistry;
6import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
7import org.hibernate.cfg.Configuration;
8

EXAMPLE

Example16
JCode Cell
1 
2import com.durgasoft.hbn.pojo.Employee;
3

EXAMPLE

Example17
JCode Cell
1 
2public class Test {
3

EXAMPLE

Example18
JCode Cell
1 
2public static void main(String[] args)throws Exception {
3Configuration cfg = new Configuration();
4cfg.configure();
5StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder();
6builder = builder.applySettings(cfg.getProperties());
7StandardServiceRegistry registry = builder.build();
8SessionFactory sessionFactory = cfg.buildSessionFactory(registry);
9Session session = sessionFactory.openSession();
10Employee emp = new Employee();
11//emp.setEno(111);
12emp.setEname("AAA");
13emp.setEsal(5000);
14emp.setEaddr("Hyd");
15Transaction tx = session.beginTransaction();
16session.save(emp);
17tx.commit();
18System.out.println("Employee Inserted Successfully");
19
Output

Employee Inserted Successfully
      

EXAMPLE

Transaction Management

Example19
JCode Cell
1 
2}
3}
4
📝 Key Takeaways
  • Key ideas of Hibernate - Transaction Management explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end