11/08/2018, 19:00

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

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 Việc ánh xạ One-to-Many ...

  • 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

Việc ánh xạ One-to-Many có thể được thực hiện bằng tập Set trong java mà không chứa bất kỳ phần tử nào bị trùng lặp.

Một Set được ánh xạ với một phần tử <set> trong bảng ánh xạ và khởi tạo với java.util.HashSet. Bạn có thể sử dụng tập Set trong lớp của mình khi không có phần tử trùng lặp nào được yêu cầu.

Các công nghệ được sử dụng trong ví dụ này:

  • Eclipse MARS 2
  • MySQL 5.0.11
  • JDK 1.8
  • Hibernate 3.6.3

Cấu trúc project

cau truc project one-to-many-example

Các bước thực hiện

Trong ví dụ này của chúng ta một đối tượng nhân viên employee có thể có nhiều đối tượng chứng chỉ certificate.

Định nghĩa các table trong MySQL

Tạo bảng EMPLOYEE để lưu trữ các nhân viên sẽ có cấu trúc sau:

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)
);

Giả sử một nhân viên (employee) có một hoặc nhiều chứng chỉ (certificate). Chúng ta cần phải lưu những thông tin về chứng chỉ này trong bảng có cấu trúc như sau:

create table CERTIFICATE (
   id INT NOT NULL auto_increment,
   certificate_name VARCHAR(30) default NULL,
   employee_id INT default NULL,
   PRIMARY KEY (id)
);

Có mối quan hệ one-to-many giữa các EMPLOYEE và CERTIFICATE.

Định nghĩa các lớp POJO

Tạo lớp POJO Certificate tương ứng với bảng CERTIFICATE. Lớp này phải impliemnts cả hai phương thức equals() và hashCode() để Java có thể xác định có hai phần tử/đối tượng nào đó giống nhau không.

public class Certificate {
   private int id;
   private String name; 

   public Certificate() {}
   public Certificate(String name) {
      this.name = name;
   }
   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 boolean equals(Object obj) {
      if (obj == null) return false;
      if (!this.getClass().equals(obj.getClass())) return false;

      Certificate obj2 = (Certificate)obj;
      if((this.id == obj2.getId()) && (this.name.equals(obj2.getName())))
      {
         return true;
      }
      return false;
   }
   public int hashCode() {
      int tmp = 0;
      tmp = ( id + name ).hashCode();
      return tmp;
   }
}

Tạo lớp POJO Employee được sử dụng để lưu trữ các đối tượng vào bảng EMPLOYEE và có một tập các chứng chỉ được lưu bằng tập Set có tên biến là certificates

import java.util.*;

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

   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;
   }

   public Set getCertificates() {
      return certificates;
   }
   public void setCertificates( Set certificates ) {
      this.certificates = certificates;
   }
}

Định nghĩa các Hibernate Mapping File

Chúng ta phải tạo các file mapping để hướng dẫn Hibernate làm thế nào để ánh xạ các lớp vào các bảng cơ sở dữ liệu. Phần tử <set> sẽ được sử dụng để thiết lập mối quan hệ one-to-many giữa các EMPLOYEE và CERTIFICATE.

File: Employee.hbm.xml

<?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="vn.viettuts.hibernate.entity.Employee" table="EMPLOYEE">
        <meta attribute="class-description">
            Lớp này chưa thông tin chi tiết về employee.
        </meta>
        <id name="id" type="int" column="id">
            <generator class="native" />
        </id>
        <set name="certificates" cascade="all">
            <key column="employee_id" />
            <one-to-many class="vn.viettuts.hibernate.entity.Certificate" />
        </set>
        <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>

File: Certificate.hbm.xml

<?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="vn.viettuts.hibernate.entity.Certificate" table="CERTIFICATE">
        <meta attribute="class-description">
            Lớp này chưa thông tin chi tiết về certificate.
        </meta>
        <id name="id" type="int" column="id">
            <generator class="native" />
        </id>
        <property name="name" column="certificate_name" type="string" />
    </class>
</hibernate-mapping>

Chúng ta hãy xem chi tiết hơn về các phần tử của file mapping trong Hibernate như sau:

  • Tài liệu mapping là một tài liệu XML có <hibernate-mapping> là phần tử gốc chứa tất cả các phần tử <class>.
  • Các phần tử <class> được sử dụng để định nghĩa ánh xạ cụ thể từ các lớp Java sang các bảng cơ sở dữ liệu. Tên lớp Java được chỉ định sử dụng thuộc tính name của phần tử lớp và tên bảng cơ sở dữ liệu được chỉ định sử dụng thuộc tính table.
  • Phần tử <meta> là thành phần tùy chọn và có thể được sử dụng để tạo ra mô tả lớp.
  • Phần tử <id> ánh xạ thuộc tính ID duy nhất trong lớp tới khóa chính của bảng cơ sở dữ liệu. Thuộc tính name của id id đề cập đến thuộc tính trong lớp và thuộc tính column đề cập đến cột trong bảng cơ sở dữ liệu. Thuộc tính type giữ kiểu ánh xạ hibernate, các kiểu mapping này sẽ chuyển đổi từ kiểu dữ liệu Java sang SQL.
  • Phần tử <generator> bên trong phần tử id được sử dụng để tự động tạo giá trị cho khóa chính. Thiết lập thuộc tính class của phần tử generator được đặt là native để cho phép hibernate chọn identity, sequence hoặc hilo để tạo khoá chính tùy thuộc vào khả năng của cơ sở dữ liệu.
  • Phần tử <property> được sử dụng để ánh xạ một thuộc tính của lớp Java vào một cột trong bảng cơ sở dữ liệu. Thuộc tính name của phần tử đề cập đến thuộc tính trong lớp và thuộc tính column đề cập đến cột trong bảng cơ sở dữ liệu. Thuộc tính type giữ kiểu ánh xạ hibernate, các kiểu mapping này sẽ chuyển đổi từ kiểu dữ liệu Java sang SQL.
  • Phần tử <set> thiết lập mối quan hệ giữa các lớp Certificate và Employee. Chúng ta sử dụng thuộc tính cascade trong phần tử <set> để nói với Hibernate rằng các đối tượng Certificate tồn tại đồng thời với các đối tượng Employee. Thuộc tính name được đặt thành biến Set được định nghĩa trong cha, trong trường hợp của chúng ta là certificates. Đối với mỗi biến set, chúng ta cần định nghĩa một phần tử set riêng trong file mapping.
  • Phần tử <key> là cột trong bảng CERTIFICATE chứa khoá ngoại tới đối tượng cha như bảng EMPLOYEE.
  • Phần tử <one-to-many> chỉ ra rằng một đối tượng Employee liên quan đến nhiều đối tượng Certificate.

Tạo file cấu hình Hibernate

File cấu hình Hibernate được sử dụng để định nghĩa các thông tin kết nối database (trong ví dụ này là MySQL) và liên kết đến các file mapping.

<?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>

         
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost/testdb
        </property>
        <property name="hibernate.connection.username">
            root
        </property>
        <property name="hibernate.connection.password">
            1234567890
        </property>

         
        <mapping resource="Employee.hbm.xml" />
        <mapping resource="Certificate.hbm.xml" />

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

Tạo lớp ứng ụng

Cuối cùng, chúng ta sẽ tạo lớp chứa phương thức main () để chạy ứng dụng. Chúng ta sẽ sử dụng ứng dụng này để lưu các employee cùng với các certificate của họ. Sau đó áp dụng các thao tác CRUD bản ghi đã tạo.

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

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

import vn.viettuts.hibernate.entity.Certificate;
import vn.viettuts.hibernate.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();
        // Let us have a set of certificates for the first employee
        HashSet set1 = new HashSet();
        set1.add(new Certificate("MCA"));
        set1.add(new Certificate("MBA"));
        set1.add(new Certificate("PMP"));

        // Add employee records in the database
        Integer empID1 = ME.addEmployee("Ho", "Van A", 4000, set1);

        // Another set of certificates for the second employee
        HashSet set2 = new HashSet();
        set2.add(new Certificate("BCA"));
        set2.add(new Certificate("BA"));

        // Add another employee record in the database
        Integer empID2 = ME.addEmployee("Vo", "Van B", 3000, set2);

        System.out.println("Show list all employees");
        ME.listEmployees();

        System.out.println("Update empID1");
        ME.updateEmployee(empID1, 5000);

        System.out.println("Delete empID2");
        ME.deleteEmployee(empID2);

        System.out.println("Show list all employees");
        ME.listEmployees();

    }

    // Method to add an employee record in the database
    public Integer addEmployee(String fname, String lname, int salary, Set cert) {
        Session session = factory.openSession();
        Transaction tx = null;
        Integer employeeID = null;
        try {
            tx = session.beginTransaction();
            Employee employee = new Employee(fname, lname, salary);
            employee.setCertificates(cert);
            employeeID = (Integer) session.save(employee);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
        return employeeID;
    }

    // Method to list all the employees detail
    public void listEmployees() {
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            List employees = session.createQuery("FROM Employee").list();
            for (Iterator iterator1 = employees.iterator(); iterator1.hasNext();) {
                Employee employee = (Employee) iterator1.next();
                System.out.print("First Name: " + employee.getFirstName());
                System.out.print("  Last Name: " + employee.getLastName());
                System.out.println("  Salary: " + employee.getSalary());
                Set certificates = employee.getCertificates();
                for (Iterator iterator2 = certificates.iterator(); iterator2.hasNext();) {
                    Certificate certName = (Certificate) iterator2.next();
                    System.out.println("Certificate: " + certName.getName());
                }
            }
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

    // Method to update salary for an employee
    public void updateEmployee(Integer EmployeeID, int salary) {
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Employee employee = (Employee) session.get(Employee.class, EmployeeID);
            employee.setSalary(salary);
            session.update(employee);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }

    // Method to delete an employee from the records
    public void deleteEmployee(Integer EmployeeID) {
        Session session = factory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Employee employee = (Employee) session.get(Employee.class, EmployeeID);
            session.delete(employee);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
}

Kết quả chạy trên Eclipse

Show list all employees
First Name: Ho  Last Name: Van A  Salary: 4000
Certificate: MCA
Certificate: PMP
Certificate: MBA
First Name: Vo  Last Name: Van B  Salary: 3000
Certificate: BCA
Certificate: BA
Update empID1
Delete empID2
Show list all employees
First Name: Ho  Last Name: Van A  Salary: 5000
Certificate: MCA
Certificate: PMP
Certificate: MBA

Download Source Code Eclipse

Bài tiếp theo: Hibernate many-to-many relationship với MySQL trên Eclipse
Hibernate là gì?
0