IntelliJ IDEA 2016.1 Help

Creating Persistence Units

When JPA support for a module is enabled, persistence.xml file is created in the META-INF directory under the module root. To persist Java objects to a data source, you have to define persistence units.

IntelliJ IDEA enables you to populate persistence.xml file with persistence units, using the context menu, or by editing the persistence.xml file.

The editor is aware of JPA specification and provides extensive coding assistance, which includes syntax and error highlighting, completion of tags, attributes and values, as well as intention actions and quick fixes.

This section describes how to create persistence units using the context menu or the editor.

To create a persistence unit using the context menu

  1. Open Persistence tool window.
  2. Right-click the desired module with JPA facet, and choose New | Persistence Unit on the context menu:
    javaeeNewPersistenceUnit.png
  3. In the New Persistent Unit dialog box, specify the name of the new persistent unit, or accept default. A new persistence unit node is added to the module, and created in the Persistence.xml file.

    By default, according to the JPA specification, a persistence unit in Java EE environment includes all the available annotated entities, which are displayed in the Persistence tool window. If you want a particular persistence unit to contain the entities that are unique to this unit only, in the editor add the exclude-unlisted-classes element with the value true to the source code of the persistence unit.

To create a persistence unit in the editor

  1. Open the persistence.xml file for editing.
  2. Start typing the persistence-unit tag, and see code completion in action:
    jpaNewPersistenceUnit.png
  3. Type the name of the new persistence unit. Note that the corresponding persistence unit node is immediately created in the Persistence tool window.

Populate your persistence unit with elements, create relationships or get schema information from a data source.

See Also

Last modified: 13 July 2016