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.  [ 15 posts ] 
Author Message
 Post subject: Customizing code generation
PostPosted: Thu Dec 14, 2006 7:18 am 
Newbie

Joined: Tue Nov 29, 2005 11:20 am
Posts: 9
Hibernate version:3.2 beta8

Hi,
I am trying to reverse engineer and get the DAO & POJO code generated. In the POJO, for all members corresponing to 1..n relationship the term "es" is added as suffix to the Set.

For example
public class FileUploadDetails implements java.io.Serializable
{
// Fields
private Set fileActivationDetailses = new HashSet( 0 );


How can I get rid of the es?

Thanks and Regards,
~H


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 9:58 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
By default, as you can see, it attempts to do a simple pluralization for english, but since it's such a pain in the butt language, it is not always that easy.

Using a reverse engineering file, where foreign key constraint name between FileUploadDetails and FileActivationDetails is named FUD_F01_FAD:
Code:
<hibernate-reverse-engineering>
   <table name="FILE_UPLOAD_DETAILS" class="eg.hibernate.FileUploadDetails">
      <foreign-key constraint-name="FUD_F01_FAD">
         <set property="fileActivationDetails"/>
      </foreign-key>
   </table>
</hibernate-reverse-engineering>


or, using a DelegatingReverseEngineeringStrategy:
Code:
public class Strategy extends DelegatingReverseEngineeringStrategy {
   @Override
   public String foreignKeyToCollectionName(String keyname, TableIdentifier fromTable, List fromColumns,
         TableIdentifier referencedTable, List referencedColumns, boolean uniqueReference) {
      String propertyName = super.foreignKeyToCollectionName(keyname, fromTable, fromColumns, referencedTable,
            referencedColumns, uniqueReference);
      if ("fileActivationDetailses".equals(propertyName)) {
         propertyName = "fileActivationDetails"
      }
      return propertyName;
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 1:40 pm 
Newbie

Joined: Thu Dec 14, 2006 1:28 pm
Posts: 10
I'm running into the same problem with the sets having an "es" at the end. The solution posted works but it seems like creating the hibernate reverse engineering file or the custom strategy where you check for the specific names doesn't seem scalable.

I would like to modify the tools if possible but I'm not sure where adding the "es" occurs.

If Max could point me to in the right direction, I'll make the change and post it if other people don't like the "es" at the end either.

Minh


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 2:15 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
It's in the DefaultReverseEngineeringStrategy's foreignKeyToCollectionName, where it invokes ReverseEngineeringStrategyUtil.simplePluralize method to pluralize the property name. The 'es' is only added if the property name ends in 's'.

Of course, it would be easier if DBA's named their tables like they were singular entities instead of collections of entities. Like FileUploadDetails. When you get a row from that table you are getting a FileUploadDetails. Does that make sense? No. It would be nice if the table name referenced the singular row object returned, e.g. FileUploadDetail. That's pretty much the way I handle it, by mapping the tables to singular names of entities. So, even if the table name is FILE_UPLOAD_DETAILS, I map that to the FileUploadDetail class, and then the collections are properly named fileUploadDetails.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 2:21 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
minho wrote:
The solution posted works but it seems like creating the hibernate reverse engineering file or the custom strategy where you check for the specific names doesn't seem scalable.


That was just example code, you can put whatever you want in the foreignKeyToCollectionName.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 2:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
so write a revengstrategy that does exactly what you want for collections.

What would you actually prefer instead ?

getCustomerSet() or ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 2:34 pm 
Newbie

Joined: Thu Dec 14, 2006 1:28 pm
Posts: 10
Thanks for the quick response Max and Ananasi, first off....you are right about the table name convention, they should be singular and Max, no I don't need a getCustomerSet(). I just wanted to get rid of the "es" where ever that string was added but since I'm using a fresh database, changing the names to be singular won't be a deal.

If the need ever arises where I want to get rid of the "es" in the code, I know where to look now...thanks guys.

Much appreciated :)

Minh


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 14, 2006 6:27 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what we actually need is someone to make our pluralize code aware of proper endings. anyone up for it ? :)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 1:28 pm 
Newbie

Joined: Thu Dec 14, 2006 1:28 pm
Posts: 10
I'll take it up. Once I have something that I think is better, I'd like to post it here so anyone that is an english major can point out any other nuances in the english language that I might have over looked.

Minh


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 2:37 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
tip: jdk 1.4 regular expressions can probably help you solve this in a good/better way.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 11:14 am 
Newbie

Joined: Thu Dec 20, 2007 6:36 pm
Posts: 2
Hi,

Just wonder if there are any updates on this from minho or any one else? Or if I would like to contribute by making that changes, where should I start?

Thank you.

Eddie


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 12:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Hi Eddie,

There haven't been any proper pluralization functionallity added yet so a contribution would be worthwhile.

Where you should start - look at the ReverseEngineeringStrategy and the its default implementation DefaultReverseEngineeringStrategy and locate the places that converts table/column names into propertynames and collection names. These are the places where pluralization should be implemented.

There are some basic pluralization features/tests today so just search for plural and you should find it.

p.s. this is in hibernate tools core code - its 100% independent of the eclipse code base.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 1:47 pm 
Newbie

Joined: Thu Dec 20, 2007 6:36 pm
Posts: 2
Thanks, Max. I think I will also need a few pointers such as where to check out source codes and where/how to post proposed changes, as I am new to this open-source collaboration.

Thank you!

Eddie


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 26, 2007 4:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Look at the Build and Contribute page: http://www.hibernate.org/268.html

Should have all the info - if something doesn't work let me know ,)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Depluralization
PostPosted: Sun Mar 23, 2008 3:50 pm 
Newbie

Joined: Wed Mar 19, 2008 11:19 am
Posts: 15
Location: Orlando
You may find this function helpful:
Code:
public String depluralize(String word) {
    if (word.matches(".*oe?s$"))
        return word.replaceAll("oe?s$", "o");
    else if (word.matches(".*[s,z,ch,sh,x]es$"))
        return word.replaceAll("es$", "");
    else if (word.matches(".*sses$"))
        return word.replaceAll("es$","");      
    else if (word.matches(".*ses$"))
        return word.replaceAll("s$", "");
    else if (word.matches(".*ies$"))
        return word.replaceAll("ies$", "y");
    else if (word.matches(".*es"))
        return word.replaceAll("es$", "");
    else if (word.matches(".*s"))
        return word.replaceAll("s$", "");
    else
        return word;
}


It's not perfect, but it's pretty good. There are certain words like "cases" which don't depluralize correctly. I prefer to pluralize my table names and use singular class names. It just makes more sense to me.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 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.