views:

114

answers:

1

I have

  • A MySQL database currently in production use for a CakePHP application
  • A Java SE application accessing the same database via Hibernate, currently in development.

I'm using the Netbeans "automigrate" feature to create the POJO classes and XML files (do I really need the XML files when using annotations?). As the schema is quite complex creating the tables manually is way too much work.

Cake expects all DB tables to be pluralized (the Address class is automagically mapped to the addresses table). When running the Netbeans automigration it then does pluralization on the already pluralized table names (I'm getting Addresses.java and setAddresseses() methods).

I know I'm asking for trouble running two very different data layers against the same database, but I'd like to know if it's possible to have Netbeans generating the POJO classes in singular form or if there is another (better) way to manage this.

+2  A: 

When doing reverse engineering, it is possible to use a custom reverse engineering strategy. Quoting the documentation:

It is possible to implement a user strategy. Such strategy must implement org.hibernate.cfg.reveng.ReverseEngineeringStrategy. It is recommended that one uses the DelegatingReverseEngineeringStrategy and provide a public constructor which takes another ReverseEngineeringStrategy as argument. This will allow you to only implement the relevant methods and provide a fallback strategy. Example of custom delegating strategy which converts all column names that ends with "PK" into a property named "id".

public class ExampleStrategy extends

DelegatingReverseEngineeringStrategy {

 public ExampleStrategy(ReverseEngineeringStrategy

delegate) { super(delegate); }

 public String columnToPropertyName(TableIdentifier

table, String column) { if(column.endsWith("PK")) { return "id"; } else { return super.columnToPropertyName(table, column); } } }

In your case, you might want to implement

public String tableToClassName(TableIdentifier tableIdentifier) {
    return delegate==null?null:delegate.tableToClassName(tableIdentifier);
}

to "depluralize" classes generated from table names (ADDRESSES => Address).

But sadly, the NetBeans Hibernate Reverse Engineering Wizard does not provide an option to pick reverse engineering strategy if the user happen to have any (this is an enhancement planned for NetBeans 7.0 if time permits).

So if you want to use a custom strategy, you'll have to use Ant or Maven. This is currently not possible from NetBeans.

Pascal Thivent