解决方案:
- 创建两个 ReverseEngineeringStrategies(基础和混凝土)
- 从 HibernateTools.jar 更新 Freemarker 模板
- 在 Eclipse(Hibernate 插件)中创建代码生成配置并设置策略和自定义模板。
- 切换逆向工程策略并运行两次。
来源:
BaseClassStrategy:
public class BaseClassStrategy extends DelegatingReverseEngineeringStrategy {
public BaseClassStrategy(ReverseEngineeringStrategy delegate) {
super(delegate);
}
@Override
public Map<String, MetaAttribute> tableToMetaAttributes(TableIdentifier tableIdentifier) {
@SuppressWarnings("unchecked")
Map<String, MetaAttribute> metaAttributes = super.tableToMetaAttributes(tableIdentifier);
if (metaAttributes == null) {
metaAttributes = new HashMap<String, MetaAttribute>();
}
// Update modifier
if (!metaAttributes.containsKey("scope-class")) {
MetaAttribute metaAttribute = new MetaAttribute("scope-class");
metaAttribute.addValue("public abstract");
metaAttributes.put(metaAttribute.getName(), metaAttribute);
}
// Update class name
if (!metaAttributes.containsKey("generated-class")) {
MetaAttribute metaAttribute = new MetaAttribute("generated-class");
metaAttribute.addValue(tableToAbstractClassName(tableIdentifier));
metaAttributes.put(metaAttribute.getName(), metaAttribute);
}
return metaAttributes;
}
private String tableToAbstractClassName(TableIdentifier tableIdentifier) {
String className = super.tableToClassName(tableIdentifier);
int dotIndex = className.lastIndexOf('.');
return className.substring(0, dotIndex + 1) + className.substring(dotIndex + 1) + "Base";
}
}
ConcreteClassStrategy:
public class ConcreteClassStrategy extends DelegatingReverseEngineeringStrategy {
public ConcreteClassStrategy(ReverseEngineeringStrategy delegate) {
super(delegate);
}
@Override
public Map<String, MetaAttribute> tableToMetaAttributes(TableIdentifier tableIdentifier) {
@SuppressWarnings("unchecked")
Map<String, MetaAttribute> metaAttributes = super.tableToMetaAttributes(tableIdentifier);
if (metaAttributes == null) {
metaAttributes = new HashMap<String, MetaAttribute>();
}
String className = super.tableToClassName(tableIdentifier);
int dotIndex = className.lastIndexOf('.');
String abstractClassName = className.substring(dotIndex + 1) + "Base";
// Update extends modifier
if (!metaAttributes.containsKey("scope-class")) {
MetaAttribute metaAttribute = new MetaAttribute("extends");
metaAttribute.addValue(abstractClassName);
metaAttributes.put(metaAttribute.getName(), metaAttribute);
}
return metaAttributes;
}
}
休眠模板:
将以下文件夹结构添加到您的项目中:
src/main/resources
|-> hibernate-templates
|-> dao
|-> pojo
从 hibernate-tools.jar 复制 pojo 和 dao 文件夹并更新以下文件。只有同时添加两个文件夹才有效!
Ejb3TypeDeclaration.ftl
<#if ejb3?if_exists>
<#if pojo.isComponent()>
@${pojo.importType("javax.persistence.Embeddable")}
<#else>
@${pojo.importType("javax.persistence.Entity")}
@${pojo.importType("javax.persistence.Table")}(name="${clazz.table.name}"
<#if clazz.table.schema?exists>
,schema="${clazz.table.schema}"
</#if><#if clazz.table.catalog?exists>
,catalog="${clazz.table.catalog}"
</#if>
<#assign uniqueConstraint=pojo.generateAnnTableUniqueConstraint()>
<#if uniqueConstraint?has_content>
, uniqueConstraints = ${uniqueConstraint}
</#if>)
</#if>
</#if>
Ejb3TypeDeclaration.ftl
<#if ejb3?if_exists>
<#if pojo.isComponent()>
@${pojo.importType("javax.persistence.Embeddable")}
<#else>
@${pojo.importType("javax.persistence.MappedSuperclass")}
</#if>
</#if>
Pojo.ftl
${pojo.getPackageDeclaration()}
// Generated ${date} by Hibernate Tools ${version}
<#assign classbody>
<#include "PojoTypeDeclaration.ftl"/> {
<#if !pojo.isInterface()>
<#if pojo.getDeclarationName()?ends_with("Base")>
<#include "PojoFields.ftl"/>
</#if>
<#include "PojoConstructors.ftl"/>
<#if pojo.getDeclarationName()?ends_with("Base")>
<#include "PojoPropertyAccessors.ftl"/>
<#include "PojoToString.ftl"/>
<#include "PojoEqualsHashcode.ftl"/>
</#if>
<#else>
<#include "PojoInterfacePropertyAccessors.ftl"/>
</#if>
<#include "PojoExtraClassCode.ftl"/>
}
</#assign>
${pojo.generateImports()}
${classbody}
PojoConstructor.ftl
<#-- /** default constructor */ -->
public ${pojo.getDeclarationName()}() {}
<#if pojo.needsMinimalConstructor()>
<#-- /** minimal constructor */ -->
public ${pojo.getDeclarationName()}(${c2j.asParameterList(pojo.getPropertyClosureForMinimalConstructor(), jdk5, pojo)}) {
<#if pojo.getDeclarationName()?ends_with("Base")>
<#foreach field in pojo.getPropertiesForMinimalConstructor()>
this.${field.name} = ${field.name};
</#foreach>
<#else>
super(${c2j.asArgumentList(pojo.getPropertyClosureForMinimalConstructor())});
</#if>
}
</#if>
<#if pojo.needsFullConstructor()>
<#-- /** full constructor */ -->
public ${pojo.getDeclarationName()}(${c2j.asParameterList(pojo.getPropertyClosureForFullConstructor(), jdk5, pojo)}) {
<#if pojo.getDeclarationName()?ends_with("Base")>
<#foreach field in pojo.getPropertiesForFullConstructor()>
this.${field.name} = ${field.name};
</#foreach>
<#else>
super(${c2j.asArgumentList(pojo.getPropertyClosureForFullConstructor())});
</#if>
}
</#if>