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 JPA project

The following section describes how to create a new JPA project in CodeReady Studio.

Prerequisites

  • The Sakila database server is started.
    To start the Sakila database:

    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

Procedure

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

    The Select a Wizard window appears.

    crs creating a jpa project
  3. Enter JPA in the search field.
  4. Select JPA Project.
  5. Click Next.

    The New JPA Project window appears.

    crs jpa project creation
  6. Name your project.
  7. Select the location for your project.
  8. Click the down-arrow in the Target runtime field to select the runtime server.
  9. Set the JPA version to 2.1.
  10. Click Next.

    The Java window appears.

    crs creating jpa project
  11. Select the source folder.
  12. Click Next.

    The JPA Facet window appears.

    crs jpa facet
  13. Click the down-arrow in the Platform field and select Hibernate (JPA 2.1).
  14. 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”.

  15. Click Add connection.

    The Connection Profile window appears.

    crs creating generic jdbc connection
  16. Enter Generic in the search field.
  17. Select Generic JDBC.
  18. Enter Sakila in the Name field.
  19. Click Next.

    The Specify a Driver and Connection Details window appears.

  20. Click the New Driver Definition icon.

    new driver definition crs

    The New Driver Definition window appears.

    crs specifying new driver definition
  21. Select the Generic JDBC Driver.
  22. Click the JAR List tab.

    crs selecting sakila database
  23. Click the Add JAR/Zip button.
  24. Select the .jar file for the Sakila database.
  25. Click the Properties tab.

    crs properties window
  26. Add jdbc:h2:tcp://localhost/./sakila to the Connection URL field.
  27. Click the Driver Class field.
  28. Click the three dots icon at the end of the Driver Class field.

    The Available Classes from Jar List window appears.

    crs class from jar list
  29. Select the Browse for Class option.
  30. Select org.h2.Driver.
  31. Click OK.
  32. Enter sa in the User ID field.

    crs propertie window filled
  33. Click OKFinishFinish.

Your newly created JPA 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

  1. Download Hibernate ORM.
  2. Extract the files.
  3. Start CodeReady Studio.
  4. Click WindowPreferences.

    crs preferences

    The Preferences window appears.

    crs preferences user libraries
  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 OK.
  10. Select your new user library.

    crs select user libraries
  11. Click the Add External JARs button.
  12. Select the directory you extracted the Hibernate ORM files into.
  13. Navigate to the /lib/required/ directory.
  14. Select a .jar file.
  15. Click Open.

    Your selected .jar file appears under your user library.

    crs user libraries added
  16. 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

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

    crs generate tables from entities

    The Generate Tables from Entities window appears.

    crs generating tables from entities
  4. Select the Use Console Configuration check box.
  5. 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

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

    The Select a wizard window appears.

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

    The Create Hibernate XML Mapping file window appears.

    crs hibernate xml mapping table creation
  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 Next.

    The New Hibernate XML Mapping files window appears.

    crs selecting parent directory for hibernate mapping file
  10. Select the parent directory.
  11. Name your .hbm.xml file .
  12. 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

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

    The Select a wizard window appears.

    crs creating hibernate config file
  3. Enter Hibernate in the search field.
  4. Select Hibernate Configuration file (cfg.xml).
  5. Click Next.

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

    crs selecting parent dir for hibernate config file
  6. Select the parent directory.
  7. Click Next.

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

    crs hibenate config file creation
  8. Click the down-arrow in the Database dialect field to select the database.
  9. Click the down-arrow in the Driver class field to select the driver.
  10. Click the down-arrow in the Connection URL field to select the URL.
  11. 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

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

    The Select a wizard window appears.

    crs creating a hibernate console config
  3. Enter Hibernate in the search field.
  4. Select Hibernate Console Configuration.
  5. Click Next.

    The Create Hibernate Console Configuration window appears.

    crs create hibernate console config
  6. Name your configuration file .
  7. Ensure that the Type is set to Core.
  8. Select the correct Hibernate version.
  9. Click Browse to locate your project.
  10. Click New to configure a new Database connection.

    The New Connection Profile window appears.

  11. Select the Database Connection or create a new one.
  12. Click Setup to set up the Property file.

    The Setup property file window appears.

  13. Click Create new.

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

    crs create hibernate properties file
  14. Select the parent directory.
  15. Name your .properties file.
  16. Click Finish.
  17. Click Setup to set up the Configuration file.
  18. Select the path to the target .cfg.xml file.

    The Setup configuration file window appears.

  19. Click Create new.

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

    crs selecting parent dir for hibernate config file
  20. Select the parent directory.
  21. Click Next.

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

    crs hibenate config file creation
  22. Click the down-arrow in the Database dialect field to select the database.
  23. Click the down-arrow in the Driver class field to select the driver.
  24. Click the down-arrow in the Connection URL field to select the URL.
  25. Click Finish.

    The Create Hibernate Console Configuration window appears.

    crs creation of console config
  26. Set the database connection to sakila.
  27. Click Finish.

7.7. Editing Hibernate project configurations

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

Procedure

  1. Start CodeReady Studio.
  2. Click WindowShow ViewOther.

    crs window show view other

    The Show View window appears.

    crs opening hibernate view
  3. Enter Hibernate in the search field.
  4. Select Hibernate Configurations.
  5. Click Open.

    The Hibernate Configurations view appears.

    crs editing hibernate config
  6. Right-click your projectEdit Configuration.

    The Edit Configuration window appears.

    crs edit config view
  7. Edit your configurations.
  8. Click Apply.
  9. 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

  1. Click WindowPreferences.

    The preference page opens.

    crs hibernate preferences page
  2. Enter Hibernate in the search field.
  3. Select Hibernate.

Check to enable or uncheck to disable the runtimes for your Hibernate project.