Hibernate - 将注释从属性(方法)级别移动到字段级别
如何从带有字段级别注释的表生成休眠域类?我使用 Hibernate Tools 项目并从数据库中的表生成域类。生成的类在 getter 方法上而不是在字段级别上有注释。请建议一种生成带有注释字段的域类的方法。 eclipse/IDEA 等中是否有可用的重构工具将注释从方法级别移动到字段级别?
感谢您的帮助和时间。
How do I generate hibernate domain classes from tables with annotations at field level? I used Hibernate Tools project and generated domain classes from the tables in the database. The generated classes have annotations on the getter methods rather than at the field level. Kindly advice a way to generate domain classes that have the fields annotated. Is there any refactoring facility available in eclipse/IDEA etc.. to move the annotations from method level to field level?
Appreciate your help and time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
步骤如下:
通过在 Eclipse 文件夹中执行搜索来分配“hibernate-tools.jar”
例如,您将在以下位置找到它:
C:\eclipse\plugins\org.hibernate.eclipse_3.4.1.xxx\lib\tools
解压到临时文件夹(WinRar 可以做到这一点)
例如,解压到
[Your Project]\template
在[您的项目]\template\pojo文件夹下,创建一个名为“Ejb3FieldGetAnnotation.ftl”的文件
该文件实际上是“Ejb3PropertyGetAnnotation.ftl”的副本,但所有单词“property”都被“field”替换,因为该文件将被放置在迭代所有字段(而不是属性)的循环中。
我在这篇文章中包含了该文件的内容
删除属性级注释:
在文件“PojoPropertyAccessors.ftl”中,删除或注释掉
添加字段级注释:
在文件“PojoFields.ftl”中,添加
生成Java实体时,选择“使用自定义模板”并指定模板文件夹。在本例中,它将是 [您的项目]\template
<前><代码>============================================== =======================================
Ejb3FieldGetAnnotation.ftl
=================================================== =================================
<#if ejb3>
<#if pojo.hasIdentifierProperty()>
<#if field.equals(clazz.identifierProperty)>
${pojo.generateAnnIdGenerator()}
<#-- 如果这是 id 属性(getter)-->
<#--显式设置该属性的列名-->
<#if c2h.isOneToOne(field)>
${pojo.generateOneToOneAnnotation(字段, cfg)}
<#elseif c2h.isManyToOne(field)>
${pojo.generateManyToOneAnnotation(字段)}
<#--TODO支持可选和targetEntity-->
${pojo.generateJoinColumnsAnnotation(字段, cfg)}
<#elseif c2h.isCollection(field)>
${pojo.generateCollectionAnnotation(字段, cfg)}
<#else>
${pojo.generateBasicAnnotation(字段)}
${pojo.generateAnnColumnAnnotation(字段)}
希望它适合您。
Here are the steps:
Allocate "hibernate-tools.jar" by perform a search within your eclipse folder
For example, You will find it at
C:\eclipse\plugins\org.hibernate.eclipse_3.4.1.xxx\lib\tools
Extract to a temp folder (WinRar can do this)
For example, extract to
[Your Project]\template
Under [Your Project]\template\pojo folder, create a file named "Ejb3FieldGetAnnotation.ftl"
This file is actually a copy of "Ejb3PropertyGetAnnotation.ftl" but all of words "property" are replaced by "field" because this file will be placed in the a loop that iterates through all fields (instead of properties).
I include the content of the file in this post
Remove property-level annotations:
In file "PojoPropertyAccessors.ftl", remove or comment out
Add field-level annotations:
In file "PojoFields.ftl", add
When generate Java entities, select "Use Custom Templates" and specify the template folder. In this case, it will be [Your Project]\template
Hope it work for you.
我花了很多时间阅读 5 年前的答案,但不了解如何做到这一点(特别是如果您在 Intellij 而不是 Eclipse 中工作),为什么这个问题还没有解决。所以我找到了它,就在这里,而且很简单:
在 Intellij 中:
persistence.xml
相同的文件夹中创建一个文件orm.xml
您的实体将具有字段注释!
I spent a lot of time reading answers from 5+ years ago without understanding how to do it (especially if you work in Intellij and not Eclipse) and how come this is not already solved. So i found it, here it is, and it is simple:
In Intellij:
orm.xml
in the same folder as yourpersistence.xml
with this contentYour entities will have field annotations!
目前需要使用自定义模板。这里有更多的参考和例子来实现这一点:
https://forum.hibernate.org/ viewtopic.php?f=6&t=1003858&p=2429868#p2429868
currently it is necessary to use custom templates. here is more references and examples hot to implement this:
https://forum.hibernate.org/viewtopic.php?f=6&t=1003858&p=2429868#p2429868