openplanning

Sử dụng Hibernate Tool tạo ra các class Entity từ các bảng

  1. Giới thiệu
  2. Cơ sở dữ liệu ví dụ, sử dụng trong tài liệu này
  3. Kết nối vào Database thông qua "Database Development"
  4. Tạo Project
  5. Cấu hình Hibenate
  6. Phụ lục: Lỗi khi tạo các entity
  7. Hướng dẫn học Hibernate

1. Giới thiệu

Tài liệu được viết dựa trên:
  • Eclipse 4.5 (MARS)

  • Hibernate 5.1.0.Final

  • Hibernate Tools 4.3.1.Final.html

(Last update document: 21-05-2016).
Trước hết bạn phải đảm bảo rằng đã cài đặt "Hibernate Tools" vào Eclipse. "Hibernate Tools" là một công cụ trong bộ công cụ của JBoss - "JBoss Tools".
Xem thêm:

2. Cơ sở dữ liệu ví dụ, sử dụng trong tài liệu này

Cơ sở dữ liệu dùng để minh họa trong bài viết này là "simplehr". Bạn có thể tham khảo tại:

3. Kết nối vào Database thông qua "Database Development"

Trên Eclipse chọn:
  • Window/Perspective/Open Perspective/Other...
Tạo mới một connection tới cơ sở dữ liệu simplehr:
Trong hướng dẫn này tôi sẽ kết nối tới loại cơ sở dữ liệu Oracle. Bạn có thể kết nối tới một cơ sở dữ liệu khác, cách làm hoàn toàn tương tự.
Nhấn nút khai báo Driver cho loại database bạn vừa chọn.
Nhấn nút "Add JAR/Zip"
Nhập các thông tin để kết nối tới database.Sau đó nhấn "Test Connection" để đảm bảo rằng việc kết nối tới database là thành công.
Bạn có thể nhìn thấy cấu trúc database của bạn trên "Data Source Explorer".
Thử query dữ liệu trên một bảng.

4. Tạo Project

Nhập vào:
  • Group Id: org.o7planning
  • Artiface Id: HibernateGenerateEntities
Project đã được tạo ra:
Khai báo thư viện Hibernate 5, và các thư viện JDBC cho Oracle, MySQL, SQL Server.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  http://maven.apache.org/xsd/maven-4.0.0.xsd">
 
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.o7planning</groupId>
  <artifactId>HibernateGenerateEntities</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>HibernateGenerateEntities</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

 <repositories>
     <!-- Repository for ORACLE JDBC Driver -->
     <repository>
         <id>codelds</id>
         <url>https://code.lds.org/nexus/content/groups/main-repo</url>
     </repository>
 </repositories>


  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
   <!-- Hibernate -->
     <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
     <dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-core</artifactId>
         <version>5.1.0.Final</version>
     </dependency>

     <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
     <dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-entitymanager</artifactId>
         <version>5.1.0.Final</version>
     </dependency>


     <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
     <dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-c3p0</artifactId>
         <version>5.1.0.Final</version>
     </dependency>


     <!-- MySQL JDBC driver -->
     <!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java -->
     <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
         <version>5.1.34</version>
     </dependency>

     <!-- Oracle JDBC driver -->
     <dependency>
         <groupId>com.oracle</groupId>
         <artifactId>ojdbc6</artifactId>
         <version>11.2.0.3</version>
     </dependency>

   <!-- SQLServer JDBC driver (JTDS) -->
   <!-- http://mvnrepository.com/artifact/net.sourceforge.jtds/jtds -->
   <dependency>
       <groupId>net.sourceforge.jtds</groupId>
       <artifactId>jtds</artifactId>
       <version>1.3.1</version>
   </dependency>
            
  </dependencies>
 
</project>

5. Cấu hình Hibenate

"Hibernate Tools" cho phép tạo ra file cấu hình Hibernate hoàn toàn trực quan. Và bây giờ chúng ta sẽ tạo file "hibernate.cfg.xml" bằng công cụ này.
Trên Eclipse chọn:
  • Window/Perspective/Open Perspective/Other...
Nhấn phải chuột chọn "Add Configuration.." để tạo mới file cấu hình Hibernate.
Nhập vào tên file "hibernate.cfg.xml", và đặt nó vào thư mục "src/main/java" của Project.
Bạn có thể nhập vào các thông tin kết nối tới database hoặc lấy từ thông tin kết nối mà bạn đã khai báo trước đó. Nhấn vào "Get values from Connection".
Chọn Connection "Oracle (Simplehr)", cái mà bạn đã tạo ra trước đây. Nhấn OK để tiếp tục.
Tiếp theo chọn lớp "dialect" tương ứng với loại Database của bạn. Với Oracle sẽ là Oracle10g, chú ý lớp dialect "Oracle10g" được dùng chung cho cả 2 phiên bản 10 và 11 của Oracle.
Nhấn OK để hoàn thành việc cấu hình. Sau bước này sẽ có file "hibernate.cfg.xml" được tạo ra trong thư mục src/main/java của Project của bạn.
Cấu hình Hibernate đã được tạo ra. (Xem trên Hibernate Configurations view)
Xem trên "Package Explorer":
  • hibernate.cfg.xml
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>
        <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="hibernate.connection.password">12345</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:db12c</property>
        <property name="hibernate.connection.username">simplehr</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
    </session-factory>
    
</hibernate-configuration>
Tiếp theo chúng ta sẽ cấu hình cách để công cụ tạo ra các lớp entity từ việc đọc cấu trúc bảng trong Database.
  • Window/Perspective/Open Perspective/Hibernate
Chú ý: Nếu quá trình tạo ra các entity bị lỗi, bạn có thể xem cách giải quyết trong phần phụ lục.

6. Phụ lục: Lỗi khi tạo các entity

Có thể khi bạn nhấn nút để tạo ra các Entity bạn sẽ gặp lỗi như sau:
** NoClassDefFoundError **
org.hibernate.console.HibernateConsoleRuntimeException: Received a NoClassDefFoundError,
probably the console configuration classpath is incomplete or contains conflicting versions of the same class
Received a NoClassDefFoundError, probably the console configuration classpath is incomplete or contains conflicting versions of the same class
org.hibernate.console.HibernateConsoleRuntimeException:
Received a NoClassDefFoundError, probably the console configuration classpath is incomplete or contains conflicting versions of the same class
Received a NoClassDefFoundError, probably the console configuration classpath is incomplete or contains conflicting versions of the same class
java.lang.NoClassDefFoundError: org/apache/commons/collections/MultiMap
org/apache/commons/collections/MultiMap
java.lang.ClassNotFoundException: org.apache.commons.collections.MultiMap cannot be found by org.jboss.tools.hibernate.runtime.v_5_1_5.0.1.Final-v20160331-1852-B88
org.apache.commons.collections.MultiMap cannot be found by org.jboss.tools.hibernate.runtime.v_5_1_5.0.1.Final-v20160331-1852-B88
Bạn cần cấu hình sử dụng Hibernate phiên bản cũ hơn.
Và chạy để tạo lại các Entity:

7. Hướng dẫn học Hibernate

Có thể bạn quan tâm: