Skip to content

How to configure Eclipse Enviroment for an Hibernate app with Maven

Notifications You must be signed in to change notification settings

thomascristofaro/Config-Hibernate-Eclipse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Configure Hibernate in Eclipse

Index

Prerequisites

MySQL Driver creation

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.

Project creation

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)

Hibernate dependencies

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"

Persistance file (hibernate.cfg.xml)

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":

Try code

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.

Database Reverse Engineering

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:

About

How to configure Eclipse Enviroment for an Hibernate app with Maven

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages