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.
- Open Persistence tool window.
- Right-click the desired module with JPA facet, and choose
- 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
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.
- Open the persistence.xml file for editing.
- Start typing the persistence-unit tag, and see code completion in action:
- Type the name of the new persistence unit. Note that the corresponding persistence unit node is immediately created in the Persistence tool window.