2
votes

I have a table has 300 columns I want to make get a POJO from the table through reverse engineering using Eclipse - Hibernate tools. I don't want all columns exist in the generated POJO.

The problem is that there are too many columns so I need to specify all the columns names in the hibernate.reveng.xml file to exclude many of them.

I know how to exclude each column using the below tag inside the hibernate.reveng.xml file.

<table name="PEOPLE">
    <column name="REG_CODE" exclude="true"/>
       ..... <!-- around 300 lines to exclude unnecessary columns -->
</table>

Before typing in(or copy and paste from a generated .hbm.xml file) all the column names, I wonder there is any easy way to exclude unnecessary columns from the generated POJO.

I got a hint from the official Hibernate document from JBOSS site about using org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy.

Could anyone please share your experience with me?

Thank you in advance.

1

1 Answers

1
votes

I found an answer by myself. I guess no one had this kind of issue but me. This is one thing I could do. I know it is not that elegant but I could achieve what I wanted.

reveng.xml

<hibernate-reverse-engineering>
<schema-selection match-schema="TRADE"/>
<table-filter match-name="PEOPLE"/> 
<table-filter match-name="PRODUCT"/></hibernate-reverse-engineering>

CustomReverseEngineeringStrategy.java

public class CustomReverseEngineeringStrategy extends DelegatingReverseEngineeringStrategy {

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

@Override
public boolean excludeColumn(TableIdentifier identifier, String columnName) {
    if (identifier.getName().equals("PEOPLE") && (
            columnName.equals("PERSON_CODE")
            || columnName.startsWith("NAME") 
            )) {
        return false;
    }
    if (identifier.getName().equals("PRODUCT") &&  (
            columnName.startsWith("PRODUCT_CODE") 
            || columnName.startsWith("NAME") 
            || columnName.startsWith("PRICE")
            )) {
        return false;
    }
    return true;
}

}