IntelliJ IDEA 2016.2 Help

Working with the Persistence tool window

See also, Persistence Tool Window.

Overview of the tool window

The Persistence tool window shows your JPA and Hibernate project items, and lets you create configuration files, <persistence-unit> and <session-factory> elements, persistent classes and fields (see Using the New command), navigate to related source code in the editor (F4), open consoles and entity-relationship diagrams, and more.


Opening the Persistence tool window

For the tool window to be available, there must be a JPA- or Hibernate-enabled module in your project, i.e. a module with a JPA or Hibernate facet. See Enabling JPA Support or Enabling Hibernate Support.

To open the tool window, do one of the following:

  • Select View | Tool Windows | Persistence
  • If the tool window bars are currently shown, click the Persistence button (normally located in the lower-left part of the workspace).
  • Point to show_tool_window_bars or show_hide_tool_window_bars in the lower-left corner of the workspace and select Persistence.

Generating managed entity classes and O/R mappings

You can generate managed entity classes and object/relational mappings for them by importing:

  • A database schema represented by a data source.
  • An EJB facet with an associated deployment descriptor file ejb-jar.xml. The facet should be available in the same project. When retrieving the information from ejb-jar.xml, only the <entity> elements are processed.
  • Only for JPA: a Hibernate object/relational mapping file (.hbm.xml). The file should be in the same project.

To perform the import:

  1. Right-click a module, persistence unit or session factory, point to Generate Persistence Mapping and select one of the following options:
    • By Database Schema
    • By Entity Beans
    • By Hibernate Mappings (this option is not available for Hibernate-enabled modules and session factories)
  2. In the dialog that opens, specify the source of import and the output options.

Example: Using a database as a source

  1. Right-click a module, persistence unit or session factory, point to Generate Persistence Mapping and select By Database Schema.
  2. In the Import Database Schema dialog that opens:
    • General Settings. Select the data source to be used as a source. Specify the destination package for your entity classes, and the prefix and the suffix for the class names.
    • Database Schema Mapping. Select the tables and columns to be mapped, edit the class and field names (the Map As column), and specify the field types (the Mapped Type column).
    • Generation Settings. Specify the target <persistence-unit> or <session-factory>, and select the necessary output options.

Using the New command

The New command lets you create XML configuration files, <persistence-unit> and <session-factory> elements, entity classes, embeddables, mapped superclasses, entity listeners and object-relational mappings, persistent fields in entity classes along with getter and setter method for them, etc.

The New command can be accessed by using the context or the File menu, or the keyboard shortcut Alt+Insert. The command options depend on which item is currently selected.

Example: Creating an entity class

  1. Right-click a persistence unit or session factory, point to New, and select Entity.
  2. In the New Entity dialog that opens, specify:
    • Create class: the name of the class.
    • Destination package: the package in which the class should be created.
    • Target destination directory: the destination source root folder (if there is more than one).
  3. For JPA, the Select Metadata Targets dialog is shown. Select the files in which you want to add the mapping info for the class:
    • Object/relational mapping XML files: the info will be added as an <entity> element within <entity-mappings>.
    • The class file itself (.java): the @Entity annotation will be added.

    IntelliJ IDEA will also add the info to persistence.xml either as a <class> or a <mapping-file> element.

    For Hibernate, the Select Metadata Targets dialog is not shown. The @Entity annotation is added to the class and the <mapping class=""> element is added to hibernate.cfg.xml.

Opening entity-relationship diagrams

To open an entity-relationship diagram, right-click a persistence unit, session factory, entity or field, and select ER Diagram from the context menu.

See also, Diagram Toolbar and Context Menu and General Techniques of Using Diagrams.

Associating persistence units and session factories with data sources

You can associate a <persistence-unit> or <session-factory> element with a data source. If you do so:

  • Your source code that references database tables will automatically be validated against the corresponding data source.
  • When using the JPA or Hibernate console, you won't need to additionally specify the database connection settings. The corresponding settings configured for the associated DB data source will automatically be used.

If the <persistence-unit> or <session-factory> elements are generated by importing a database schema, the association between the corresponding elements and the data source is set automatically.

To associate a persistence unit or session factory with a data source:

  1. Right-click a module, persistence unit, session factory or entity, and select Assign Data Sources.
  2. In the dialog that opens, click the necessary Data Source cell and select the data source from the list. (To remove an association with a data source, select <none>.)

Associating a session factory with a NamingStrategy implementation class (Hibernate)

  1. Right-click a module, session factory or entity, and select Assign Naming Strategies.
  2. In the dialog that opens, click the necessary Naming Strategy cell and select the implementation class from the list.

Starting a JPQL or an HQL console

Right-click a persitence unit or session factory, or any node within it, and select Console.

See also:

See Also

Last modified: 23 November 2016