Use the JPA console to write and run JPQL queries. It provides useful highlighting and completion for JPQL keywords, object and property names.
Make sure that persistence.xml contains the necessary information for the persistence unit to be able to run JPQL queries from the corresponding JPA console: the persistence provider, a list of persistent classes, and connection properties. For example, in case of EclipseLink and MySQL, it could be similar to the following:
Open the JPA console
In the Persistence tool window, right-click a persistence unit or entity and select Console. Alternatively, you can click in the toolbar or press Ctrl+Shift+F10.
The JPA console input pane opens as a separate tab where you can write and execute JPQL queries.
The JPA console toolbar contains the following buttons:
|Ctrl+Enter||Run the current query.|
Generate an SQL statement or a DDL script.
|Open the Database page of the Settings/Preferences dialog Ctrl+Alt+S.|
|Open the Parameters pane to see the parameters used in your queries and set their values.|
|Ctrl+Alt+E||Open the console history dialog that shows all the queries that you have run in this console.|
|Restore the original tool window layout after layout changes.|
|Select the source for properties.|
|Ctrl+F2||Terminate the current console session.|
|Ctrl+Shift+F4||Close the current console.|
Run JPQL queries
After you write a query, do one of the following:
Click in the toolbar.
When you execute a query with parameters, you will need to enter their values. Alternatively, click to open the Parameters pane.
IntelliJ IDEA stores all queries that you execute. Press Up to see the previously executed commands, starting from the most recent one. To see the full history, press .
Navigate to the declaration of a class or field
When composing a query, it is useful to look at the declaration of the corresponding class or field.
Hold Ctrl and click the relevant object or property.
Position the caret at the name of the relevant object or property and press Ctrl+B or selectfrom the main menu.
Run the JPA console with custom JVM options
The JPA console is a Java process. If necessary, you can start it with custom JVM options:
Create an Application run configuration
From the main menu, select Application., click , and select
In the VM options field, specify the options that you want to pass to the JVM when it starts. The rest of the run configuration settings don't matter and you don't need to specify them.
Click OK to save the run configuration.
When you open the JPA console, IntelliJ IDEA will display an additional VM and Env Configuration popup with the available run configurations. Select the one with the necessary JVM options or run with the default settings.