Persistence top

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we'll have a look at some supporting tools for JPA. We'll be focusing on the plugins available for two of the most popular IDEs: IntelliJ IDEA and Eclipse.

2. JPA Support in IntelliJ IDEA and Eclipse

JPA is the most widely used specification for working with relational databases in Java applications. In fact, JPA defines all aspects of the implementation, from annotations to data processing rules. 

Usually, we don't work solely on JPA entities. In addition to pure ORM-related code, we may also need things like a database versioning system, SQL/JPQL/HQL queries optimization, integration with IoC containers, etc. Here is where the plugins become of great help. They can support database reverse engineering, schema generation, migrations scripts generation, or Spring Data JPA repositories scaffolding. 

When it comes to using JPA frameworks in apps development, it's usually done with the help of an IDE. This is because they provide us with a powerful set of tools to boost a developer’s productivity:  

  • Boilerplate code generation
  • Database reverse engineering
  • Data objects definition generation
  • Advanced code autocomplete for both Java and JPQL
  • JPA-specific code suggestions
  • etc.

Let’s have a look at the two most common IDEs — IntelliJ IDEA and Eclipse — and see what they provide in terms of JPA applications development support.

3. IntelliJ IDEA

IntelliJ IDEA comes in two versions: Community Edition (Free) and Ultimate (Paid). The Ultimate version has a bundled plugin that supports JPA. 

On the other hand, the Community Edition doesn't provide specialized support for working with JPA, Hibernate, Spring Data, etc.

However, IntelliJ has a wide marketplace with different plugins. Therefore, we can find support for almost any technology, and JPA is no exception. 

For IntelliJ IDEA Community users, we'll cover the JPA Buddy plugin to support JPA features. Nevertheless, it also can be a good addition to IntelliJ IDEA Ultimate.

3.1. JPA Plugin for IntelliJ Ultimate

This plugin provides a set of advanced features including:

  • ER-diagrams for JPA entities
  • Persistence tool window 
  • Database reverse engineering
  • JPA console to test JPQL statement and generate DDL scripts
  • JPA-specific code inspections and completion

In IntelliJ IDEA Ultimate we can use a dedicated tool window – “Persistence”. This will display the JPA entities' structure in the project.

In this view, we can create entity attributes and relations using visual tools:

The “Persistence” window also allows quick navigation between code and its related entities hierarchy using gutter icons in the code editor. 

We can also take advantage of the reverse engineering process.  It will generate JPA entities, associations, and proper annotations for us. Also, it creates the pesistence.xml file or updates it if one exists.

Apart from generic JPA support, it will provide extra help if we choose Hibernate as a JPA implementation. We can perform Hibernate-specific actions, such as (but not limited to):

  • Manage configuration and mapping files
  • Execute HQL queries in the HQL console

There is a detailed review showing the plugin in action available on Youtube.

3.2. JPA Buddy

JPA Buddy provides a set of visual editors to work with JPA entities and Spring Data JPA repositories. In addition, it supports both Java and Kotlin languages. 

Let's see the main features it provides:

  • Visual tools and code navigation
  • Hibernate bean validation support
  • Hibernate types and JPA Converters support
  • Database versioning tools support (Liquibase and Flyway)
  • JPA-specific code inspections
  • Kotlin support
  • Spring Data Repositories visual designer

We don’t need to remember all JPA annotations, Spring Data repository methods naming rules, or Liquibase tags. The plugin allows us to choose required items from a palette.

Consequently, it enables us to update their properties in a visual editor instead of writing code:

Another challenge that JPA Buddy addresses is database versioning. The plugin supports both Liquibase and Flyway tools and can generate XML or SQL migration scripts accordingly.

JPA Buddy generates the scripts by comparing the project database to the actual JPA entities defined in the codebase. After generation, we can review generated scripts and update them before saving them to the codebase.

In addition to this, the plugin introduces several smart inspections and quick fixes, such as:

  • ID field absence in an entity code
  • No-args constructor absence
  • Potential issues with Lombok usage (e.g. improper equals() & hashCode() definition) 

Apart from JPA support, the plugin can assist with creating Spring Data JPA repositories. It provides visual editors that can help us with proper repository method naming.

4. Eclipse

As we know, there is a big plugin ecosystem for Eclipse. Nevertheless, the Dali Tools plugin is the de-facto standard for JPA support in the Eclipse IDE.

Let's explore this plugin. Then, we'll see how to get some extra support if we use Hibernate in our project.

4.1. Dali Tools

Dali covers almost all areas of JPA development. Its functionality includes (but not limited to): 

  • ER-diagrams for JPA entities
  • Visual editors and navigators
  • DDL (SQL) generation based on JPA model
  • Database reverse engineering 

There are two main visual tools provided by the plugin. Context-dependent “JPA Details” editor allows us to work with different JPA objects which we can select in the “JPA Structure” navigator:

We can select and change:

  • Entity definition, including named queries (with JPQL code assistance)
  • Entity attributes and associations: one-to-many, many-to-one, etc.
  • The orm.xml file. We can edit default mappings and persistence information for the project

Also, a dedicated persistence.xml editor allows us to avoid memorizing all the parameters that can be used in this configuration file according to the JPA spec. We can edit everything from connection options to lock timeout or validation mode. 

The Dali Tools plugin can generate and execute DDL SQL for a specific database based on the JPA model. In addition, the execution phase is separated from generation, so we can review the generated SQL before applying it to the app’s DB. 

The database reverse engineering process settings allow us to change the following options in the entities code:

  • PK generation strategy
  • Field access policy
  • Association fetch policy
  • Collection properties class (List, Set,…)

4.2. JBoss Hibernate Tools

Let's look at an option we have if we need support specifically for Hibernate.  In the Eclipse plugin repository, we can find the Jboss Hibernate Tools plugin.

This plugin can be used independently or in conjunction with JPA Tools and provides:

  • Code generation tools, including reverse engineering and refactoring
  • Advanced Hibernate config and mapping files display and editing
  • HQL execution console and SQL preview
  • Data Model export

The reverse engineering tool allows to configure a lot of options, including table name masks, type mapping and you can even exclude some columns from a generated entity class:

Generated JPA entities can be represented as POJOs with *.hbm.xml mapping files or as annotated classes. Also, the plugin supports DAO object generation for every entity with CRUD methods. The DAO utilizes Hibernate-specific classes for database access.

The HQL execution console and criteria editor allow us to generate and execute HQL queries. Apart from executing an arbitrary HQL, the console provides SQL preview for the HQL statement. It could be useful for creating optimized SQL queries to avoid “n+1 select” and other ORM edge cases.

5. Conclusion

In this article, we've covered several tools for JPA support available in the most popular Java IDEs: IntelliJ IDEA and Eclipse.

We saw how these can help during development to write better applications and increase productivity. So, when choosing the best tool for the job, we should pay attention to not only  “pure JPA” features, but also to the integration with the IDE and the selected development stack. 

Persistence bottom
Get started with Spring Data JPA through the reference Learn Spring Data JPA course: >> CHECK OUT THE COURSE
Persistence footer banner
Comments are closed on this article!