0%

How to install Hibernate in IntelliJ IDEA

1. Install database

In this post, we just use mysql as the database. You can use other databases as well.

First, you should install mysql in your computer. Then in terminal use your password to open mysql:

1
> mysql -u root -p

Create a test database:

1
2
3
4
5
6
mysql> CREATE DATABASE test;
mysql> use test;
mysql> CREATE TABLE example (
id INT,
data VARCHAR(100)
);

2. Import Hiberante into IntelliJ IDEA

Use the previous project Spring MVC example code. Unzip the code to a folder and use IntelliJ to open the code folder. You can refer the previous post to see more details.

2.1 Configure hibernate

In File -> Project Structure -> Modules -> Add -> Hibernate:

Add hibernate.cfg.xml:

2.2 Connect to mysql

Next, we need to connect to database (Now we use mysql for example, you can use what database you want). View -> Tool Windows -> Database -> Choose Mysql:

Fill your database information. Then click Apply. In Schema section, choose current schema.
Maybe, you need to install jdbc in the configuration. Just click install button in this configuration.

2.3 Generate model and hbm files

In View -> Tool Windows -> Persistence -> Generate Persistence Mapping -> By database schema:

Set where the generated code is. I set in the package org.shen.model. And choose the data source.

Then the files will be generated, and hibernate has been installed in IntelliJ.