VietTuts

Tự Học Lập Trình Online

  • Home
  • Java
  • Servlet
  • JSP
  • Struts2
  • Hibernate
  • Spring
  • MyBatis
  • Java WS
  • C
  • C++
  • C#
  • Python
  • PHP
  • Excel
  • VBA
  • Web
    • JavaScript
    • JQUERY
    • JSON
    • AJAX
    • CSS
    • HTML
    • HTML5
    • Node.js
    • Angular 7
  • SQL
    • MySQL
    • SQL Server
  • Misc
    • Eclipse
    • Phần mềm tiện ích
    • Cấu trúc DL&GT
    • Selenium Test

Hibernate Tuts

ORM là gì? Hibernate là gì? Hibernate - Kiến trúc Hibernate - Môi trường Hibernate - File cấu hình XML Hibernate - File Mapping Hibernate - Thuộc tính type Hibernate - Lớp persistent Hibernate - Sessions Hibernate - example Hibernate - O/R mapping Hibernate - Annotations Hibernate - Query Language Hibernate - Criteria Queries Hibernate - Native SQL Hibernate - Bộ nhớ cache Hibernate - Batch processing Hibernate - Interceptors

Hibernate với các ví dụ

Hibernate - many-to-one relationship với MySQL trên Eclipse Hibernate - one-to-one relationship với MySQL trên Eclipse Hibernate - one-to-many relationship với MySQL trên Eclipse Hibernate - many-to-many relationship với MySQL trên Eclipse

Hibernate Tool

Cài đặt Hibernate/Jboss Tools trong Eclipse IDE Tạo Hibernate Configuration (hibernate.cfg.xml) với Hibernate Tools Sử dụng Hibernate Tools tạo các file mapping và annotation

Hibernate 5 Examples

Ví dụ Hibernate 5 XML Mapping – MySQL Ví dụ Hibernate 5 XML Mapping – SQLServer 2017 Ví dụ Hibernate 5 Annotation Mapping – MySQL Ví dụ Hibernate 5 Annotation Mapping – SQLServer 2017

Hibernate 4 Examples

Ví dụ Hibernate 4 XML Mapping – MySQL Ví dụ Hibernate 4 XML Mapping – SQLServer 2017 Ví dụ Hibernate 4 Annotation Mapping – MySQL Ví dụ Hibernate 4 Annotation Mapping – SQLServer 2017

Hibernate 3 Examples

Ví dụ Hibernate 3 XML Mapping – MySQL Ví dụ Hibernate 3 XML Mapping – SQLServer 2017 Ví dụ Hibernate 3 Annotation Mapping – MySQL Ví dụ Hibernate 3 Annotation Mapping – SQLServer 2017

Phỏng vấn Hibernate

List câu hỏi phỏng vấn Hibernate
1 / 3
❮ ❯

Batch processing trong Hibernate


Bộ nhớ cache trong Hibernate
Interceptor trong Hibernate

Xem xét một tình huống khi bạn cần tải lên một số lượng lớn các bản ghi vào cơ sở dữ liệu của bạn bằng cách sử dụng Hibernate. Sau đây là đoạn code để đạt được điều này bằng cách sử dụng Hibernate:

Session session = SessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
    Employee employee = new Employee(.....);
    session.save(employee);
}
tx.commit();
session.close();

Mặc định, Hibernate sẽ lưu trữ tất cả các đối tượng đã tồn tại trong bộ nhớ cache cấp session và cuối cùng ứng dụng của bạn sẽ rơi vào tình trạng OutOfMemoryException khi ở hàng 50.000. Bạn có thể sử dụng batch processing trong Hibernate để giải quyết vấn đề này.

Để sử dụng tính năng batch processing trong Hibernate, đầu tiên hãy đặt hibernate.jdbc.batch_size có kích thước batch là X = 10 đến 50 hoặc một số khác nào đó tùy thuộc vào kích thước đối tượng. Điều này sẽ nói với các Hibernate rằng mỗi lô có X bản ghi được insert theo batch. Để thực hiện điều này trong đoạn code trên, chúng ta sẽ cần phải sửa đổi như sau:

Session session = SessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
    Employee employee = new Employee(.....);
    session.save(employee);
 if( i % 50 == 0 ) { // Tương tự như kích thước batch JDBC
        //flush một batch của các lệnh insert và giải phóng bộ nhớ
        session.flush();
        session.clear();
    }
}
tx.commit();
session.close();

Đoạn code trên sẽ làm việc tốt cho các hoạt động INSERT, nhưng khi bạn thực hiện hoạt động UPDATE thì bạn có sử dụng code sau đây:

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

ScrollableResults employeeCursor = session.createQuery("FROM EMPLOYEE").scroll();
int count = 0;

while ( employeeCursor.next() ) {
   Employee employee = (Employee) employeeCursor.get(0);
   employee.updateEmployee();
   seession.update(employee); 
   if ( ++count % 50 == 0 ) {
      session.flush();
      session.clear();
   }
}
tx.commit();
session.close();

Nội dung chính

  • Ví dụ về Batch Processing trong Hibernate
    • Download Source Code Eclipse

Ví dụ về Batch Processing trong Hibernate

Hãy sửa file cấu hình để thêm tính năng hibernate.jdbc.batch_size:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
 <session-factory>
  <property name="hibernate.dialect">
   org.hibernate.dialect.MySQLDialect
  </property>
  <property name="hibernate.connection.driver_class">
   com.mysql.jdbc.Driver
  </property>

  <!-- Thông tin kết nối db -->
  <property name="hibernate.connection.url">
   jdbc:mysql://localhost/testdb
  </property>
  <property name="hibernate.connection.username">
   root
  </property>
  <property name="hibernate.connection.password">
   1234567890
  </property>

  <!-- thiết định kích thước batch -->
  <property name="hibernate.jdbc.batch_size">
   50
  </property>

  <!-- List các file XML mapping -->
  <mapping resource="Employee.hbm.xml" />

 </session-factory>
</hibernate-configuration>

Tạo lớp java POJO Employee

package hibernate.batchprocessing.entity;

public class Employee {
 private int id;
 private String firstName;
 private String lastName;
 private int salary;

 public Employee() {
 }

 public Employee(String fname, String lname, int salary) {
  this.firstName = fname;
  this.lastName = lname;
  this.salary = salary;
 }

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String first_name) {
  this.firstName = first_name;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String last_name) {
  this.lastName = last_name;
 }

 public int getSalary() {
  return salary;
 }

 public void setSalary(int salary) {
  this.salary = salary;
 }
}

Tạo bảng EMPLOYEE để lưu trữ các đối tượng Empoyee

create table EMPLOYEE (
   id INT NOT NULL auto_increment,
   first_name VARCHAR(20) default NULL,
   last_name  VARCHAR(20) default NULL,
   salary     INT  default NULL,
   PRIMARY KEY (id)
);

Tạo file mapping để ánh xạ các đối tượng Employee với bảng EMPLOYEE.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
 "-//Hibernate/Hibernate Mapping DTD//EN"
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
   <class name="hibernate.batchprocessing.entity" table="EMPLOYEE">
      <meta attribute="class-description">
         Lớp này chứa chi tiết về employee
      </meta>
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      <property name="firstName" column="first_name" type="string"/>
      <property name="lastName" column="last_name" type="string"/>
      <property name="salary" column="salary" type="int"/>
   </class>
</hibernate-mapping>

Cuối cùng, chúng ta sẽ tạo ra lớp ứng dụng của với phương thức main() để chạy ứng dụng, trong đó chúng ta sẽ sử dụng các phương thức flush() và clear() của đối tượng Session để Hibernate tiếp tục ghi các bản ghi này vào cơ sở dữ liệu thay vì lưu trữ chúng trong bộ nhớ cache.

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import hibernate.batchprocessing.entity.Employee;

public class ManageEmployee {
 private static SessionFactory factory;

 public static void main(String[] args) {
  try {
   factory = new Configuration().configure().buildSessionFactory();
  } catch (Throwable ex) {
   System.err.println("Failed to create sessionFactory object." + ex);
   throw new ExceptionInInitializerError(ex);
  }
  ManageEmployee ME = new ManageEmployee();

  // Add employee records in batches
  ME.addEmployees();
 }

 // Method to create employee records in batches
 public void addEmployees() {
  Session session = factory.openSession();
  Transaction tx = null;

  try {
   tx = session.beginTransaction();
   for (int i = 0; i < 100000; i++) {
    String fname = "First Name " + i;
    String lname = "Last Name " + i;
    Integer salary = i;
    Employee employee = new Employee(fname, lname, salary);
    session.save(employee);
    if (i % 50 == 0) {
     session.flush();
     session.clear();
    }
   }
   tx.commit();
  } catch (HibernateException e) {
   if (tx != null)
    tx.rollback();
   e.printStackTrace();
  } finally {
   session.close();
  }
  return;
 }
}

Download Source Code Eclipse

Download Now!
Bài tiếp theo: Interceptor trong Hibernate

Bộ nhớ cache trong Hibernate
Interceptor trong Hibernate

Recent Updates

Ví dụ Hibernate 3 XML Mapping - SQLServer 2017Ví dụ Hibernate 4 Annotation Mapping - MySQLVí dụ Hibernate 4 Annotation Mapping - SQLServer 2017Ví dụ Hibernate 4 XML Mapping - MySQLVí dụ Hibernate 4 XML Mapping - SQLServer 2017Ví dụ Hibernate 5 Annotation Mapping - MySQLVí dụ Hibernate 5 Annotation Mapping - SQLServer 2017Ví dụ Hibernate 5 XML Mapping - MySQLVí dụ Hibernate 5 XML Mapping - SQLServer 2017Ví dụ về HibernateSự khác nhau giữa MyBatis và HibernateList câu hỏi phỏng vấn HibernateSắp Tết 2026 Rồi! - Còn bao nhiêu ngày nữa là đến tết 2026?

VietTuts on facebook

Học Lập Trình Online Miễn Phí - VietTuts.Vn
Danh Sách Bài Học

Học Java | Hibernate | Spring
Học Excel | Excel VBA
Học Servlet | JSP | Struts2
Học C | C++ | C#
Học Python
Học SQL

Bài Tập Có Lời Giải

Bài tập Java
Bài tập C
Bài tập C++
Bài tập C#
Bài tập Python
Ví dụ Excel VBA

Câu Hỏi Phỏng Vấn

201 câu hỏi phỏng vấn java
25 câu hỏi phỏng vấn servlet
75 câu hỏi phỏng vấn jsp
52 câu hỏi phỏng vấn Hibernate
70 câu hỏi phỏng vấn Spring
57 câu hỏi phỏng vấn SQL

Scroll back to top

Copyright © 2016 VietTuts.Vn all rights reserved. | Liên hệ | Chính sách - riêng tư | sitemap.html | sitemap_index.xml