Ce contenu n'est pas disponible dans la langue sélectionnée.
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.shfile. Execute the
runh2.shfile:./runh2.sh
$ ./runh2.shCopy to Clipboard Copied! Toggle word wrap Toggle overflow
7.1. Creating a new JPA project Copier lienLien copié sur presse-papiers!
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
.jarfile for the Sakila database. - Click the Properties tab.
-
Add
jdbc:h2:tcp://localhost/./sakilato 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
sato 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 Copier lienLien copié sur presse-papiers!
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
.zipfile. - 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
.zipfile into. -
Navigate to the
/lib/required/directory. -
Select the
.jarfiles. - Click the button.
7.3. Generating entities Copier lienLien copié sur presse-papiers!
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 Copier lienLien copié sur presse-papiers!
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.xmlfile 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.xmlfile . - Click the button.
Your newly created hbm.xml file is now displayed in CodeReady Studio.
7.5. Creating a Hibernate configuration file Copier lienLien copié sur presse-papiers!
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.xmlfile. - 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 Copier lienLien copié sur presse-papiers!
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 button to configure a new Database connection.
Click the button to setup the Property file.
The Setup property file window appears.
Click the button.
The Create Hibernate Properties file (.properties) window appears.
- Select the parent directory.
-
Name your
.propertiesfile. - Click the button.
- Click the button setup the Configuration file.
Select the path to the target
.cfg.xmlfile.For more information on how to create a new
.cfg.xmlfile, see Section 7.5, “Creating a Hibernate configuration file”.- Click the button.
- Click the button.
7.7. Editing Hibernate project configurations Copier lienLien copié sur presse-papiers!
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.