【问题标题】:How to get Hibernate Tools to generate POJOs with toString,equals and hashcode?如何让 Hibernate 工具生成带有 toString、equals 和 hashcode 的 POJO?
【发布时间】:2011-06-24 03:05:24
【问题描述】:

Eclipse 的 Hibernate Tools 插件(版本 3.2.4)

大家好,
我正在使用该插件从我的 DB-Schema 对我的 POJO 和 DAO 进行逆向工程,并且由于某种原因,在 POJO 中没有创建 toString、equals 和 hashcode 方法。
我正在做的是以下内容: 创建一个新的 JPA 项目。
配置它的 persistence.xml 文件如下:

<persistence-unit name="PU">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="hibernate.connection.password" value="pass"/>
<property name="hibernate.connection.url" value="jdbc:sqlserver://****:1433;DatabaseName=myDB"/>
<property name="hibernate.connection.username" value="user"/>
<property name="hibernate.default_catalog" value="myDB"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
<property name="hibernate.connection.schema" value="dbo"/>

<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="false"/>

创建了一个 hibernate.reveng.xml 文件以仅在我的目录中选择我的 dbo scehma。
然后创建了一个类型为 JPA 的 Hibernate 控制台配置以及要从 JPA 配置中获取的连接,最后配置了持久性单元。
使用 Hibernate Code Generation 配置创建一个新配置,其中我启用了“从 JDBC 连接反向工程”定义了输出目录、包和 reveng 文件。
此外,我检查了该选项卡中的所有复选框(使用自定义模板除外)。
在我使用的出口商标签中:
1. 使用 Java 5 语法。
2.生成EJB3注解。
并使用了“域代码”和“DAO 代码”导出器。
这很好用(在解决了 DTP 插件兼容性的一些问题之后)。
主要问题是我在 Pojo.ftl 中看到以下表达式:

<#include "PojoToString.ftl"/>
<#include "PojoEqualsHashcode.ftl"/>

在 PojoToString.ftl 中我看到:

<#if pojo.needsToString()> 

我可以在哪里设置这个属性?我希望我所有的 pojo 都需要 toString、equals 和 hashcode?

提前谢谢你

【问题讨论】:

    标签: java jpa code-generation reverse-engineering hibernate-tools


    【解决方案1】:

    这假定默认的 Hibernate 工具/JBoss 工具

    您可以使用要调用 toString() 或 equals() 的每一列来定义它。将其作为meta 属性提供

    <table schema="public" name="someName" class="com.stackovervlow">
            <primary-key>
    
            </primary-key>
            <column name="xx" property="id" type="long">
                <meta attribute="use-in-tostring">true</meta>
                <meta attribute="scope-set">private</meta>
            </column>
            <column name="yy">
                <meta attribute="use-in-tostring">true</meta>
                <meta attribute="use-in-equals">true</meta>
            </column>   
        </table>
    

    【讨论】:

      猜你喜欢
      • 2012-02-17
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      • 2021-11-12
      • 2013-09-25
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      相关资源
      最近更新 更多