Search code examples
hibernatenaming-conventionshibernate-mappingpojo

Change POJO name while generating from hibernate


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


Solution

  • 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";
            }
    }