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).
1. Show Hibernate Perspective
Select "Windows" --> "Perspective" --> "Open Perspective" --> "Others..." --> choose "Hibernate".
2. Create New Hibernate Configuration
In Hibernate Configuration, right click and select "Add Configuration..."
In "Edit Configuration" dialog box:
- In "Name" box, input your configuration name.
- In "Hibernate Version" dropdown, select Hibernate Version.
- In "Project" box, click on the "Browse..." button to select your hibernate project.
-
In "Database Connection" box, click "New..." button to create your database settings.
-
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):
-
Select your hibernate project and folder to create hibernate configuration file (hibernate.cfg.xml).
-
Input your MySql database information:
- 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>