问题
Hibernate Tools plugin (version 3.2.4) for eclipse
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"/>
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"/>
and in the PojoToString.ftl I see:
<#if pojo.needsToString()>
Where can I set this property? I want all my pojos to need toString,equals and hashcode?
Thank you in advance
回答1:
This assumes the default Hibernate Tools/JBoss Tools
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>
来源:https://stackoverflow.com/questions/4945160/how-to-get-hibernate-tools-to-generate-pojos-with-tostring-equals-and-hashcode