- Prerequisites
- MySQL Driver creation
- Project creation
- Hibernate dependencies
- Persistance file
- Try code
- Database Reverse Engineering
-
MySQL: https://dev.mysql.com/downloads/installer/ using all default options
-
Last JDK: https://www.oracle.com/java/technologies/downloads
-
Eclipse: https://www.eclipse.org/downloads/ using the option "Eclipse IDE for Enterprise Java and Web Developers" during the installation
-
Hibernate installation: Help → Eclipse Marketplace → JBOSS TOOLS (Install ONLY Hibernate Tools)
-
Change Perspective: Window → Perspective → Open Perspective → Other → Hibernate
This driver is a prerequisite for the creation of the Persistance file.
Window → Preferences → Data Management → Connectivity → Driver Definitions
In JAR List remove all and add the JAR that you installed with MySQL (maybe in C:\Program Files (x86)\MySQL\Connector J 8.0):
Continue in tab Properties and then save it.
File → New → Project → Maven Project:
Call Group Id e Artifact Id as you prefer. Finish it and then wait until you have this folder structure
Right click on JRE System Library → Properties → Change the enviroment with the JRE that you have in your computer
If you click on "enviroment" button you can see with wich version is matched with the installed JRE (for me is JavaSE-18):
Create two Java package in src/main/java: com.hibernate.model
(for table-classes) and com.hibernate.app
(for manager-classes)
pom.xml
is the file of the project configuration of Maven.
Paste at the end of pom.xml
(before </project>
)
<dependencies>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.7.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
</dependencies>
Save the file. You must see the dependences downloaded inside "maven dependencies"
This is a file that allow hibernate to connect with the database.
File → New → Hibernate configuration file:
Next → "Get values from connection" → if you don't have a profile → New → MySQL → Driver created before Test the connection and go next:
Then change in Annotations and finish the setup:
Insert inside Hibernate.cfg.xml
and save it:
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
If is all ok, we must see the database connection with tables inside the tab "Hibernate Configurations":
Now we can try some code for testing connection.
Database table:
CREATE TABLE `courses` (
`COURSE_ID` bigint NOT NULL,
`COURSE_NAME` varchar(255) NOT NULL,
PRIMARY KEY (`COURSE_ID`)
);
Create and copy/paste these 3 java classes:
- Course.java in
com.hibernate.model
→ it contains the definition of the table - CourseManager.java in
com.hibernate.app
→ it contains CRUD operations on the table - App.java in
com.hibernate.app
→ it contains the main
Make attention at packages, jakarta importing and CFGFILE path.
Insert in hibernate.cfg.xml
the connection between hibernate and java classes:
<mapping class="com.hibernate.model.Course" />
Right click on CourseManager → Run As → Java Application → you must see red (INFO) and white (SQL) code in output terminal.
Check if there is some data inside courses
table.
With this setup, hibernate can create java class from database tables.
Run → Hibernate Code Generation → Hibernate Code Generation Configurations → New Configuration:
In reveng.xml click on Setup → Create New:
Then next, include just a table for testing creation and finish.
Return in Hibernate Code Generation Configurations and be sure that reveng.xml is compiled and then Run the Code Generation:
The generated files will be created in the package previously set:
Unfortunately the class generated is without @annotations: