Hibernate 逆向工程 - 排除不必要列的任何智能方法? [英] Hibernate reverse engineering - any smart way to exclude unnecessary columns?

查看:50
本文介绍了Hibernate 逆向工程 - 排除不必要列的任何智能方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有 300 列的表我想使用 Eclipse - Hibernate 工具通过逆向工程从表中获取 POJO.我不希望生成的 POJO 中存在所有列.

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.

问题是列太多了,所以我需要在 hibernate.reveng.xml 文件中指定所有列名称以排除其中许多列.

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.

我知道如何使用 hibernate.reveng.xml 文件中的以下标记排除每一列.

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>

在输入(或从生成的 .hbm.xml 文件复制并粘贴)所有列名之前,我想知道有什么简单的方法可以从生成的 POJO 中排除不必要的列.

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.

我从 JBOSS 站点的官方 Hibernate 文档中得到了有关使用 org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy 的提示.

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?

提前致谢.

推荐答案

我自己找到了答案.我想没有人有这种问题,但我.这是我可以做的一件事.我知道它不是那么优雅,但我可以实现我想要的.

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.

reven.xml

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

CustomReverseEngineeringStrategy.java

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

}

这篇关于Hibernate 逆向工程 - 排除不必要列的任何智能方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆