我覆盖了休眠中逆向工程的默认策略。
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
方法,则在正确的包中生成类。有什么解释吗?
谢谢。
最佳答案
我确实在此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;
}