Partner – JPA Buddy – NPI (tag=JPA/Hibernate)
announcement - icon

The right tools can and will save a lot of time. As long as you are using Hibernate and IntelliJ IDEA you can boost your coding speed and quality with JPA Buddy. It will help in a lot of the day-to-day work:

  • Creating JPA entities that follow best practices for efficient mapping
  • Creating DTOs from entities and MapStruct mappers using convenient visual tools
  • Generating entities from the existing database or Swagger-generated POJOs
  • Visually composing methods for Spring Data JPA repositories
  • Generating differential SQL to update your schema in accordance with your changes in entities
  • Autogenerating Flyway migrations and Liquibase changelogs comparing entities with the database or two databases
  • … and a lot more

Simply put, you'll learn and use the best practices of Hibernate and surrounding technology and become a lot more!

Definitely visit the JPA Buddy site to see its features in action closer.

Persistence top

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

>> CHECK OUT THE COURSE

1. Overview

JPA Buddy is a widely used IntelliJ IDEA plugin targeted to both new and experienced developers who work with JPA data model and related technologies like Spring Data JPA, DB versioning tools (Flyway, Liquibase), MapStruct, and some others. The plugin provides visual designers, code generation, and additional inspections that should simplify the development and improve the code according to the best practices for JPA.

The plugin works under both Community and Ultimate versions of IntelliJ IDEA and uses the “freemium” model. Most features are available for free, and we'll need to buy a subscription to access the paid ones.

In this tutorial, we'll look at the plugin's main features and see how we can use them during the application development cycle. As an example, we're going to use the popular reference application – “Spring PetClinic“.

2. Getting Started

We can activate JPA Buddy automatically when adding a JPA dependency to the application source code. The plugin is in the “Recommended InteiilJ Plugins” list, so even if we don't have it installed, IDEA will suggest we do this. We can also install the plugin from IntelliJ's marketplace:

JpaBuddy Notification Window

JPA Buddy functionality may vary depending on the libraries attached to the application. E.g., we won't see menus for DB versioning scripts generation if we have neither Liquibase nor Flyway attached.

3. Working with JPA Entities

Usually, application development starts with the data model. JPA Buddy provides a visual designer and component palette that allows us to create an entity and add basic attributes and associations to it. We can invoke the corresponding action from a context menu or the JPA Structure tool window:

Screenshot-2022-11-15-at-12.08.36

 

If we need to add or edit an entity attribute, we can use the JPA palette and inspector. To add an attribute, we need to double-click on it or perform drag-and-drop. The visual editor allows us to define the attribute properties:

Attribute visual editor

To edit an entity's attribute, we can use the JPA inspector. According to JPA specs, the inspector allows us to view and edit almost all options for an entity's attribute. The editor works both ways: when we update an attribute's property, the code is updated. And vice versa, when the entity's code is updated, all changes are reflected in the inspector.

3.1. Lombok Support

According to JPA Specs, entities should have getters and setters for attributes, and this is often treated as “boilerplate” code. Lombok is a popular library that allows us to avoid writing boilerplate and replace it with a couple of annotations. Many developers use Lombok in the JPA entities definition, so JPA Buddy supports it fully. We can edit Lombok-related properties in the JPA Inspector:

Lombok support in inspector

The plugin functionality is not limited to inspection and editing. The important part of the support is inspections. Some Lombok annotations might cause performance issues or unexpected errors when used with JPA. For example, using @ToString annotation might cause LazyInitException because it uses all attributes for an object string representation, including lazy ones.

JPA Buddy shows warnings for such cases and provides quick fixes, and it helps us to avoid bugs in the JPA code:

Lombok code inspection warning

4. DB Migration with Liquibase and Flyway

To create a database from the JPA data model, we usually use DB versioning tools; Liquibase and Flyway are two big players in this area. The most error-prone part of the DB migration process is writing the migration script based on the JPA entity's changes. JPA Buddy provides schema differential script generation for both Liquibase and Flyway. The plugin can compare the existing DB schema to the JPA model or even two schemas:

Migration scripts preview window

In addition to this, JPA Buddy provides scripts autocompletion and smart preview. In this mode, the plugin analyzes the generated scripts and shows a warning if changes can cause failures in the target data source on update:

Warning for a potentially dangerous code

5. Spring Data JPA Support

When the data model is created, the next step is the implementation of the data access repositories. Spring Data JPA is probably the most popular framework for this. JPA Buddy allows us to generate new repositories based on entity definitions. In the repository code, we can use the JPA palette to create various derived methods and queries in a repository code using visual tools. JPA Buddy can generate code to return DTOs and Spring Data Projections from a repository method.

Screenshot-2022-11-15-at-13.27.17

 

 

One more useful feature is query extraction. Sometimes derived method names might become too long for complex queries like this:

List<Owner> findDistinctByFirstNameIgnoreCaseOrLastNameIgnoreCaseOrPets_NameIgnoreCaseAllIgnoreCaseOrderByFirstNameAsc(
  String firstName, String lastName, String name);

JPA Buddy analyzes a derived method name and allows us to refactor it by renaming and moving the JPQL query to @Query annotation:

JPA Repository query extraction window
@Query("select distinct o from Owner o left join o.pets pets " +
  "where upper(o.firstName) = upper(:firstName) " +
  "or upper(o.lastName) = upper(:lastName) " +
  "or upper(pets.name) = upper(:name) " +
  "order by o.firstName")
List<Owner> findByAnyName(@Param("firstName") String firstName,
  @Param("lastName") String lastName,
  @Param("name") String name);

6. Entities Generation Based on DB Tables

Data usually outlives the code, so when we build a JPA Data layer over an existing database, we need to create JPA entities based on the current tables. JPA Buddy provides this functionality and allows developers to create entities by selecting desired tables. In contrast to the existing solutions, the plugin performs “smart” generation and tries to detect associations between entities. We will generate the corresponding attributes even for OneToMany and ManyToMany entities that are not backed with columns:

Screenshot-2022-11-15-at-12.46.02

7. DTOs and Mappers Generation

DTO is a valuable design pattern for the case of passing data that we cannot map directly to JPA entities. For example, when creating REST API, we might want to expose only some entity attributes.

JPA Buddy can generate DTOs and mappers with the MapStruct library. All we need to do is to select desired attributes for generated DTOs:

DTO Generation window

The plugin generates a DTO and a mapper, including proper mapping for the associations. JPA Buddy supports Lombok for this case too and generates proper annotations for DTOs:

@Data
public class OwnerDto implements Serializable {
   private final Integer id;
   @NotEmpty
   private final String firstName;
   @NotEmpty
   private final String lastName;
   private final List<PetDto> pets;
}

JPA Buddy also can generate entities from POJOs. It may be useful if you develop your application starting from API definition, not from data model.

Screenshot-2022-11-15-at-14.19.38

8. Minimalistic Mode

JPA Buddy has a friendly UI, but for those who prefer a “focused” view in IntelliJ IDEA, the plugin provides “minimalistic mode”. We can hide all tool windows and use the keyboard only for entities generation, Spring Data JPA repositories creation and edit, invoke DTO creation wizards, and so on. In addition to this, developers can use actions panel on the top of every editor to perform these actions:

Screenshot-2022-11-15-at-13.06.02

 

9. Smart Code Completion

To make developers even more productive, JPA Buddy allows code generation without changing a focus. With the smart code completion feature, a developer can create a new repository, add a method to an existing repository, and even create a DTO and a mapper without leaving the code editor. Buddy provides additional context-dependent postfixes to do this. Just by typing the entity class name and adding “.”, we can see all available methods that we can apply to the entity class:

Screenshot-2022-11-15-at-14.11.17

10. Conclusion

JPA Buddy provides a set of powerful tools that make JPA development easier. The good thing about the plugin is that it supports not only JPA but also related libraries used in data access layer development: Spring Data JPA, MapStruct, Lombok, and DB versioning solutions.

If we consider IntelliJ IDEA Community, this plugin can significantly simplify the work with JPA. IDEA Ultimate users can get some of the JPA Buddy functionality from the bundled plugins for JPA and Spring Framework support. Still, it looks like DB versioning script generation and DTOs creation features remain unique in DB development automation.

For those who prefer video format, there is a fifteen-minute recording showing JPA Buddy in action. We can see how to create an application with JPA Buddy and Spring Boot using IntelliJ IDEA:

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!