3

I'd like to know how i can change the name of my POJO when generating it using hibernate.

My tables have a naming convention of : FR_ and TRN_. While generating the POJOs I'd like the remove the FR and TRN and append VO to the name.

For example,

Table name: FR_ACCOUNT_MST

POJO to be generated: accountMstVO

Thanks, Varun

Varun Achar
  • 14,781
  • 7
  • 57
  • 74

3 Answers3

8

Right, you have to extend the DelegatingReverseEngineeringStrategy class (hibernate-tool.jar lib) and override tableToClassName method.

The code below will rename FR_ACCOUNT_MST to FR_ACCOUNT_MSTVO.

I let you use some regex to get the result wanted.

The variable className contains the package + class name (ie. com.mycompany.project.hibernate.FR_ACCOUNT_MST)

Source: http://www.cereslogic.com/pages/2008/08/05/hibernate-tools-tips-for-reverse/

package com.altenor.coffre.generated;

import org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.ReverseEngineeringStrategy;
import org.hibernate.cfg.reveng.TableIdentifier;

public class CoffreReverseEngineeringStrategy extends DelegatingReverseEngineeringStrategy {

    public CoffreReverseEngineeringStrategy(ReverseEngineeringStrategy delegate) {
        super(delegate);
    }

    //add Base before class name
    public String tableToClassName(TableIdentifier tableIdentifier) {
          String className = super.tableToClassName(tableIdentifier);
          return className+"VO";
        }
}
Remy Mellet
  • 1,675
  • 20
  • 23
  • Damn.. this was the only question that i hadn't marked as answered.. Thanks for the solution.. even though I needed it a few months ago.. :).. and welcome to SOF! – Varun Achar Aug 23 '11 at 17:06
5

Or you can do it by adding in the hibernate.reveng.xml file the name of each pojo:

<hibernate-reverse-engineering>
  <table-filter match-schema="CO" match-name="FR_ACCOUNT_MST"/>

  <table name="FR_ACCOUNT_MST" schema="CO" class="com.bonables.co.hibernate.pojo.accountMstVO" />

</hibernate-reverse-engineering>
Remy Mellet
  • 1,675
  • 20
  • 23
1

I am assuming that you are using Hibernate Tool's ability to reverse engineer the domain model classes from database metadata. In that case, you might want to implement a custom org.hibernate.cfg.reveng.ReverseEngineeringStrategy as explained here.

Binil Thomas
  • 13,699
  • 10
  • 57
  • 70