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
❮ ❯

Sử dụng Hibernate Tools tạo các file mapping và annotation


Hibernate là gì?
Hibernate many-to-many relationship với MySQL trên Eclipse

Bài này chúng tôi hướng dẫn bạn làm thế nào để sử dụng Hibernate Tools tạo các file mapping (hbm) và annotation từ các table trong database một cách tự động.

Lưu ý: trước khi tiếp tục bạn cần phải cài đặt Hibernate Tools theo như hướng dẫn trong bài Cài đặt Hibernate/Jboss Tools trong Eclipse IDE

Các Tool được sử dụng trong bài này:

  • Eclipse kepler 2
  • MySQL 5.0
  • JDK 1.8

Nội dung chính

  • 1. Hiển thị Hibernate Perspective
  • 2. Tạo Hibernate Project
  • 3. Tạo Hibernate Configuration
  • 4. Tạo Hibernate Code một cách tự động

1. Hiển thị Hibernate Perspective

Trong Eclipse, tại menu bar, chọn "Windows" –> "Open Perspective" –> "Others".

Tại đây, thực hiện double click vào "Hibernate" hoặc click [OK] để hiển thị Hibernate Perspective ra giao diện chính của Eclipse.

Sử dụng Hibernate Tools tạo các file mapping và annotation

2. Tạo Hibernate Project

Tạo project bằng maven có cấu trúc như sau:

Sử dụng Hibernate Tools tạo các file mapping và annotation

File 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>hibernate-auto-mapping</groupId>
  <artifactId>hibernate-auto-mapping</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <!-- dinh nghia cac thu vien -->
  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>3.6.3.Final</version>
    </dependency>
    <dependency>
      <groupId>javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.12.1.GA</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>
  </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source />
          <target />
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Định nghĩa file hibernate.cfg.xml

<?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>
   <!-- Assume test is the database name -->
   <property name="hibernate.connection.url">
      jdbc:mysql://localhost/testdb
   </property>
   <property name="hibernate.connection.username">
      root
   </property>
   <property name="hibernate.connection.password">
      1234567890
   </property>
   <!-- List of XML mapping files -->
   <mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>

3. Tạo Hibernate Configuration

Trong Hibernate Perspective, right click và chọn "Add Configuration…"

Sử dụng Hibernate Tools tạo các file mapping và annotation

Tại "Edit Configuration" dialog

  1. Tại hạng mục "Project", click button "Browser..." để chọn project
  2. Tại hạng mục "Database Connection", click button "New" để thiết lập kết nối đến database của bạn. Lưu ý: trong hướng dẫn này chúng tôi đã tạo ra một database connection có tên là "MySQLConnector", bạn cũng có thể tạo ra các database connection cho riêng mình.
  3. Tại hạng mục "Configuration File", click button "Setup" để tạo mới hoặc sử dụng file cấu hình hibernate "hibernate.cfg.xml" đã tồn tại trong project

    Sử dụng Hibernate Tools tạo các file mapping và annotation

Xem danh sách các bảng trong database của bạn tại "Hibernate Perspective"

Sử dụng Hibernate Tools tạo các file mapping và annotation

4. Tạo Hibernate Code một cách tự động

Đến bước này là bạn đã có thể tạo ra các file mapping (hbm) và annotation, bạn cũng có thể tạo ra file cấu hình hibernate "hibernate.cfg.xml" nếu như nó chưa tồn tại.

Click vào biểu tượng "Hibernate code generation", sau đó click "Hibernate Code Generation Configuration".

Sử dụng Hibernate Tools tạo các file mapping và annotation

Tạo new configuration:

Nhập hạng mục Name, sau đó chọn tab "Main", rồi nhập hạng mục "Output directory" - nơi lưu các file được tạo ra, và check vào tùy chọn "Reverse engineer from JDBC Connection".

Sử dụng Hibernate Tools tạo các file mapping và annotation

Tại tab "Exporter", chọn những gì bạn muốn Hibernate Tools tạo ra cho bạn chẳng hạn như Entity, mapping file (.hbm.xml) , DAO, annotation code, ...

Cuối cùng, click button "Run"

Sử dụng Hibernate Tools tạo các file mapping và annotation

Kết quả:

Sử dụng Hibernate Tools tạo các file mapping và annotation

Chúc các bạn thành công!

Bài tiếp theo: Ví dụ mối quan hệ many-to-many trong Hibernate

Hibernate là gì?
Mối quan hệ many-to-many trong Hibernate

Recent Updates

Sắp Tết 2024 Rồi! - Còn bao nhiêu ngày nữa là đến tết 2024?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 Hibernate

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