2014-05-15 60 views
0

我重寫了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

我確實在這個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; 
    }