Hibernate - 将注释从属性(方法)级别移动到字段级别

发布于 2024-08-14 01:02:20 字数 172 浏览 5 评论 0原文

如何从带有字段级别注释的表生成休眠域类?我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疏忽 2024-08-21 01:02:21

步骤如下:

  1. 通过在 Eclipse 文件夹中执行搜索来分配“hibernate-tools.jar”
    例如,您将在以下位置找到它:
    C:\eclipse\plugins\org.hibernate.eclipse_3.4.1.xxx\lib\tools

  2. 解压到临时文件夹(WinRar 可以做到这一点)
    例如,解压到 [Your Project]\template

  3. 在[您的项目]\template\pojo文件夹下,创建一个名为“Ejb3FieldGetAnnotation.ftl”的文件

    该文件实际上是“Ejb3PropertyGetAnnotation.ftl”的副本,但所有单词“property”都被“field”替换,因为该文件将被放置在迭代所有字段(而不是属性)的循环中。
    我在这篇文章中包含了该文件的内容

  4. 删除属性级注释:
    在文件“PojoPropertyAccessors.ftl”中,删除或注释掉

    <#include "GetPropertyAnnotation.ftl"/>
    
  5. 添加字段级注释:
    在文件“PojoFields.ftl”中,添加

    <#include "Ejb3FieldGetAnnotation.ftl"/>
    ${pojo.getFieldModifiers(字段)} ... 
    
  6. 生成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:

  1. 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

  2. Extract to a temp folder (WinRar can do this)
    For example, extract to [Your Project]\template

  3. 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

  4. Remove property-level annotations:
    In file "PojoPropertyAccessors.ftl", remove or comment out

    <#include "GetPropertyAnnotation.ftl"/>
    
  5. Add field-level annotations:
    In file "PojoFields.ftl", add

    <#include "Ejb3FieldGetAnnotation.ftl"/>
    ${pojo.getFieldModifiers(field)} ... 
    
  6. When generate Java entities, select "Use Custom Templates" and specify the template folder. In this case, it will be [Your Project]\template

    ==================================================================================
    Ejb3FieldGetAnnotation.ftl
    ==================================================================================
    
    <#if ejb3>
    <#if pojo.hasIdentifierProperty()>
    <#if field.equals(clazz.identifierProperty)>
     ${pojo.generateAnnIdGenerator()}
    <#-- if this is the id property (getter)-->
    <#-- explicitly set the column name for this property-->
    </#if>
    </#if>
    
    <#if c2h.isOneToOne(field)>
    ${pojo.generateOneToOneAnnotation(field, cfg)}
    <#elseif c2h.isManyToOne(field)>
    ${pojo.generateManyToOneAnnotation(field)}
    <#--TODO support optional and targetEntity-->    
    ${pojo.generateJoinColumnsAnnotation(field, cfg)}
    <#elseif c2h.isCollection(field)>
    ${pojo.generateCollectionAnnotation(field, cfg)}
    <#else>
    ${pojo.generateBasicAnnotation(field)}
    ${pojo.generateAnnColumnAnnotation(field)}
    </#if>
    </#if>
    

Hope it work for you.

慢慢从新开始 2024-08-21 01:02:21

我花了很多时间阅读 5 年前的答案,但不了解如何做到这一点(特别是如果您在 Intellij 而不是 Eclipse 中工作),为什么这个问题还没有解决。所以我找到了它,就在这里,而且很简单:

在 Intellij 中:

  1. 使用以下内容在与 persistence.xml 相同的文件夹中创建一个文件 orm.xml
<?xml version="1.0" encoding="UTF-8"?>
    <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
                     version="2.0">
        <persistence-unit-metadata>
            <persistence-unit-defaults>
                <access>FIELD</access>
            </persistence-unit-defaults>
        </persistence-unit-metadata>
    </entity-mappings>
  1. 现在您可以生成您的 pojo(生成持久性映射 -> 通过数据库模式 -> 选择您的表等,并且不要忘记勾选“生成 JPA 注释”)

您的实体将具有字段注释!

@Entity
@Table(name = "user", schema = "public", catalog = "my_db")
public class User {
    @Id
    @Column(name = "id")
    private Integer id;
...
}

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:

  1. Create a file orm.xml in the same folder as your persistence.xml with this content
<?xml version="1.0" encoding="UTF-8"?>
    <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
                     version="2.0">
        <persistence-unit-metadata>
            <persistence-unit-defaults>
                <access>FIELD</access>
            </persistence-unit-defaults>
        </persistence-unit-metadata>
    </entity-mappings>
  1. Now you can generate your pojos (Generate persistence mapping -> By database schema -> choose your tables etc and don't forget to tick the "Generate JPA Annotations")

Your entities will have field annotations!

@Entity
@Table(name = "user", schema = "public", catalog = "my_db")
public class User {
    @Id
    @Column(name = "id")
    private Integer id;
...
}
城歌 2024-08-21 01:02:21

目前需要使用自定义模板。这里有更多的参考和例子来实现这一点:
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文