开发者

Change POJO name while generating from hibernate

I'd like to know how i can c开发者_C百科hange 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


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


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜