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

  1. Download the h2 version of the Sakila database.
  2. Navigate to the directory that contains the runh2.sh file.
  3. 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

  1. Start CodeReady Studio.
  2. Press Ctrl+N.

    The New window appears.

  3. Enter JPA in the search field.
  4. Select JPA Project
  5. Click the Next button.

    The New JPA Project window appears.

  6. 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 (-).

  7. Click the down-arrow in the Target runtime field.
  8. Select the runtime server.
  9. Ensure that the JPA version is set to 2.1.
  10. Click the Next button.

    The Java window appears.

  11. Select the source folder.
  12. Click the Next button.

    The JPA Facet window appears.

  13. Click the down-arrow in the Platform field.
  14. Select Hibernate (JPA 2.1).
  15. 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”.

  16. Click Add connection.

    The New Connection Profile window appears.

  17. Enter Generic in the search field.
  18. Select Generic JDBC.
  19. Enter sakila in the Name field
  20. Click the Next button.

    The Specify a Driver and Connection Details window appears.

  21. Click the New Driver Definition icon.

    new driver definition crs

    Click the New Driver Definition icon.

    The New Driver Definition window appears.

  22. Select the Generic JDBC Driver.
  23. Click the JAR List tab.
  24. Click the Add JAR/Zip button.
  25. Select the .jar file for the Sakila database.
  26. Click the Properties tab.
  27. Add jdbc:h2:tcp://localhost/./sakila to the Connection URL field.
  28. Click the Driver Class field.
  29. Click the three dots icon at the end of the Driver Class field.

    The Available Classes from Jar List window appears.

  30. Select the Browse the Class option.

    The org.h2.Driver appears.

  31. Click the OK button.
  32. Enter sa to the User ID field.
  33. Click the OK button.
  34. Click the Finish button.
  35. Click the Finish button.

    The Open Associated Perspective? window appears.

  36. Click the Open Perspective 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

  1. Download Hibernate ORM from http://hibernate.org/orm/.
  2. Extract the files from the .zip file.
  3. Start CodeReady Studio.
  4. Click WindowPreferences.

    The Preferences window appears.

  5. Enter Libraries in the search field.
  6. Select User Libraries under Java.
  7. Click the New button.

    The New User Library window appears.

  8. Name your user library.
  9. Click the OK button.
  10. Select your new user library.
  11. Click the Add External JARs button.
  12. Locate the directory you extracted the Hibernate ORM .zip file into.
  13. Navigate to the /lib/required/ directory.
  14. Select the .jar files.
  15. Click the Apply and Close button.

7.3. Generating entities

The following section describes how to generate entities for your hibernate project in CodeReady Studio.

Procedure

  1. Start CodeReady Studio.
  2. Open Project Explorer.
  3. Right-click the target JPA projectJPA ToolsGenerate Tables from Entities.

    The Generate Tables from Entities window appears.

  4. Select the Use Console Configuration check box.
  5. Click the Finish 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

  1. Start CodeReady Studio.
  2. Press Ctr+N.

    The Show View window appears.

  3. Enter Hibernate in the search field.
  4. Select Hibernate XML Mapping file (hbm.xml).
  5. Click the Next button.

    The Create Hibernate XML Mapping file (hbm.xml) window appears.

  6. Click the Add Class button to add classes.
  7. Click the Add Package button to add packages.

    Alternatively, you can create an empty hbm.xml file by not selecting any packages or classes.

  8. Select the depth control check box to define the dependency depth used when choosing classes.
  9. Click the Next button.
  10. Select the parent directory.
  11. Name your hbm.xml file .
  12. Click the Finish 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

  1. Start CodeReady Studio.
  2. Press Ctr+N.

    The Show View window appears.

  3. Enter Hibernate in the search field.
  4. Select Hibernate Configuration file (cfg.xml).
  5. Click the Next button.

    The Create Hibernate Configuration file (cfg.xml) window appears.

  6. Select the parent directory.
  7. Click the Next button.

    The Hibernate Configuration file (cfg.xml) window appears.

  8. Name your cfg.xml file.
  9. Click the down-arrow in the Database dialect field.
  10. Select the relevant database.
  11. Click the down-arrow in the Driver class field.
  12. Select the relevant driver.
  13. Click the down-arrow in the Connection URL field.
  14. Select the relevant URL.
  15. Click the Finish 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

  1. Start CodeReady Studio.
  2. Press Ctr+N.

    The Show View window appears.

  3. Enter Hibernate in the search field.
  4. Select Hibernate Console Configuration.
  5. Click the Next button.

    The Create Hibernate Console Configuration window appears.

  6. Name your configuration file .
  7. Ensure that the Type is set to Core.
  8. Select the relevant Hibernate version.
  9. Click the Browse button to locate the target project.
  10. Click the New button to configure a new Database connection.
  11. Click the Setup button to setup the Property file.

    The Setup property file window appears.

  12. Click the Create new button.

    The Create Hibernate Properties file (.properties) window appears.

  13. Select the parent directory.
  14. Name your .properties file.
  15. Click the Finish button.
  16. Click the Setup button setup the Configuration file.
  17. 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”.

  18. Click the OK button.
  19. Click the Finish button.

7.7. Editing Hibernate project configurations

The following section describes how to edit configurations for the Hibernate project in CodeReady Studio.

Procedure

  1. Start CodeReady Studio.
  2. Press Ctr+N.

    The Show View window appears.

  3. Enter Hibernate in the search field.
  4. Select Hibernate Configuration.
  5. Click the OK button.

    The Hibernate Configurations view appears.

  6. Right-click the target projectEdit Configuration.

    The Edit Configuration window appears.

  7. Edit the scenery configurations.
  8. Click the Apply button.
  9. Click the OK button.