Chapter 7. Hibernate Tools basics in CodeReady Studio
Hibernate Tools is a collection of tools for projects related to Hibernate version 5 and earlier. The tools provide Eclipse plugins for reverse engineering, code generation, visualization, and interaction with Hibernate.
7.1. Creating a new Jakarta Persistence project
The following section describes how to create a new Jakarta Persistence project in CodeReady Studio.
Prerequisites
The Sakila database server is started.
To start the Sakila database:- Download the h2 version of the Sakila database.
-
Navigate to the directory that contains the
runh2.sh
file. Execute the
runh2.sh
file:$ ./runh2.sh
Procedure
- Start CodeReady Studio.
Press Ctrl+N.
The Select a Wizard window appears.
- Enter JPA in the search field.
- Select JPA Project.
Click Next.
The New JPA Project window appears.
- Name your project.
- Select the location for your project.
- Click the down-arrow in the Target runtime field to select the runtime server.
- Set the JPA version to 2.1.
Click Next.
The Java window appears.
- Select the source folder.
Click Next.
The JPA Facet window appears.
- Click the down-arrow in the Platform field and select Hibernate (JPA 2.1).
Add user libraries or set the JPA Implementation Type to Disable Library Configuration.
For more information on how to set up user libraries, see Section 7.2, “Adding libraries”.
Click Add connection.
The Connection Profile window appears.
- Enter Generic in the search field.
- Select Generic JDBC.
- Enter Sakila in the Name field.
Click Next.
The Specify a Driver and Connection Details window appears.
Click the New Driver Definition icon.
The New Driver Definition window appears.
- Select the Generic JDBC Driver.
Click the JAR List tab.
- Click the Add JAR/Zip button.
-
Select the
.jar
file for the Sakila database. Click the Properties tab.
-
Add
jdbc:h2:tcp://localhost/./sakila
to the Connection URL field. - Click the Driver Class field.
Click the three dots icon at the end of the Driver Class field.
The Available Classes from Jar List window appears.
- Select the Browse for Class option.
- Select org.h2.Driver.
- Click OK.
Enter
sa
in the User ID field.- Click OK → Finish → Finish.
Your newly created Jakarta Persistence project is now listed in the Project Explorer view.
7.2. Adding libraries
The following section describes how to add libraries to your Hibernate project in CodeReady Studio.
Procedure
- Download Hibernate ORM.
- Extract the files.
- Start CodeReady Studio.
Click Window → Preferences.
The Preferences window appears.
- Enter Libraries in the search field.
- Select User Libraries under Java.
Click the New button.
The New User Library window appears.
- Name your user library.
- Click OK.
Select your new user library.
- Click the Add External JARs button.
- Select the directory you extracted the Hibernate ORM files into.
-
Navigate to the
/lib/required/
directory. -
Select a
.jar
file. Click Open.
Your selected
.jar
file appears under your user library.- Click Apply and Close.
7.3. Generating tables from entities
The following section describes how to generate tables from entities for your Hibernate project in CodeReady Studio.
Procedure
- Start CodeReady Studio.
- Open Project Explorer.
Right-click your JPA project → JPA Tools → Generate Tables from Entities.
The Generate Tables from Entities window appears.
- Select the Use Console Configuration check box.
- Click Finish.
7.4. Creating a Hibernate mapping file
Hibernate mapping files specify how your objects relate to the database tables.
The following section describes how to create a Hibernate mapping file in CodeReady Studio.
Procedure
- Start CodeReady Studio.
Press Ctrl+N.
The Select a wizard window appears.
- Enter Hibernate in the search field.
- Select Hibernate XML Mapping file (hbm.xml).
Click Next.
The Create Hibernate XML Mapping file window appears.
- Click the Add Class button to add classes.
Click the Add Package button to add packages.
Alternatively, you can create an empty
.hbm.xml
file by not selecting any packages or classes.- Select the depth control check box to define the dependency depth used when choosing classes.
Click Next.
The New Hibernate XML Mapping files window appears.
- Select the parent directory.
-
Name your
.hbm.xml
file . - Click Finish.
7.5. Creating a Hibernate configuration file
For reverse engineering, prototype queries, or Hibernate Core usage, a hibernate.properties
or a hibernate.cfg.xml
file is required. CodeReady Studio provides a wizard to generate the configuration file hibernate.cfg.xml
.
The following section describes how to create a Hibernate configuration file in CodeReady Studio.
Procedure
- Start CodeReady Studio.
Press Ctrl+N.
The Select a wizard window appears.
- Enter Hibernate in the search field.
- Select Hibernate Configuration file (cfg.xml).
Click Next.
The Create Hibernate Configuration file (cfg.xml) window appears.
- Select the parent directory.
Click Next.
The Hibernate Configuration File (cfg.xml) window appears.
- Click the down-arrow in the Database dialect field to select the database.
- Click the down-arrow in the Driver class field to select the driver.
- Click the down-arrow in the Connection URL field to select the URL.
- Click Finish.
7.6. Creating a Hibernate console configuration file
A console configuration file describes how the Hibernate plugin configures Hibernate. It also describes the configuration files and classpaths needed to load the POJOs, JDBC drivers, and so on. It is required to make use of query prototyping, reverse engineering and code generation. You can have multiple console configurations per project, however, one configuration is sufficient.
The following section describes how to create a Hibernate console configuration file in CodeReady Studio.
Procedure
- Start CodeReady Studio.
Press Ctrl+N.
The Select a wizard window appears.
- Enter Hibernate in the search field.
- Select Hibernate Console Configuration.
Click Next.
The Create Hibernate Console Configuration window appears.
- Name your configuration file .
- Ensure that the Type is set to Core.
- Select the correct Hibernate version.
- Click Browse to locate your project.
Click New to configure a new Database connection.
The New Connection Profile window appears.
- Select the Database Connection or create a new one.
Click Setup to set up the Property file.
The Setup property file window appears.
Click Create new.
The Create Hibernate Properties file (.properties) window appears.
- Select the parent directory.
-
Name your
.properties
file. - Click Finish.
- Click Setup to set up the Configuration file.
Select the path to the target
.cfg.xml
file.The Setup configuration file window appears.
Click Create new.
The Create Hibernate Configuration file (cfg.xml) window appears.
- Select the parent directory.
Click Next.
The Hibernate Configuration File (cfg.xml) window appears.
- Click the down-arrow in the Database dialect field to select the database.
- Click the down-arrow in the Driver class field to select the driver.
- Click the down-arrow in the Connection URL field to select the URL.
Click Finish.
The Create Hibernate Console Configuration window appears.
-
Set the database connection to
sakila
. - Click Finish.
7.7. Editing Hibernate project configurations
The following section describes how to edit configurations for your Hibernate project in CodeReady Studio.
Procedure
- Start CodeReady Studio.
Click Window → Show View → Other.
The Show View window appears.
- Enter Hibernate in the search field.
- Select Hibernate Configurations.
Click Open.
The Hibernate Configurations view appears.
Right-click your project → Edit Configuration.
The Edit Configuration window appears.
- Edit your configurations.
- Click Apply.
- Click OK.
7.8. Setting preferences for Hibernate runtime implementations
You can enable or disable runtime implementations in the Hibernate preference page.
The following section describes how to configure your runtime preferences.
Procedure
Click Window → Preferences.
The preference page opens.
- Enter Hibernate in the search field.
- Select Hibernate.
Check to enable or uncheck to disable the runtimes for your Hibernate project.