此内容没有您所选择的语言版本。

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

You need to start the Sakila database server before you can create a new JPA project in CodeReady Studio.

  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
    Copy to Clipboard Toggle word wrap

Procedure

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

    The Select a Wizard window appears.

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

    The New JPA Project window appears.

  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.

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

    The JPA Facet window appears.

  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.

  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.

    The New Driver Definition window appears.

  21. Select the Generic JDBC Driver.
  22. Click the JAR List tab.

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

  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.

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

  33. Click OK Finish Finish.

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 Window Preferences.

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

  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.

  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 project JPA Tools Generate Tables from Entities.

    The Generate Tables from Entities window appears.

  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.

  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.

  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.

  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.

  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.

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

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

  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.

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

    The Create Hibernate Console Configuration window appears.

  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.

  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.

  20. Select the parent directory.
  21. Click Next.

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

  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.

  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 Window Show View Other.

    The Show View window appears.

  3. Enter Hibernate in the search field.
  4. Select Hibernate Configurations.
  5. Click Open.

    The Hibernate Configurations view appears.

  6. Right-click your project Edit Configuration.

    The Edit Configuration window appears.

  7. Edit your configurations.
  8. Click Apply.
  9. Click OK.
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat