20/07/2019, 10:11

Tạo Hibernate Configuration với Hibernate Tools

Hibernate là gì? Bài này hướng dẫn bạn Tạo Hibernate Configuration với Hibernate Tools (hibernate.cfg.xml) with Hibernate Tools . Tạo “hibernate.cfg.xml” kết nối với MySQL database . Đầu tiên bạn phải cài đặt plugin như trong bài hướng dẫn cài đặt Hibernate/Jboss ...

Hibernate là gì?

Bài này hướng dẫn bạn Tạo Hibernate Configuration với Hibernate Tools (hibernate.cfg.xml) with Hibernate Tools. Tạo “hibernate.cfg.xml” kết nối với MySQL database.

Đầu tiên bạn phải cài đặt plugin như trong bài hướng dẫn cài đặt Hibernate/Jboss Tools trong Eclipse IDE.

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

  • Eclipse 4.7 oxygen.
  • JBoss Tools 4.5.2.Final Eclipse plugin.
  • MySQL 10.1.29-MariaDB (from xampp-win32-7.2.0-0-VC15-installer).

Nội dung chính:

  • 1. Show Hibernate Perspective
  • 2. Create New Hibernate Configuration
  • 3. Result

1. Show Hibernate Perspective

Select “Windows” –> “Perspective” –> “Open Perspective” –> “Others…” –> choose “Hibernate“.

Generate Hibernate Configuration File With Hibernate Tools

2. Create New Hibernate Configuration

In Hibernate Configuration, right click and select “Add Configuration…

Generate Hibernate Configuration File With Hibernate Tools

In “Edit Configuration” dialog box:

  1. In “Name” box, input your configuration name.
  2. In “Hibernate Version” dropdown, select Hibernate Version.
  3. In “Project” box, click on the “Browse…” button to select your hibernate project.
  4. In “Database Connection” box, click “New…” button to create your database settings.

    Generate Hibernate Configuration File With Hibernate Tools
  5. In “Configuation File” box, click “Setup…” button to create new or use existing hibernate configuration file (hibernate.cfg.xml).

    Click “Create new…” to create new hibernate configuration file (hibernate.cfg.xml):

    Generate Hibernate Configuration File With Hibernate Tools
  6. Select your hibernate project and folder to create hibernate configuration file (hibernate.cfg.xml).

    Generate Hibernate Configuration File With Hibernate Tools
  7. Input your MySql database information:
    Generate Hibernate Configuration File With Hibernate Tools
  8. Click “Finish” button.

3. Result

File: 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"> 
            com.mysql.jdbc.Driver
        </property>
        <property name="hibernate.connection.url"> 
            jdbc:mysql://localhost:3306/testdb
        </property>
        <property name="hibernate.connection.username"> 
            root
        </property>
        <property name="hibernate.connection.password">
            1234567890
        </property>
        <property name="hibernate.dialect"> 
            org.hibernate.dialect.MySQLDialect
        </property>
    </session-factory>
</hibernate-configuration>
Hibernate là gì?
0