在ECLIPSE MARS 2,通过Marketplace,安装JBOSS TOOLS 4.3.0 Final,安装的Hibernate Tools是 hibernate-tools-4.3.1.Final.jar,通过搜索,使用的路径《eclipse-jee-mars-2-win32-x86_64\eclipse\configuration\org.eclipse.osgi\1089\0\.cp\lib》
同时也安装有3.5.X,3.6.X,4.0.X几个版本。使用Hibernate Tools生成entity和hbm.xml是,数据库里面的数据表和字段的注解无法显示Comment。
0x01 参考
《》
《hibernate-tools使用心得》
0x02 修改代码
下载Hibernate Tools代码,参考《》修改对应的代码。
下载地址:
选择4.3.x分支
可以通过GIT CLONE或者自己建个普通的JAVA工程,把代码放上去,把依赖的JAR包放上去。
依赖的包,通过MAVEN BUILD一下就可以到本地的仓库了。
修改FreeMarker模板
PojoFields.ftl
<#-- // Fields -->
<#foreach field in pojo.getAllPropertiesIterator()>
<#if pojo.getMetaAttribAsBool(field, "gen-property", true)>
/**
<#if pojo.hasMetaAttribute(field, "field-description")>
${pojo.getFieldJavaDoc(field, 0)}
</#if>
<#foreach column in field.columnIterator><#if column.comment?exists && column.comment?trim?length!=0>
* Column comment: ${column.comment}.
</#if>
</#foreach>
*/
${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, jdk5)} ${field.name}<#if pojo.hasFieldInitializor(field, jdk5)> = ${pojo.getFieldInitialization(field, jdk5)}</#if>;
</#if>
</#foreach>
PojoPropertyAccessors.ftl
<#-- // Property accessors -->
<#foreach property in pojo.getAllPropertiesIterator()>
<#if pojo.getMetaAttribAsBool(property, "gen-property", true)>
/**
<#if pojo.hasFieldJavaDoc(property)>
* ${pojo.getFieldJavaDoc(property, 4)}
</#if>
<#foreach column in property.columnIterator>
<#if column.comment?exists && column.comment?trim?length!=0>
* getter: ${column.comment}.
</#if>
</#foreach>
*/
<#include "GetPropertyAnnotation.ftl"/>
${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${pojo.getGetterSignature(property)}() {
return this.${property.name};
}
/**
<#if pojo.hasFieldJavaDoc(property)>
* ${pojo.getFieldJavaDoc(property, 4)}
</#if>
<#foreach column in property.columnIterator>
<#if column.comment?exists && column.comment?trim?length!=0>
* setter: ${column.comment}.
</#if>
</#foreach>
*/
${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)}(${pojo.getJavaTypeName(property, jdk5)} ${property.name}) {
<#if pojo.hasMetaAttribute(property, "pre-cond")>
${c2j.getMetaAsString(property, "pre-cond","\n")}
</#if>
this.${property.name} = ${property.name};
<#if pojo.hasMetaAttribute(property, "post-cond")>
${c2j.getMetaAsString(property, "post-cond","\n")}
</#if>
}
</#if>
</#foreach>
PojoTypeDeclaration.ftl
/**
* ${pojo.getClassJavaDoc(pojo.getDeclarationName() + " generated by hbm2java", 0)}
<#if clazz.table.comment?exists>
* Entity: ${clazz.table.comment}
</#if>
*/
<#include "Ejb3TypeDeclaration.ftl"/>
${pojo.getClassModifiers()} ${pojo.getDeclarationType()} ${pojo.getDeclarationName()} ${pojo.getExtendsDeclaration()} ${pojo.getImplementsDeclaration()}
generalhbm.hbm.ftl
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
for General Global Setttings
-->
<#if c2h.isImportData(cfg)>
<#include "import.hbm.ftl">
</#if>
<#if c2h.isNamedQueries(cfg)>
<#include "query.hbm.ftl">
</#if>
<#if c2h.isNamedSQLQueries(cfg)>
<#include "sql-query.hbm.ftl">
</#if>
<#if c2h.isFilterDefinitions(cfg)>
<#include "filter-def.hbm.ftl">
</#if>
</hibernate-mapping>
hibernate-mapping.hbm.ftl
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated ${date} by Hibernate Tools ${version} -->
<#if hmgs?exists && hmgs.hasNonDefaultSettings()>
<hibernate-mapping
<#if hmgs.hasDefaultPackage()>
package="${hmgs.defaultPackage}"
</#if>
<#if hmgs.hasSchemaName()>
schema="${hmgs.schemaName}"
</#if>
<#if hmgs.hasCatalogName()>
catalog="${hmgs.catalogName}"
</#if>
<#if hmgs.hasNonDefaultCascade()>
default-cascade="${hmgs.defaultCascade}"
</#if>
<#if hmgs.hasNonDefaultAccess()>
default-access="${hmgs.defaultAccess}"
</#if>
<#if !hmgs.isDefaultLazy()>
default-lazy="false"
</#if>
<#if !hmgs.isAutoImport()>
auto-import="false"
</#if>>
<#else>
<hibernate-mapping>
</#if>
<#include "persistentclass.hbm.ftl"/>
</hibernate-mapping>
以下根据上文参考文章进行修改:
TemplateProducer.java
jtidy.properties
MySQLMetaDataDialect.java
JDBCBinder.java
TemplateHelper.java
JDBCReaderFactory.java
TableIdentifier.java
0x03 解决hbm.xml生成中文乱码问题
修改过的包,生成JAVA的ENTITY文件可以显示中文注解,但是生成的HBM.XML文件依然出现乱码。原因jtidy.properties好像设置了编码格式没生效。改造过的HIBERNATE TOOLS进行JTIDY格式化后,重新生成格式化后的HBM.XML出现乱码。
org.hibernate.tool.hbm2x
XMLPrettyPrinter.java
修改一下函数为:
public static void prettyPrintFile(Tidy tidy, File inputFile,
File outputFile, boolean silent) throws IOException {
log.debug( "XMLPrettyPrinting " + inputFile.getAbsolutePath() );
InputStream is;
OutputStream os;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
is = new BufferedInputStream( new FileInputStream( inputFile ) );
// fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destination), "UTF-8"));
outputFile.getParentFile().mkdirs();
outputFile.createNewFile();
os = new BufferedOutputStream(bos );
tidy.setInputEncoding("utf-8");
tidy.setOutputEncoding("utf-8");
tidy.parse( is, os );
// byte[] bs = bos.toByteArray();//乱码导致乱码。
// byte[] bs = bos.toString("utf-8").getBytes();//依然乱码
// String tempWrite= new String(bs,"utf-8");//无法解决乱码
String tempWrite=bos.toString("utf-8");
try {
is.close();
}
catch (IOException e1) {
// ignore
}
try {
os.flush();
os.close();
}
catch (IOException e1) {
// ignore
}
// generate output file
if ( tidy.getParseErrors() == 0 ) {
// BufferedOutputStream out = new BufferedOutputStream(
// new FileOutputStream( outputFile ) );
// InputStream in = new ByteArrayInputStream( bs );
Writer fileWriter = null;
fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8"));
fileWriter.write(tempWrite);
if(fileWriter!=null) {
try {
fileWriter.flush();
fileWriter.close();
}
catch (IOException e) {
log.warn("Exception while flushing/closing " + outputFile,e);
}
}
// Transfer bytes from in to out
// byte[] buf = new byte[1024];
// int len;
// while ( (len = in.read( buf ) ) > 0 ) {
// out.write( buf, 0, len );
// }
// in.close();
// out.close();
}
if ( tidy.getParseErrors() > 0 ) {
if(silent) {
log.warn("Tidy was unable to process file " + inputFile + ", " + tidy.getParseErrors() + " errors found." );
} else {
throw new ExporterException( "Tidy was unable to process file "
+ inputFile + ", " + tidy.getParseErrors() + " errors found." );
}
} else {
log.debug("XMLPrettyPrinting completed");
}
}
最后,替换生成的CLASS文件,jar -cvf hibernate-tools-4.3.1.Final.jar . 重新打包替换ECLIPSE目录下的 hibernate-tools-4.3.1.Final.jar。