此内容没有您所选择的语言版本。
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.
The following section describes how to:
- Create a new JPA project.
- Generate entities.
- Create a Hibernate mapping file.
- Create a Hibernate configuration file.
- Create a Hibernate console configuration file.
- Edit Hibernate project configurations.
Prerequisites
- 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
7.1. Creating a new JPA project
The following section describes how to create a new JPA project in CodeReady Studio.
Procedure
- Start CodeReady Studio.
Press Ctrl+N.
The New window appears.
- Enter JPA in the search field.
- Select JPA Project
Click the
button.The New JPA Project window appears.
Name your project.
Note that the name of the project cannot include spaces or special characters. The only special characters allowed are periods (
.
), underscores (_
), and dashes (-
).- Click the down-arrow in the Target runtime field.
- Select the runtime server.
- Ensure that the JPA version is set to 2.1.
Click the
button.The Java window appears.
- Select the source folder.
Click the
button.The JPA Facet window appears.
- Click the down-arrow in the Platform field.
- 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
.The New Connection Profile window appears.
- Enter Generic in the search field.
- Select Generic JDBC.
- Enter sakila in the Name field
Click the
button.The Specify a Driver and Connection Details window appears.
Click the
icon.Click the New Driver Definition icon.
The New Driver Definition window appears.
- Select the Generic JDBC Driver.
- Click the JAR List tab.
- Click the 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 the Class option.
The org.h2.Driver appears.
- Click the button.
-
Enter
sa
to the User ID field. - Click the button.
- Click the button.
Click the
button.The Open Associated Perspective? window appears.
- Click the button.
Your newly created JPA project is now listed in the Project Explorer.
7.2. Adding libraries
The following section describes how to add libraries to your Hibernate project in CodeReady Studio.
Procedure
- Download Hibernate ORM from http://hibernate.org/orm/.
-
Extract the files from the
.zip
file. - Start CodeReady Studio.
Click
. The Preferences window appears.
- Enter Libraries in the search field.
- Select User Libraries under Java.
Click the
button.The New User Library window appears.
- Name your user library.
- Click the button.
- Select your new user library.
- Click the button.
-
Locate the directory you extracted the Hibernate ORM
.zip
file into. -
Navigate to the
/lib/required/
directory. -
Select the
.jar
files. - Click the button.
7.3. Generating entities
The following section describes how to generate entities for your hibernate project in CodeReady Studio.
Procedure
- Start CodeReady Studio.
- Open Project Explorer.
Right-click the target
. The Generate Tables from Entities window appears.
- Select the Use Console Configuration check box.
- Click the button.
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 Ctr+N.
The Show View window appears.
- Enter Hibernate in the search field.
- Select Hibernate XML Mapping file (hbm.xml).
Click the
button.The Create Hibernate XML Mapping file (hbm.xml) window appears.
- Click the button to add classes.
Click the
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 the button.
- Select the parent directory.
-
Name your
hbm.xml
file . - Click the button.
Your newly created hbm.xml
file is now displayed in CodeReady Studio.
7.5. Creating a Hibernate configuration file
For reverse engineering, prototype queries, or to Hibernate Core usage, a hibernate.properties
or a hibernate.cfg.xml
file is required. CodeReady Studio provides a wizard to generate the hibernate.cfg.xml
.
The following section describes how to create a Hibernate mapping file in CodeReady Studio.
Procedure
- Start CodeReady Studio.
Press Ctr+N.
The Show View window appears.
- Enter Hibernate in the search field.
- Select Hibernate Configuration file (cfg.xml).
Click the
button.The Create Hibernate Configuration file (cfg.xml) window appears.
- Select the parent directory.
Click the
button.The Hibernate Configuration file (cfg.xml) window appears.
-
Name your
cfg.xml
file. - Click the down-arrow in the Database dialect field.
- Select the relevant database.
- Click the down-arrow in the Driver class field.
- Select the relevant driver.
- Click the down-arrow in the Connection URL field.
- Select the relevant URL.
- Click the button.
Your newly created cfg.xml
file is now displayed in CodeReady Studio.
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 Ctr+N.
The Show View window appears.
- Enter Hibernate in the search field.
- Select Hibernate Console Configuration.
Click the
button.The Create Hibernate Console Configuration window appears.
- Name your configuration file .
- Ensure that the Type is set to Core.
- Select the relevant Hibernate version.
- Click the button to locate the target project.
- Click the Database connection. button to configure a new
Click the Property file.
button to setup theThe Setup property file window appears.
Click the
button.The Create Hibernate Properties file (.properties) window appears.
- Select the parent directory.
-
Name your
.properties
file. - Click the button.
- Click the Configuration file. button setup the
Select the path to the target
.cfg.xml
file.For more information on how to create a new
.cfg.xml
file, see Section 7.5, “Creating a Hibernate configuration file”.- Click the button.
- Click the button.
7.7. Editing Hibernate project configurations
The following section describes how to edit configurations for the Hibernate project in CodeReady Studio.
Procedure
- Start CodeReady Studio.
Press Ctr+N.
The Show View window appears.
- Enter Hibernate in the search field.
- Select Hibernate Configuration.
Click the
button.The Hibernate Configurations view appears.
Right-click the target
. The Edit Configuration window appears.
- Edit the scenery configurations.
- Click the button.
- Click the button.