-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: POJO Generator Missing Methods
PostPosted: Wed Feb 09, 2011 10:05 am 
Newbie

Joined: Mon Feb 07, 2011 11:17 am
Posts: 5
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?
Please, has anyone been able to generate entities with these methods...

Thank you in advance


Top
 Profile  
 
 Post subject: Re: POJO Generator Missing Methods
PostPosted: Wed Feb 09, 2011 10:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
nebenzal wrote:
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?
Please, has anyone been able to generate entities with these methods...

Thank you in advance


there is no way for tools to generate equals/hashcode and tostring safely.

You have to explicitly set/enable it; see the docs concerning use-in-tostring and use-in-equals.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Re: POJO Generator Missing Methods
PostPosted: Wed Feb 09, 2011 10:25 am 
Newbie

Joined: Mon Feb 07, 2011 11:17 am
Posts: 5
max wrote:
You have to explicitly set/enable it; see the docs concerning use-in-tostring and use-in-equals.

Hi Max,
First of all thanks for your quick reply.
How does the pojo.needsToString method decide if a pojo needs toString? Does it check if there is any property configured for use in to string and if that is positive then that cycle begins?

In addition in my configuration where I don't use hbm.xml files where can I configure which properties should take place in toString/equals?

One last question, Can I add a generic exporter that will write the toString/equals/hashcode according to my own logic and that will be inserted to the pojos during the rev-eng process?
If so I'd really appreciate a link to a tutorial or something along those lines.

Thanks in advance

BTW, how can I rate your comment? I wasn't able to find a rate button


Top
 Profile  
 
 Post subject: Re: POJO Generator Missing Methods
PostPosted: Wed Feb 09, 2011 10:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
nebenzal wrote:
max wrote:
You have to explicitly set/enable it; see the docs concerning use-in-tostring and use-in-equals.

Hi Max,
First of all thanks for your quick reply.
How does the pojo.needsToString method decide if a pojo needs toString? Does it check if there is any property configured for use in to string and if that is positive then that cycle begins?


its in the docs :) http://docs.jboss.org/tools/3.1.0.GA/en ... index.html

Quote:
In addition in my configuration where I don't use hbm.xml files where can I configure which properties should take place in toString/equals?


if you are doing reverse engineering need to set the properties in reveng.xml or in a custom reverseengineeringstrateg.

Quote:
One last question, Can I add a generic exporter that will write the toString/equals/hashcode according to my own logic and that will be inserted to the pojos during the rev-eng process?
If so I'd really appreciate a link to a tutorial or something along those lines.


Yes.
The docs outlines/gives examples of this.

Quote:
BTW, how can I rate your comment? I wasn't able to find a rate button


sorry - I should update my signature now that rating isnt in the new forum ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Re: POJO Generator Missing Methods
PostPosted: Wed Feb 16, 2011 8:25 am 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
Nebenzal, you may be requesting something different than you wanted. To compare whether two objects are the same:

1) If they are from the same Hibernate session, simply compare for equality. Hibernate's load mechanism makes sure that an entity is never loaded twice (that's what it's keeping its cache for). The entity is just the Java-side mirror of a specific database row.
2) If they are not from the same Hibernate session, equality does not make much sense anyway. You better reload the entity using the primary key's value.

To compare primary keys for equality does make sense.
If your data model uses only single-field PKs, you simply use Java's isEqual().
If your data model uses multi-field PKs, Reveng will generate hashCode, isEqual and toString for you, so you don't need to do anything specific.

(Personally, I think it is a design error to define equality on entities. If I load an entity, create a copy, then change one field in the original, comparing the rows for equality isn't a very useful operation anymore. Except if you wish to check whether anything changed - that's what isDirty is good for.)


Top
 Profile  
 
 Post subject: Re: POJO Generator Missing Methods
PostPosted: Wed Aug 24, 2011 5:27 am 
Newbie

Joined: Wed Aug 24, 2011 4:17 am
Posts: 3
Hi all,

Thank you all, for your posts.

I agree with you all, concerning equals/hashcode.

Now, about toString, I've been struggling with this for years, looking for a way to, automatically, generate a toString method in all POJOs, with hibernate tools. I know that I can use the meta attribute use-in-tostring in hbm.xml... but how can I do this when not using hbm.xml files at all???

The reason why I don't use hbm.xml files is that, when doing reverse engineering, hbm.xml files are always overwrited, so the meta attribute use-in-tostring are cleaned. And when in development stage, you have to execute the reverse engineering tool, against hundreds of tables, many times, because the schema changes many times, too, so it's very painful having to edit hbm.xml files manually to insert use-in-tostring over and over again... and having a backup folder and merge hbm.xml files manually is not a good solution either...

max wrote:
nebenzal wrote:
In addition in my configuration where I don't use hbm.xml files where can I configure which properties should take place in toString/equals?

if you are doing reverse engineering need to set the properties in reveng.xml or in a custom reverseengineeringstrateg.


Max, can I specify in reveng.xml what colums/properties should take place in toString? Is this possible? I can't find in the docs.... Or, can I do this in reverse engineering strategy?

The only way I can do this is modifying the freemarker templates, but I would prefer an out-of-the-box way...

Thank you again!
Rui Baeta


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.