如何让Hibernate Tools用toString,equals和hashcode生成POJO? [英] How to get Hibernate Tools to generate POJOs with toString,equals and hashcode?

查看:199
本文介绍了如何让Hibernate Tools用toString,equals和hashcode生成POJO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于eclipse的Hibernate Tools插件(版本3.2.4)

Hibernate Tools plugin (version 3.2.4) for eclipse

大家好,

我正在使用该插件对我的POJO进行反向工程来自我的DB-Schema的DAO和由于某种原因在POJO中没有创建toString,equals和hashcode方法。

我正在做的是以下内容:
创建一个新的JPA项目。

配置它的persistence.xml文件如下:

Hi all,
I'm using the plugin to reverse engineer my POJOs and DAOs from my DB-Schema and for some reason the toString,equals and hashcode methods aren't created in the POJOs.
What I'm doing is the following: Create a new JPA project.
Configure it's persistence.xml file as follows:

<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代码生成配置创建新配置我启用了从JDBC连接反向工程定义输出目录,包和reveng文件。

另外我检查了该选项卡中的所有复选框(除了使用自定义模板)。

在我使用的导出器选项卡中:

1.使用Java 5语法。

2.生成EJB3注释。

并使用域代码和DAO代码导出器。

这很好用(在解决了DTP插件兼容性的一些问题之后)。

主要问题是我在Pojo.ftl中看到了以下表达式:

Created a hibernate.reveng.xml file to only select my dbo scehma in my catalog.
Then created a Hibernate Console Configuration with type as JPA and the connection to be taken from the JPA config, and lastly configured the Persistence unit.
Used the Hibernate Code Generation configurations to create a new config where I enabled the "Reverse engineer from JDBC Connection" defined the output directory, package and reveng file.
In addition I checked all the checkboxes in that tab (apart from use custom templates).
In the exporters tab I used:
1. Use Java 5 syntax.
2. Generate EJB3 annotations.
and used the "Domain code" and "DAO code" exporters.
This works fine (after solving some problems with the DTP plugin compatibality).
The main problem is that I see in the Pojo.ftl the following expressions:

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

在PojoToString.ftl我看到:

and in the PojoToString.ftl I see:

<#if pojo.needsToString()> 

我在哪里可以设置此属性?我希望我的所有pojos都需要toString,equals和hashcode?

Where can I set this property? I want all my pojos to need toString,equals and hashcode?

提前谢谢

推荐答案

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

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

You define it with each column you want to have a toString() or equals() call. Provide it as a meta property

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

这篇关于如何让Hibernate Tools用toString,equals和hashcode生成POJO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆