在hibernate中定制逆向工程策略后忽略了包名称

时间:2014-05-15 08:53:53

标签: java hibernate reverse-engineering hibernate-tools

我在hibernate中覆盖了逆向工程的默认策略。

public class MyReverseEngineeringStrategy extends DelegatingReverseEngineeringStrategy {   
    public MyReverseEngineeringStrategy(ReverseEngineeringStrategy delegate) {
        super(delegate);
    }

    @Override
    public String columnToPropertyName( TableIdentifier table, String column) {
        return column;
    }
    @Override   
    public String tableToClassName( TableIdentifier table){
        return table.getName();
    }
}

问题是类是在默认包中生成的,而不是在我指定的包名中生成的。如果我删除了tableToClassName方法,则会在包右包中生成类。

任何解释?
感谢。

1 个答案:

答案 0 :(得分:1)

我确实找到了这个link的解决方案。事实上,当我覆盖tableToClassName时,我必须指定类的全名,即包。

我尝试了链接中建议的解决方案,但它确实有效。这是我的新MyReverseEngineeringStrategy课程:

public class MyReverseEngineeringStrategy extends DelegatingReverseEngineeringStrategy {

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

    @Override
    public String columnToPropertyName( TableIdentifier table,
                                        String column) {
        return column;
    }

    @Override
    public String tableToClassName(TableIdentifier tableIdentifier) {
        final String defaultClassName = super.tableToClassName(tableIdentifier);
        final String packageName = this.extractPackageName(defaultClassName);
        final String className = tableIdentifier.getName();
        final String concatedString = packageName.concat(className);
        return concatedString;
    }

    private String extractPackageName(String defaultClassName) {
        int lastIndex = defaultClassName.lastIndexOf(".");
        final String packageName = defaultClassName.substring(0, lastIndex + 1);
        return packageName;
    }