IntelliJ IDEA provides a dedicated JPA facet to support JPA framework. To enable JPA in a module, extend it with the JPA facet. If a new module is being created from scratch, it is possible to enable JPA support in the New Module wizard. If a module already exists, the JPA facet should be added in the Module Settings dialog.
Only one JPA facet is allowed per module.
Create a new Java module from scratch.
- On the technologies page of the wizard, click the Java EE Persistence check box.
- From the drop-down list, select the default JPA provider that will be used to generate mappings.
- If you want to import a database schema automatically, select the Import database schema check box.
- Configure a library for the new module. IntelliJ IDEA displays the list of missing archives, and suggests you to either use the libraries, already configured in project, or pick these archives from your file system, and then specify the library name and level.
- Click Finish. The resulting module contains persistence.xml file. Further you will need to populate it with persistent units, and map them to data sources.
- Open the Module Settings dialog box.
- With the desired module selected, click on the toolbar, and select JPA from the list of facets available for this module.
- Click the JPA facet node. The right pane of the dialog box shows the facet options.
- If you have not configured library for JPA in advance, the facet page displays
the list of missing libraries.
To resolve the problem, click the Fix button. In the Specify Libraries dialog box, specify whether you would like to use one of the existing libraries, or find the archive in the file system. You can control the target location where the archive will be placed, the library name, and the level on which the library will be created.
- Create descriptors. To do that, click ( or or or or ) in the Descriptors
section, and select the desired descriptor type:
- JPA configuration descriptor persistence.xml for defining the classes that should be persisted.
- JPA mapping descriptor orm.xml for specifying persistence using metadata rather than annotations.
In both cases, specify location where the descriptor files should be stored.
- Select the default JPA provider from the drop-down list.
- Click OK. The persistence.xml file is generated in the META-INF directory under the module root. Further you will have to populate it with persistent units, and map them to data sources.
- Configuring Module Dependencies and Libraries
- Creating Persistence Units
- Generating Persistence Mappings