JBoss.org Community Documentation

Chapter 3. Eclipse Plugins

3.1. Introduction
3.2. Creating a Hibernate Mapping File
3.3. Creating a Hibernate Configuration File
3.4. Creating a Hibernate Console Configuration
3.5. Reverse Engineering and Code Generation
3.5.1. Code Generation Launcher
3.5.2. Exporters
3.6. Hibernate Mapping and Configuration File Editor
3.6.1. Java property/class completion
3.6.2. Table/Column completion
3.6.3. Configuration property completion
3.7. Structured Hibernate Mapping and Configuration File Editor
3.8. Reveng.xml Editor
3.9. Hibernate Console Perspective
3.9.1. Viewing the entity structure
3.9.2. Prototyping Queries
3.9.3. Properties View
3.10. Enable debug logging in the plugins
3.10.1. Relevant Resources Links

This chapter will introduce you to the functionality that Hibernate Tools provide within Eclipse. That is a set of wizards and editors for simplifying the work with Hibernate.

The following features are available in the Hibernate Tools Eclipse plugins:

Mapping Editor: An editor for Hibernate XML mapping files, supporting auto-completion and syntax highlighting. It also supports semantic auto-completion for class names and property/field names, making it much more versatile than a normal XML editor.

Hibernate Console: The console is a new perspective in Eclipse. It provides an overview of your Hibernate Console configurations, were you also can get an interactive view of your persistent classes and their relationships. The console allows you to execute HQL queries against your database and browse the result directly in Eclipse.

Configuration Wizards and Code generation: A set of wizards are provided with the Hibernate Eclipse tools; you can use a wizard to quickly generate common Hibernate configuration (cfg.xml) files, and from these you can code generate a series of various artifacts, there is even support for completely reverse engineer an existing database schema and use the code generation to generate POJO source files and Hibernate mapping files.

Eclipse JDT integration: Hibernate Tools integrates into the Java code completion and build support of Java in Eclipse. This gives you codecompletion of HQL inside Java code plus Hibernate Tools will add problem markers if your queries are not valid against the console configuration associated with the project.

Hibernate mapping files are used to specify how your objects are related to database tables.

For creating a skeleton mapping file, i. e. any .hbm.xml , Hibernate Tools provide a basic wizard which you can bring up by navigating New > Hibernate XML mapping file.


At first you'll be asked to specify the location and the name for a new mapping file. On the next dialog you should type or browse the class to map.


Pressing finish creates the file and opens it in the structured hbm.xml editor.

If you start the wizard from the selected class, all values will be detected there automatically.


To be able to reverse engineer, prototype queries, and of course to simply use Hibernate Core a hibernate.properties or hibernate.cfg.xml file is needed. The Hibernate Tools provide a wizard for generating the hibernate.cfg.xml file if you do not already have such one.

Start the wizard by clicking New > Other (Ctrl+N) , then Hibernate > Hibernate Configuration File (cfg.xml) and press Next . After selecting the wanted location for the hibernate.cfg.xml file, you will see the following page:


Note

The contents in the combo boxes for the JDBC driver class and JDBC URL change automatically, depending on the Dialect and actual driver you have chosen.

Enter your configuration information in this dialog. Details about the configuration options can be found in Hibernate Reference Documentation.

Press Finish to create the configuration file, after optionally creating a Console configuration, the hibernate.cfg.xml will be automatically opened in an editor. The last option Create Console Configuration is enabled by default and when enabled, it will automatically use the hibernate.cfg.xml for the basis of a Console configuration.

A Console configuration describes how the Hibernate plugin should configure Hibernate and what configuration files, including which classpath are needed to load the POJO's, JDBC drivers etc. It is required to make usage of query prototyping, reverse engineering and code generation. You can have multiple named console configurations. Normally you would just need one per project, but more is definitely possible if your project requires this.

You create a console configuration by running the Console Configuration Wizard, shown in the following screenshot. The same wizard will also be used if you are coming from the hibernate.cfg.xml wizard and had enabled Create Console Configuration .


The dialog consists of three tabs, General for the basic/required settings, Classpath for classpath and Mappings for additional mappings. The two latter ones are normally not required if you specify a project and it has /hibernate.cfg.xml or /META-INF/persistence.xml in its project classpath.

The following table describes the available settings. The wizard can automatically detect default values for most of these if you started the wizard with the relevant java project or resource selected.



The fallowing table specifies the parameters of the Classpath tab of the wizard.



Parameters of the Mappings tab in the Hibernate Console Configuration wizard are explained below:


Clicking Finish creates the configuration and shows it in the Hibernate Configurations View.


A "click-and-generate" reverse engineering and code generation facility is available. This facility allows you to generate a range of artifacts based on database or an already existing Hibernate configuration, be that mapping files or annotated classes. Some of these are POJO Java source file, Hibernate .hbm.xml , hibernate.cfg.xml generation and schema documentation.

To start working with this process, start the Hibernate Code Generation which is available in the toolbar via the Hibernate icon or via the Run > Hibernate Code Generation menu item.

When you click on Open Hibernate Code Generation Dialog... the standard Eclipse launcher dialog will appear. In this dialog you can create, edit and delete named Hibernate code generation "launchers".



The first time you create a code generation launcher you should give it a meaningful name, otherwise the default prefix New_Generation will be used.

Tip:

The "At least one exporter option must be selected" is just a warning stating that for this launch to work you need to select an exporter on the Exporter tab. When an exporter has been selected the warning will disappear.

The dialog also have the standard tabs Refresh and Common that can be used to configure which directories should be automatically refreshed and various general settings launchers, such as saving them in a project for sharing the launcher within a team.

On the Main tab you see the following fields:

Table 3.4. Code generation "Main" tab fields

Field

Description

Console Configuration

The name of the console configuration which should be used when code generating

Output directory

Path to a directory where all output will be written by default. Be aware that existing files will be overwritten, so be sure to specify the correct directory.

Reverse engineer from JDBC Connection

If enabled, the tools will reverse engineer the database available via the connection information in the selected Hibernate Console Configuration and generate code based on the database schema. If not enabled, the code generation will just be based on the mappings already specified in the Hibernate Console configuration.

Package

The package name here is used as the default package name for any entities found when reverse engineering

reveng.xml

Path to a reveng.xml file. A reveng.xml file allows you to control certain aspects of the reverse engineering. e.g. how jdbc types are mapped to hibernate types and especially important which tables are included/excluded from the process. Clicking "setup" allows you to select an existing reveng.xml file or create a new one. See more details about the reveng.xml file in Chapter 5, Controlling reverse engineering.

reveng. strategy

If reveng.xml does not provide enough customization you can provide your own implementation of an ReverseEngineeringStrategy. The class needs to be in the classpath of the Console Configuration, otherwise you will get class not found exceptions. See Section 5.3, “Custom strategy” for details and an example of a custom strategy.

Generate basic typed composite ids

A table that has a multi-colum primary key a <composite-id> mapping will always be created. If this option is enabled and there are matching foreign-keys each key column is still considered a 'basic' scalar (string, long, etc.) instead of a reference to an entity. If you disable this option a <key-many-to-one> instead. Note: a <many-to-one> property is still created, but is simply marked as non-updatable and non-insertable.

Detect optimistic lock columns

Automatically detect optimistic lock columns. Controllable via reveng. strategy; the current default is to use columns named VERSION or TIMESTAMP.

Detect many-to-many tables

Automatically detect many-to-many tables. Controllable via reveng. strategy.

Use custom templates

If enabled, the Template directory will be searched first when looking up the templates, allowing you to redefine how the individual templates process the hibernate mapping model.

Template directory

A path to a directory with custom templates


The Exporters tab is used to specify which type of code that should be generated. Each selection represents an Exporter that is responsible for generating the code, hence the name.


The following table describes in short the various exporters. Remember you can add/remove any Exporters depending on your needs.


Each Exporter listens to certain properties and these can be setup in the Properties section where you can add/remove predefined or customer properties for each of the exporters. The following table lists the time of writing predefined properties:


The Hibernate Mapping File editor provides XML editing functionality for the hbm.xml and cfg.xml files. The editor is based on the Eclipse WTP tools and extends its functionality to provide Hibernate specific code completion.


The structured editor represents the file in the tree form. It also allows to modify the structure of the file and its elements with the help of tables provided on the right-hand area.

To open any mapping file in the editor, choose Open With > Hibernate 3.0 XML Editor option from the context menu of the file. The editor should look as follows:


For the configuration file you should choose Open With > Hibernate Configuration 3.0 XML Editoroption.


A reveng.xml file is used to customize and control how reverse engineering is performed by the tools. The plugins provide an editor to ease the editing of this file and hence used to configure the reverse engineering process.

The editor is intended to allow easy definition of type mappings, table include/excludes and specific override settings for columns, e.g. define an explicit name for a column when the default naming rules are not applicable.

The editor is activated as soon as an .reveng.xml file is opened. To get an initial reveng.xml file the Reverse Engineering File Wizard can be started via Ctrl+N and Hibernate > Hibernate Reverse Engineering File (reveng.xml) then.


Or you can get it via the Code Generation Launcher by checking the proper section in the Main tab of the Hibernate Code Generation Wizard.

The following screenshot shows the Overview page where the wanted console configuration is selected (auto-detected if Hibernate 3 support is enabled for the project)


The Table Filter page allows you to specify which tables to include and exclude. Pressing Refresh shows the tables from the database that have not yet been excluded.


The Type Mappings page is used for specifying type mappings from JBDC types to any Hibernate type (including usertypes) if the default rules are not applicable. Here again to see the database tables press Refresh button underneath. More about type mappings you can find further in the Type Mappings section.


The Table and Columns page allows you to explicit set e.g. which hibernatetype and propertyname that should be used in the reverse engineered model. For more details on how to configure the tables while reverse engineering read the Specific table configuration section.


Now that you have configured all necessary parts, you can learn how to work with Hibernate Console Perspective.

The Hibernate Console Perspective combines a set of views which allow you to see the structure of your mapped entities/classes, edit HQL queries, execute the queries, and see the results. To use this perspective you need to create a Console configuration.

To view your new configuration and entity/class structure, switch to Hibernate Configurations View. Expanding the tree allows you to browse the class/entity structure and see the relationships.


The Console Configuration does not dynamically adjust to changes done in mappings and java code. To reload the configuration select the configuration and click the Reload button in the view toolbar or in the context menu.

Besides, it's possible to open source and mapping files for objects showed in Hibernate Configurations View. Just bring up the context menu for a necessary object and select Open Source File to see appropriate Java class or Open Mapping File to open a proper .hbm.xml.


Queries can be prototyped by entering them in the HQL or Criteria Editor. The query editors are opened by right-clicking the Console Configuration and selecting either HQL Editor or Hibernate Criteria Editor. The editors automatically detect the chosen configuration.

If the menu item is disabled then you need at first to create a Session Factory. That is done by simply expanding the Session Factory node.

Not to enter manually simple queries like 'from Customer' or like on the figure below, you can do it with the editors help by brining up the context menu for a necessary item and choosing HQL Editor or Hibernate Criteria Editor .


Executing the query is done by clicking the green run button in the toolbar or pressing Ctrl+Enter .

Errors during creation of the Session Factory or running the queries (e.g. if your configuration or query is incorrect) will be shown in a message dialog or inclined in the view that detected the error, you may get more information about the error in the Error Log View on the right pane.

Results of a query will be shown in the Hibernate Query Result View and details of possible errors (syntax errors, database errors, etc.) can be seen in the Error Log View.

Note

HQL queries are executed by default using list() thus without any limit of the size of the output the query could return a large result set. You might run out of memory. To avoid this you can put a value in the Max results field to reduce the number of elements returned.

It is possible to configure the eclipse plugin to route all logging made by the plugins and hibernate code it self to the Error Log View in Eclipse.

This is done by editing the hibernate-log4j.properties in org.hibernate.eclipse/ directory/jar . This file includes a default configuration that only logs WARN and above to a set of custom appenders (PluginFileAppender and PluginLogAppender). You can change these settings to be as verbose or silent as you please - see Hibernate Documentation for interesting categories and Log4j documentation.