JPA java代码生成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/452627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:47:51  来源:igfitidea点击:

JPA java code generation

javajpaautocompletetemplatescode-generation

提问by Mohan Narayanaswamy

I am specifically looking for JPA code generation technique

我特别在寻找 JPA 代码生成技术

First, what are all the project could generate JPA compliant code? (Eg. HibernateTools)

首先,所有项目可以生成哪些符合 JPA 的代码?(例如。HibernateTools)

Second, I would also like to customize the code generation utility, as it has to compliant to our corporate standards.

其次,我还想自定义代码生成实用程序,因为它必须符合我们的公司标准。

If not, what are all the framework available to generate java code using reflection? so I can write from scratch.

如果没有,可用于使用反射生成 Java 代码的所有框架是什么?所以我可以从头开始写。

Note: I used eclipse to generate JPA code and refactor it repeatedly to make it compliant.

注意:我使用 eclipse 生成 JPA 代码并反复重构以使其兼容。

采纳答案by Kariem

I also have difficulties understanding the question, but I'll try to rephrase:

我也很难理解这个问题,但我会尝试改写:

  • You have a lot of data in a DB and want to access it via JPA
  • You don't want to manually write the classes to access the different DBs/tables
  • Currently all/most of your model classes are generated from within Eclipse
  • These models have JPA annotations
  • The model classes (or the annotations) are not according to corporate standards
  • 您在数据库中有大量数据并希望通过 JPA 访问它
  • 您不想手动编写类来访问不同的数据库/表
  • 目前所有/大部分模型类都是从 Eclipse 中生成的
  • 这些模型有 JPA 注释
  • 模型类(或注释)不符合企业标准

When you say "JPA java code generation", I understand generating JPA annotated model classes from a supplied DB connection. Most frameworks often refer to this as reverse engineering.

当您说“JPA java 代码生成”时,我理解从提供的数据库连接生成 JPA 注释模型类。大多数框架通常将其称为逆向工程。

Now you have two questions:

现在你有两个问题:

  1. What code generators can be recommended to generate JPA annotated classes?
  2. Is it possible to customize the output of these frameworks, and in which way?
  1. 可以推荐哪些代码生成器来生成 JPA 注释类?
  2. 是否可以自定义这些框架的输出,以何种方式?

To answer to the first question:

回答第一个问题:

I really like the Netbeans code generation, especially if you show the results to someone not familiar with JPA.

我真的很喜欢 Netbeans 代码生成,尤其是当您向不熟悉 JPA 的人展示结果时。

At the level of customization I can only share the experience I had with Hibernate Tools. Especially for reproducible results, try to use the ant-based tasks. You can easily add some targets to your build and code generation can be done at two levels:

在定制层面,我只能分享我使用 Hibernate Tools 的经验。特别是对于可重现的结果,请尝试使用基于 ant 的任务。您可以轻松地将一些目标添加到您的构建中,代码生成可以在两个级别完成:

With the templates you should be able to cover most of the corporate standards. Look into the pojodirectory of the hibernate-tools package. The easiest way to customize the code generation is to copy and adapt the templates and have them put before the hibernate-tools.jar in the ant task used to create the pojos.

使用模板,您应该能够涵盖大部分公司标准。查看pojohibernate-tools 包的目录。自定义代码生成的最简单方法是复制和调整模板,并将它们放在用于创建 pojo 的 ant 任务中的 hibernate-tools.jar 之前。

As already pointed out in another comment, it might be difficult to modify the generated code afterwards. I hope the following tips can help you:

正如在另一条评论中已经指出的那样,之后可能很难修改生成的代码。我希望以下提示可以帮助您:

  • Try to separate generated and customized source files in different folders.
  • Think about using @MappedSuperclassfor classes which you may want to adapt in a manual step.
  • 尝试将生成的源文件和自定义的源文件分开放在不同的文件夹中。
  • 考虑@MappedSuperclass用于您可能希望在手动步骤中适应的类。

回答by cletus

Ok, basically you have things the wrong way arond: JPA is the generation tool.

好了,基本上你有事情走错了路arond: JPA is the generation tool

I say this because the only thing you could generate JPA entities from is SQL and the whole point of JPA is to do things the other way around. You define your object model first and, from that, you can generate your tables and queries.

我这样说是因为您唯一可以从中生成 JPA 实体的是 SQL,而 JPA 的全部意义在于以相反的方式做事。你首先定义你的对象模型,然后你可以生成你的表和查询。

For example, I've seen projects use Hibernate to define their entities and then they have an ant build script that creates the database from the Hibernate entity model.

例如,我见过一些项目使用 Hibernate 来定义他们的实体,然后他们有一个 ant 构建脚本从 Hibernate 实体模型创建数据库。

JPA entity definitions--especially done with annotations--aren't exactly onerous. They really are your best option as the first thing to do rather than being the product of something else.

JPA 实体定义——尤其是使用注释完成的——并不是很繁重。它们确实是您首先要做的最佳选择,而不是成为其他事物的产物。

Besides, another tool won't help you write named queries, define the correct cascade options on relationships, etc. And if you had generated code, how would you handle modifying it afterwards?

此外,另一个工具不会帮助您编写命名查询、定义关系的正确级联选项等。如果您生成了代码,您将如何处理之后的修改?

It's just not the right way to go.

这不是正确的方法。

回答by Kariem

check out JPM2java, Its a code generator for JPA. The only catch is it does not generate JPA code from SQL files or table, you'd need a orm.xml file. If you are looking for a tool to generate code directly from tables, you may want to try Netbeans. It has options to generate JPA code directly from tables

查看JPM2java,它是 JPA 的代码生成器。唯一的问题是它不会从 SQL 文件或表生成 JPA 代码,您需要一个 orm.xml 文件。如果您正在寻找直接从表生成代码的工具,您可能想尝试 Netbeans。它具有直接从表生成 JPA 代码的选项

回答by snowflake

Project lombok seems allowing you to generate basic named queries, this is another approach using annotations and code generation at compile time.

lombok 项目似乎允许您生成基本的命名查询,这是另一种在编译时使用注释和代码生成的方法。

See:

看:

A guy over the hibernate forum seems using a traditionnal code generation approach with Hibernate Tools : https://forum.hibernate.org/viewtopic.php?f=9&t=962223&p=2315766&hilit=named+queries+generate#p2315766

休眠论坛上的一个人似乎使用传统的代码生成方法和休眠工具:https: //forum.hibernate.org/viewtopic.php?f=9&t=962223&p=2315766&hilit=named+queries+generate# p2315766

I agree with cletus on the point that you cannot generate all named queries, but I guess we can imagine generating basic named queries such finders based on one or several fields of the object.

我同意 cletus 的观点,即您不能生成所有命名查询,但我想我们可以想象生成基本的命名查询,例如基于对象的一个​​或多个字段的查找器。

回答by psuzzi

I've used Dali Persistence Eclipse Plugin, The tool is available for download via the Indigo Java EE SR1 update site.

我使用过Dali Persistence Eclipse 插件,该工具可通过Indigo Java EE SR1 更新站点下载

After the plugin is installed, to make a reverse engineering of your DB, you need to create a new JPA project, set database connection, launch the automated download of JPA runtime (in my case Eclipse Link), then start the generation process.

安装插件后,要对您的数据库进行逆向工程,您需要创建一个新的 JPA 项目,设置数据库连接,启动 JPA 运行时的自动下载(在我的情况下为 Eclipse Link),然后开始生成过程。

During the code generation process you're asked to provide details on table mappings and generated classes. At the end of the generation the code is clean.

在代码生成过程中,您需要提供有关表映射和生成类的详细信息。在生成结束时,代码是干净的。

回答by lgu

Another efficient solution for JPA code generation is "Telosys Tools"

JPA 代码生成的另一个有效解决方案是“ Telosys Tools

An Eclipse pluginworking from an existing database ( "database firts" approach ) with customizable templates. This solution is more flexible than Dali thanks to its lightweight model and the Velocity templates (shared on GitHub )

一个Eclipse插件从现有的数据库(“数据库firts”方法)与合作定制的模板。由于其轻量级模型和 Velocity 模板(在 GitHub 上共享),该解决方案比 Dali 更灵活

See the web site : http://www.telosys.org/

请参阅网站:http: //www.telosys.org/

The plugin on Eclipse Marketplace : http://marketplace.eclipse.org/content/telosys-tools

Eclipse Marketplace 上的插件:http: //marketplace.eclipse.org/content/telosys-tools

A brief description of the principle : http://labs.sogeti.com/code-generation-can-it-be-simple-and-pragmatic/

原理简述:http: //labs.sogeti.com/code-generation-can-it-be-simple-and-pragmatic/

For JPA generation, use the JPA templatesavailable on GitHub : https://github.com/telosys-templates-v3

对于JPA 生成,请使用GitHub 上提供的JPA 模板https: //github.com/telosys-templates-v3

回答by xflorian

Minuteprojectis a generator tool and can generate JPA1/ JPA2as well as hibernate ORM-like artifacts. It is based on reverse-engineering from the database. You can instruct the generator to apply convention for your java code that do not follow your DB convention, but the mapping will be correct. (Example strip DB name prefix; table starting with ADMIN_ such as ADMIN_ENVIRONMENT are Environment (w/out Admin) as java class) For the moment there 20+ conventionsthat help you reshape your model to be less DB look-and-feel by more Java-OO-friendly.

Minuteproject是一个生成器工具,可以生成 JPA1/ JPA2以及类似休眠 ORM 的工件。它基于数据库的逆向工程。您可以指示生成器为不遵循数据库约定的 Java 代码应用约定,但映射将是正确的。(示例条带 DB 名称前缀;以 ADMIN_ 开头的表,例如 ADMIN_ENVIRONMENT 是 Environment (w/out Admin) 作为 java 类)目前有20 多种约定可帮助您重塑模型以减少 DB 外观和感觉Java-OO 友好。

Another interesting feature is updatable-codeenabling to modify both the generated code and your model ANDaht the next generation your modifications will be kept!The generator makes the merge.

另一个有趣的功能是可更新代码,可以修改生成的代码和您的模型并且在下一代您的修改将被保留!生成器进行合并。

The templates are opensource and work with velocity, it is 'quite' easy to append a track to make specific one for your framework (ex security aspects... that are relevant to your organisation). You can scope your template to field level, entity (table or view), package (group of entities), model, application providing flexibility, and since the template of a track knows each other via metadata it is quite easy to reference then from other templates and to associate them by configuration with naming convention.

这些模板是开源的并且工作速度很快,添加一个轨道来为您的框架制作特定的轨道是“非常”容易的(例如安全方面......与您的组织相关)。您可以将模板范围限定为字段级别、实体(表或视图)、包(实体组)、模型、应用程序,提供灵活性,并且由于轨道模板通过元数据相互了解,因此很容易从其他模板引用模板并通过配置将它们与命名约定相关联。

回答by Frank

The ideal tool/eclipse plugin for jpa code reverse generation is Hibernate Tools. This has now been made a part of JBoss Tools. So, in your eclipse start off with installing JBoss Tools.

jpa 代码反向生成的理想工具/eclipse 插件是 Hibernate Tools。这现已成为 JBoss 工具的一部分。因此,在您的 Eclipse 中,从安装 JBoss Tools 开始。

Then create a JPA Project. This project will act as the holder of all your code/configurations related to the reverse generation project. Installing the JBoss Tools first gives you the advantage that your Hibernate Configuration(part of Hibernate Tools) gets created along with your JPA project.

然后创建一个 JPA 项目。该项目将作为与反向生成项目相关的所有代码/配置的持有者。首先安装 JBoss 工具给您带来的好处是您的 Hibernate 配置(Hibernate 工具的一部分)与您的 JPA 项目一起创建。

Next step would be to use the Hibernate Tools to actually reverse generate your JPA POJO entities corresponding to your database tables.

下一步是使用 Hibernate 工具实际反向生成与数据库表相对应的 JPA POJO 实体。

To understand the steps for JPA POJO reverse generation in detail you can have a look at the following tutorial...http://www.javabrahman.com/j2ee/how-to-do-reverse-code-generation-of-hibernatejpa-pojo-entities-using-jboss-tools-eclipse-plugin/

要详细了解 JPA POJO 反向生成的步骤,您可以查看以下教程... http://www.javabrahman.com/j2ee/how-to-do-reverse-code-generation-of-hibernatejpa -pojo-entities-using-jboss-tools-eclipse-plugin/

The above tutorial also has links to tutorials for creating a JPA Project and also for installing JBoss Tools in your eclipse installation both of which are pre-requisites for JPA POJO entities reverse code generation.

上面的教程还包含指向创建 JPA 项目和在 eclipse 安装中安装 JBoss 工具的教程的链接,这两者都是 JPA POJO 实体反向代码生成的先决条件。

回答by Shineed Basheer

Please have a look on JOOQ.

请查看 JOOQ。

http://www.jooq.org/

http://www.jooq.org/

Open Source is available .....

开源可用......

回答by jGauravGupta

Open source graphical tool to generate JPA class, visualize & modify Database, reverse engineering exiting source code and import models from existing database.

用于生成 JPA 类、可视化和修改数据库、逆向工程现有源代码和从现有数据库导入模型的开源图形工具。

For NetBeans : http://jpamodeler.github.io/
For Eclipse : https://www.eclipse.org/webtools/dali/

对于 NetBeans:http: //jpamodeler.github.io/
对于 Eclipse:https: //www.eclipse.org/webtools/dali/