1. Overview
1.概述
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.
在本教程中,我们将看一下JPA的一些支持工具。我们将专注于两个最流行的IDE的可用插件。IntelliJ IDEA和Eclipse.。
2. JPA Support in IntelliJ IDEA and Eclipse
2.IntelliJ IDEA和Eclipse中的JPA支持
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.
JPA是在Java应用程序中与关系数据库合作的最广泛使用的规范。事实上,JPA定义了实现的所有方面,从注解到数据处理规则。
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.
通常情况下,我们并不只在JPA实体上工作。除了纯粹的ORM相关代码外,我们可能还需要诸如数据库版本系统、SQL/JPQL/HQL查询优化、与IoC容器的集成等内容。这里就是插件变得非常有用的地方。它们可以支持数据库逆向工程、模式生成、迁移脚本生成或Spring Data JPA存储库的支架。
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:
当涉及到在应用程序开发中使用JPA框架时,通常是在IDE的帮助下完成的。这是因为它们为我们提供了一套强大的工具,以提高开发人员的工作效率:。
- 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.
让我们看看两个最常见的IDE–IntelliJ IDEA和Eclipse,看看它们在JPA应用开发方面提供了哪些支持。
3. IntelliJ IDEA
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.
IntelliJ IDEA有两个版本。社区版(免费)和终极版(付费)。终极版有一个捆绑的插件,支持JPA。
On the other hand, the Community Edition doesn’t provide specialized support for working with JPA, Hibernate, Spring Data, etc.
另一方面,社区版并没有为使用JPA、Hibernate、Spring Data等提供专门支持。
However, IntelliJ has a wide marketplace with different plugins. Therefore, we can find support for almost any technology, and JPA is no exception.
然而,IntelliJ有一个广泛的市场,有不同的插件。因此,我们可以找到对几乎所有技术的支持,JPA也不例外。
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.
对于 IntelliJ IDEA 社区用户,我们将介绍 JPA Buddy插件以支持 JPA 功能。尽管如此,它也可以成为IntelliJ IDEA Ultimate的一个很好的补充。。
3.1. JPA Plugin for IntelliJ Ultimate
3.1.用于IntelliJ Ultimate的JPA插件
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.
在IntelliJ IDEA Ultimate中,我们可以使用一个专门的工具窗口 – “Persistence”。这将显示项目中JPA实体的结构。
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.
我们也可以利用逆向工程过程。 它将为我们生成JPA实体、关联和适当的注释。此外,它还会创建pesistence.xml文件或更新它(如果存在)。
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):
除了通用的JPA支持外,如果我们选择Hibernate作为JPA实现,它将提供额外的帮助。我们可以执行Hibernate特定的操作,例如(但不限于):
- 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.
在Youtube上有一个详细的评论,显示了该插件的操作。。
3.2. JPA Buddy
3.2 JPA伙伴
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.
JPA Buddy提供了一组可视化的编辑器,以处理JPA实体和Spring Data JPA存储库。此外,它同时支持Java和Kotlin语言。
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.
我们不需要记住所有的JPA注解、Spring Data资源库方法命名规则或Liquibase标签。该插件允许我们从一个调色板中选择所需的项目。
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解决的另一个挑战是数据库的版本问题。该插件同时支持Liquibase和Flyway工具,并能相应地生成XML或SQL迁移脚本。
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.
JPA Buddy通过比较项目数据库和代码库中定义的实际JPA实体来生成脚本。生成后,我们可以审查生成的脚本,并在保存到代码库之前更新它们。
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.
除了JPA支持外,该插件可以协助创建Spring Data JPA资源库。它提供了可视化的编辑器,可以帮助我们进行正确的资源库方法命名。
4. Eclipse
4.日蚀
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.
正如我们所知,Eclipse有一个很大的插件生态系统。然而,Dali Tools插件是Eclipse IDE中JPA支持的事实标准。
Let’s explore this plugin. Then, we’ll see how to get some extra support if we use Hibernate in our project.
让我们来探索这个插件。然后,我们将看到如果我们在项目中使用Hibernate,如何获得一些额外的支持。
4.1. Dali Tools
4.1.达利工具
Dali covers almost all areas of JPA development. Its functionality includes (but not limited to):
Dali几乎涵盖了JPA开发的所有领域。其功能包括(但不限于)。
- 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:
该插件提供了两个主要的视觉工具。与上下文相关的 “JPA细节 “编辑器允许我们处理不同的JPA对象,我们可以在 “JPA结构 “导航器中选择这些对象:。
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.
另外,专门的persistence.xml编辑器允许我们避免记忆所有根据JPA规范可以在这个配置文件中使用的参数。我们可以编辑从连接选项到锁定超时或验证模式的一切。
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.
Dali Tools插件可以基于JPA模型为特定数据库生成和执行DDL SQL。此外,执行阶段与生成阶段是分开的,因此我们可以在将生成的SQL应用到应用程序的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
4.2.JBoss Hibernate工具
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.
如果我们需要专门针对Hibernate的支持,让我们来看看我们的一个选择。 在Eclipse的插件库中,我们可以找到Jboss Hibernate Tools插件。
This plugin can be used independently or in conjunction with JPA Tools and provides:
本插件可独立使用或与JPA工具结合使用,并提供:
- 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.
生成的JPA实体可以通过 *.hbm.xml映射文件表示为POJOs,或者表示为注释类。此外,该插件支持为每个实体生成具有CRUD方法的DAO对象。该DAO利用Hibernate特定的类来访问数据库。
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.
HQL执行控制台和标准编辑器允许我们生成和执行HQL查询。除了执行任意的HQL外,控制台还提供HQL语句的SQL预览。这对于创建优化的SQL查询以避免 “n+1选择 “和其他ORM边缘情况是非常有用的。
5. Conclusion
5.总结
In this article, we’ve covered several tools for JPA support available in the most popular Java IDEs: IntelliJ IDEA and Eclipse.
在这篇文章中,我们已经介绍了最流行的Java IDE中可用的几种JPA支持工具。IntelliJ IDEA和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.
我们看到了这些是如何在开发过程中帮助编写更好的应用程序并提高生产力的。因此,在选择最佳工具时,我们不仅要注意 “纯JPA “的功能,还要注意与IDE和所选开发栈的集成。