IntelliJ IDEA 2017.1 Help

Working with the JPA console

This feature is supported in the Ultimate edition only.

Use the JPA console to write and run JPQL queries, analyze the query results, and also to perform other, associated tasks.

See also, JPA Console Tool Window.

Prerequisite

For the JPA console to be fully functional, you should associate your persistence unit with the corresponding data source, see Associating persistence units and session factories with data sources.

Opening the JPA console

  1. Open the Persistence tool window (e.g. View | Tool Windows | Persistence).
  2. Expand the JPA facet node.
  3. Select the persistence unit for which you want to open the console or any node within that persistence unit.
  4. Do one of the following:
    • Click /help/img/idea/2017.1/ConsoleOnTitleBar.png on the title bar.
    • Select Console from the context menu.
    • Press Ctrl+Shift+F10.
    /help/img/idea/2017.1/JPAConsoleOpen.png
  5. If asked to choose the console, select JPA Console.

As a result, the JPA Console tool window opens and the input pane is shown. This is where you compose your JPQL queries.

/help/img/idea/2017.1/JPAConsoleInput.png

When you run your first query (/help/img/idea/2017.1/run.png), the output pane opens above the input pane. Basically, this is the log of operations performed in the console.

If your query retrieves data (e.g. select), also the Result pane opens showing the retrieved data in table format.

/help/img/idea/2017.1/JPAConsoleInputOutputResult.png

Additionally, you can open the Parameters pane (/help/img/idea/2017.1/property_yellow.png) to manage parameters in your queries.

/help/img/idea/2017.1/JPAConsoleInputOutputResultParams.png

Running the console with custom JVM options

The JPA console is a Java process. If necessary, you can start it with custom JVM options:

  1. Create an Application run configuration: Run | Edit Configurations | /help/img/idea/2017.1/new.png | Application.

    In the VM options field, specify the options that you want to pass to the JVM at its start. The rest of the run configuration settings don't matter and you don't need to specify them.

  2. When starting the console, IntelliJ IDEA will now display an additional Configurations popup with the following options:
    • <default>. This option corresponds to an ordinary way of starting the console.
    • <YourRunConfigurationName>. Select this option to start the console with the VM options you have specified.

Viewing and modifying console settings

Before actually starting to use a console, you may want to take a look at the console settings and adjust them to your needs.

  • To access these settings, click /help/img/idea/2017.1/settings.png on the toolbar of the JPA Console tool window. (Alternatively, Ctrl+Alt+S | Tools | Database.)

As a result, the Database page of the Settings / Preferences dialog will open. The settings for the JPA console are on the following pages:

Composing JPQL queries

When composing your queries in the input pane, use auto-completion and highlighting of JPQL keywords, and object and property names.

/help/img/idea/2017.1/JPAConsoleCompletion.png

See also, Navigating to the declaration of a class or field.

Navigating to the declaration of a class or field

When composing a query, it's sometimes useful to take a look at the declaration of a class or field for an object or property referenced in the input pane. To navigate to the corresponding declaration, do one of the following:

  • Place the cursor within the name of the object or property of interest. Then use Ctrl+B. (Alternatively, you can use Navigate | Declaration from the main menu.)
  • Press and hold the Ctrl key, and point to the name of interest. When the text turns into a hyperlink, click the hyperlink.
    /help/img/idea/2017.1/JPAConsoleNavToDecl.png

As a result, the necessary source file opens in the editor and the cursor is placed within the declaration of the corresponding class or the getter method for the corresponding field.

Running a query

To run the current query, do one of the following:

  • Click /help/img/idea/2017.1/run.png on the toolbar.
  • Press Ctrl+Enter.

Running parameterized queries

Your queries can contain parameters, however, by the time you run such queries the values of the parameters must be specified. There are the following ways of specifying the parameter values:

  • Click /help/img/idea/2017.1/run.png on the toolbar or press Ctrl+Enter to run the query. In the dialog that opens, specify the parameter values and click OK.
    /help/img/idea/2017.1/JPAConsoleParamsDialog.png

    ( To start editing a value, switch to the corresponding table cell and start typing. To indicate that you have finished editing a value, press Enter or switch to a different cell. To quit the editing mode and restore an initial value, press Escape. )

  • Alternatively, you can open the Parameters pane (/help/img/idea/2017.1/property_yellow.png on the toolbar) and specify the corresponding values there. (The values are edited in the same way as in the corresponding dialog.) Then run the query (/help/img/idea/2017.1/run.png on the toolbar or Ctrl+Enter).
    /help/img/idea/2017.1/JPAConsoleParamsPane.png

Parameter values can be specified just as text or numbers, or as Groovy expressions that contain object references and method calls. For example, the value for the date parameter in the query

SELECT o FROM Order o WHERE o.date > :date

could be specified as

new java.sql.Date(System.currentTimeMillis() - 24*3600*1000)

Running auto-memorized queries

As you run JPQL queries in the console, IntelliJ IDEA memorizes them. So, at a later time, you can view the queries you have already run and, if necessary, run them again.

To open the dialog where the auto-memorized queries are shown (the History dialog), do one of the following:

  • Click /help/img/idea/2017.1/consoleHistory.png on the toolbar.
  • Press Ctrl+Alt+E.

There are two panes in the History dialog. The left-hand pane shows the list of the queries that you have run. For "long" queries, only their beginnings are shown. When you select a query in this pane, the overall query is shown in the pane to the right.

You can filter the information: just start typing. As a result, only the queries that contain the typed text will be shown.

You can copy the queries from the History dialog into the input pane of the console. To copy a query, do one of the following:

  • Double-click the query to be copied.
  • Select the query of interest and press Enter.
  • Select the query and click OK.

(Once the query is in the input pane, you can run it straight away.)

You can delete unnecessary memorized queries. To delete a query, select the query in the History dialog and press Delete.

Terminating query execution

To terminate execution of the current query, do one of the following:

  • Click /help/img/idea/2017.1/stop.gif on the toolbar.
  • Press Ctrl+F2.

Generating SQL statements and DDL SQL scripts

You can generate SQL statements for your JPQL queries and DDL SQL scripts for your persistence unit:

  • To generate an SQL equivalent of the current query, do one of the following:
    • Click /help/img/idea/2017.1/icon_sqlCopyQuery.png on the toolbar.
    • Press Ctrl+Shift+Enter.
  • To generate DDL SQL statements (CREATE TABLE, ALTER TABLE and DROP TABLE) for all the objects (classes) associated with the corresponding persistence unit, do one of the following:
    • Click /help/img/idea/2017.1/DDLCopy.png on the toolbar.
    • Press Ctrl+Shift+Alt+Enter.

The generated SQL statements are shown in the output pane.

/help/img/idea/2017.1/JPAConsoleDDL.png

Hiding or showing the toolbar

To hide or show the toolbar of the Result pane:

  • Click /help/img/idea/2017.1/viewMode.png on the title bar of the JPA Console tool window and click Show Toolbar.

Pinning the Result tab

If one and the same tab is used to show your query results, and you get the result that you want to keep, you can pin the tab to the tool window. To do that:

  • Right-click the tab and select Pin Tab.
    /help/img/idea/2017.1/JPAConsoleResultPinTab.png

See also, Show query results in new tab.

Switching between subsets of rows

If only a subset of the rows that satisfy the query is currently shown, to switch between the subsets, use:

  • /help/img/idea/2017.1/dataSourceTableEditorFirstPage.png First Page
  • /help/img/idea/2017.1/dataSourceTableEditorPreviousPage.png Previous Page (Ctrl+Alt+Up)
  • /help/img/idea/2017.1/dataSourceTableEditorNextPage.png Next Page (Ctrl+Alt+Down)
  • /help/img/idea/2017.1/dataSourceTableEditorLastPage.png Last Page

See also, Making all rows visible simultaneously.

Making all rows visible simultaneously

If you want all the rows that satisfy the query to be shown simultaneously:

  1. Click /help/img/idea/2017.1/settings.png on the toolbar of the JPA Console tool window.
  2. Switch to the Database | Data Views page, specify 0 in the Result set page size field, and click OK.
  3. Click /help/img/idea/2017.1/refresh.png or press Ctrl+F5 to refresh the table view.

See also, Updating the table view and Result set page size.

Navigating to a specified row

To switch to a row with a specified number:

  1. Do one of the following:
    • Press Ctrl+G.
    • Right-click the table and select Go To | Row from the context menu.
    • Select Navigate | Row from the main menu.
  2. In the dialog that opens, specify the row number and click OK.

Sorting data

You can sort table data by any of the columns by clicking the cells in the header row.

Each cell in this row has a sorting marker in the right-hand part and, initially, a cell may look something like this: /help/img/idea/2017.1/TableEditorHeaderCellUnsorted.png. The sorting marker in this case indicates that the data is not sorted by this column.

If you click the cell once, the data is sorted by the corresponding column in the ascending order. This is indicated by the sorting marker appearance: /help/img/idea/2017.1/TableEditorHeaderCellSortAsc.png. The number to the right of the marker (1 on the picture) is the sorting level. (You can sort by more than one column. In such cases, different columns will have different sorting levels.)

When you click the cell for the second time, the data is sorted in the descending order. Here is how the sorting marker indicates this order: /help/img/idea/2017.1/TableEditorHeaderCellSortDesc.png.

Finally, when you click the cell for the third time, the initial state is resorted. That is, sorting by the corresponding column is canceled: /help/img/idea/2017.1/TableEditorHeaderCellUnsorted.png.

See also, Restoring the initial table view and Using the Structure view to sort data, and hide and show columns.

Reordering columns

To reorder columns, use drag-and-drop for the corresponding cells in the header row.

/help/img/idea/2017.1/TableEditorReorderColumns.png

See also, Restoring the initial table view.

Hiding and showing columns

To hide a column, right-click the corresponding header cell and select Hide column.

To show a hidden column:

  1. Do one of the following:
    • Right-click any of the cells in the header row and select Column List.
    • Press Ctrl+F12.

    In the list that appears, the names of hidden columns are shown struck through.

    /help/img/idea/2017.1/TableResultPaneHideColumn.png
  2. Select (highlight) the column name of interest and press Space.
  3. Press Enter or Escape to close the list.

See also, Restoring the initial table view and Using the Structure view to sort data, and hide and show columns.

Restoring the initial table view

Click /help/img/idea/2017.1/ResetColumnOrderAndVisibility.png on the toolbar to restore the initial table view after reordering or hiding the columns, or sorting the data. As a result, the data, generally, becomes unsorted, the columns appear in the order they are defined in the corresponding query, and all the columns are shown.

Using the Structure view to sort data, and hide and show columns

When working with the Result pane, the table structure view is available as the corresponding popup.

The structure view shows the list of all the columns and lets you sort the data as well as hide and show the columns.

To open the structure popup, do one of the following:

  • Right-click a cell in the table header row and select Column List.
  • Press Ctrl+F12.

In the popup, select the column of interest and do one of the following:

  • To sort the data by this column in the ascending order, press Shift+Alt+Up.
  • To sort the data in the descending order, press Shift+Alt+Down.
  • To cancel sorting by this column, press Ctrl+Shift+Alt+Backspace.
  • To hide the column (or show a hidden column), press Space. (The names of hidden columns are shown struck through.)
    /help/img/idea/2017.1/TableResultPaneHideColumn.png

The shortcuts for sorting table data (Shift+Alt+Up, Shift+Alt+Down and Ctrl+Shift+Alt+Backspace) can be used in the Result pane without opening the structure view.

See also, Sorting data, Hiding and showing columns and Restoring the initial table view.

Copying table data to the clipboard or saving them in a file

When copying table data to the clipboard or saving them in a file, the data are converted into one of the available output formats. This can be SQL INSERT or UPDATE statements, TSV or CSV, an HTML table or JSON data. See Specifying data output format and options.

To copy or save the data, use:

  • Copy (available in the Edit and the context menu, the keyboard equivalent is Ctrl+C). This command copies the data for the selected cells onto the clipboard.
  • Dump Data | To Clipboard (available in the context menu and can also be accessed by means of /help/img/idea/2017.1/downloadArtifacts.png on the toolbar). This command copies the data for the whole table onto the clipboard.
  • Dump Data | To File (available in the context menu and can also be accessed by means of /help/img/idea/2017.1/downloadArtifacts.png on the toolbar). This command saves the data for the whole table in a file. Before actually saving the data, the dialog is shown which lets you select the output format and see how your data will look in a file.

Specifying data output format and options

To specify the output format and options for the Copy and Dump Data commands (see Copying table data to the clipboard or saving them in a file), do one of the following:

  • Click /help/img/idea/2017.1/DataExtractorButton.png on the toolbar.
  • Right-click the table and point to Data Extractor: <current_format>.

In the menu that opens, the output formats are in the upper part: SQL Inserts, SQL Updates, etc. (The options that look like file names are also the output formats or, to be more exact, the scripts that implement corresponding data converters.)

The output option are:

  • Allow Transposition. This option affects only delimiter-separated values formats (TSV, CSV). If the table is shown transposed and you are copying selected cells or rows to the clipboard (e.g. Ctrl+C), the selection is copied transposed (as shown) if the option is on and non-transposed (as in the original table) otherwise.
  • Skip Generated Columns (SQL). This is the option for SQL INSERTs and UPDATEs. When on, auto-increment fields are not included.
  • Add Table Definition (SQL). This is also the option for SQL INSERTs and UPDATEs. When on, the table definition (CREATE TABLE) is added.

Additionally:

  • Configure CSV Formats. This command opens the CSV Formats dialog that lets you manage your delimiter-separated values formats (e.g. CSV, TSV).
  • Go to Scripts Directory. This command lets you switch to the directory where the scripts that convert table data into various output formats are stored.

Saving a LOB in a file

If a cell contains a binary large object (a.k.a. BLOB or LOB), you can save such a LOB in a file.

  1. Right-click the cell that contains the LOB of interest and select Save LOB To File.
  2. In the dialog that opens, specify the name and location of the destination file and click OK.

Updating the table view

To refresh the table view, do one of the following:

  • Click /help/img/idea/2017.1/refresh.png on the toolbar.
  • Right-click the table and select Reload Page from the context menu.
  • Press Ctrl+F5.

Use this function to:

  • Synchronize the data shown with the actual contents of the database.
  • Apply the Result set page size setting after its change.

Viewing the query

To see the query that was used to generate the table:

  • Click View Query on the toolbar.

    If necessary, you can select the query text and copy it to the clipboard (Ctrl+C).

To close the pane where the query is shown, press Escape.

Closing a console

To close a console, do one of the following:

  • Click /help/img/idea/2017.1/close.png on the toolbar.
  • Press Ctrl+Shift+F4.

See Also

Last modified: 18 July 2017