问题描述
我专门找JPA代码生成技术
I am specifically looking for JPA code generation technique
首先,什么是所有项目可以生成符合 JPA 的代码?(例如 HibernateTools)
First, what are all the project could generate JPA compliant code? (Eg. HibernateTools)
其次,我还想自定义代码生成实用程序,因为它必须符合我们的企业标准.
Second, I would also like to customize the code generation utility, as it has to compliant to our corporate standards.
如果没有,有哪些框架可以使用反射生成java代码?所以我可以从头开始写作.
If not, what are all the framework available to generate java code using reflection? so I can write from scratch.
注意:我使用 eclipse 生成 JPA 代码并反复重构以使其合规.
Note: I used eclipse to generate JPA code and refactor it repeatedly to make it compliant.
推荐答案
我也很难理解这个问题,但我会尝试改写:
I also have difficulties understanding the question, but I'll try to rephrase:
- 您在数据库中有大量数据并希望通过 JPA 访问这些数据
- 您不想手动编写类来访问不同的数据库/表
- 目前所有/大部分模型类都是从 Eclipse 中生成的
- 这些模型有 JPA 注释
- 模型类(或注释)不符合公司标准
当您说JPA java 代码生成"时,我理解从提供的数据库连接生成 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 注释类?
- 是否可以自定义这些框架的输出?以何种方式?
回答第一个问题:
- Hibernate Tools - 问题中已经提到,但我更喜欢这个
- EclipseLink
- Netbeans
- Hibernate Tools - already mentioned in the question, but I prefer this one
- EclipseLink
- Netbeans
我真的很喜欢 Netbeans 代码生成,尤其是当你向不熟悉 JPA 的人展示结果时.
I really like the Netbeans code generation, especially if you show the results to someone not familiar with JPA.
在定制层面,我只能分享我使用 Hibernate Tools 的经验.特别是对于可重复的结果,请尝试使用 基于蚂蚁的任务.您可以轻松地将一些目标添加到您的构建中,代码生成可以在两个级别完成:
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:
- 使用属性一个>
- 提供自定义freemarker 模板
使用模板,您应该能够涵盖大部分公司标准.查看 hibernate-tools 包的 pojo
目录.自定义代码生成的最简单方法是复制和调整模板,并将它们放在用于创建 pojo 的 ant 任务中的 hibernate-tools.jar 之前.
With the templates you should be able to cover most of the corporate standards. Look into the pojo
directory 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.
正如在另一条评论中已经指出的那样,之后可能很难修改生成的代码.我希望以下提示可以帮助您:
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:
- 尝试将生成的源文件和自定义的源文件分开放在不同的文件夹中.
- 考虑使用
@MappedSuperclass
用于您可能需要手动调整的类.
这篇关于JPA java代码生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!