原文:http://www.blogjava.net/pauliz/archive/2009/11/13/302162.html

有同学需要修改后的Hibernate Tools
整个tool我就不上传了,需要的请到http://downloads.sourceforge.net/jboss/HibernateTools-3.2.4.GA-R200905070146-H18.zip 或者 http://files.cnblogs.com/eggbucket/hibernate-tools.zip下载。

hibernate-tools.zip
    是修改后的jar,解压后覆盖plugins\org.hibernate.eclipse_3.2.4.GA-R200905070146-H18\lib\tools\hibernate-tools.jar
template.zip 或者 http://files.cnblogs.com/eggbucket/template.zip
    是ftl模板,Hibernate Code Generation Configuration中Main选项卡最下面Template directory可指定使用自定义的模板

template.zip只是在默认的模板上做了少量修改,抛砖引玉了,如果各位有更好的模板,也希望能为大家共享一下

最近做项目时,使用Hibernate Tools 3.2.4生成entity和hbm.xml,但默认情况下,DB中的comments没法生成到javadoc和xml中,改了templates倒是有注释了,但却是乱码,心里一直耿耿于怀...(这不符合咱一直强调的编码规范不是?最主要的是人懒,有时用entity不想再找文档)。在网上找了半天,大多说是freemarker编码设置问题,但不管怎么设置,都没一点效果,决定自己动手。下了源码,查到原因,人家压根就没处理中文问题。记录一下处理过程。

ftl是freemarker模板,可以在jar包外使用,java和properties重新打包替换hibernate-tools.jar,如果是eclipse-plugins,jar包在plugins\org.hibernate.eclipse_3.2.4.GA-R200905070146-H18\lib\tools\hibernate-tools.jar

pojo\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}.
</#if>
</#foreach>
     */
    ${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, jdk5)} ${field.name}<#if pojo.hasFieldInitializor(field, jdk5)> = ${pojo.getFieldInitialization(field, jdk5)}</#if>;
</#if>
</#foreach>

pojo\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>     * 取得 ${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>     * 设置 ${column.comment}.
</#if>
</#foreach>
     */   
    ${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)}(${pojo.getJavaTypeName(property, jdk5)} ${property.name}) {
        this.${property.name} = ${property.name};
    }
</#if>
</#foreach>

org\hibernate\tool\hbm2x\TemplateProducer.java

public void produce(Map additionalContext, String templateName, File destination, String identifier, String fileType, String rootContext) {
        
        String tempResult = produceToString( additionalContext, templateName, rootContext );
        
        if(tempResult.trim().length()==0) {
            log.warn("Generated output is empty. Skipped creation for file " + destination);
            return;
        }
        FileWriter fileWriter = null;
        Writer fileWriter = null;
        try {
            
            th.ensureExistence( destination );    
         
            ac.addFile(destination, fileType);
            log.debug("Writing " + identifier + " to " + destination.getAbsolutePath() );
            fileWriter = new FileWriter(destination);
            fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destination), "UTF-8"));

            fileWriter.write(tempResult);            
        } 
        catch (Exception e) {
            throw new ExporterException("Error while writing result to file", e);    
        } finally {
            if(fileWriter!=null) {
                try {
                    fileWriter.flush();
                    fileWriter.close();
                }
                catch (IOException e) {
                    log.warn("Exception while flushing/closing " + destination,e);
                }                
            }
        }
        
    }

org\hibernate\tool\hbm2x\jtidy.properties

indent=auto
indent-spaces=4
#indent-attributes=yes
    wrap=180
markup=yes
clean=yes
output-xml=yes
input-xml=yes
show-warnings=yes
trim-empty-elements=yes
input-encoding=utf-8
output-encoding=utf-8

补充:
无法取得MySQL5 Table的Comments,修改org.hibernate.cfg.reveng.dialec.MySQLMetaDataDialect
重载getTables方法

/**
     * MetaData中无法取得table Comment,重载
     */
    @Override
    public Iterator getTables(String xcatalog, String xschema, String xtable) {
        try {
            final String catalog = caseForSearch(xcatalog);
            final String schema = caseForSearch(xschema);
            final String table = caseForSearch(xtable);

            log.debug("getTables(" + catalog + "." + schema + "." + table + ")");

            ResultSet tableRs = getMetaData().getTables(catalog, schema, table, new String[] { "TABLE", "VIEW" });

            return new ResultSetIterator(tableRs, getSQLExceptionConverter()) {

                Map element = new HashMap();

                protected Object convertRow(ResultSet tableResultSet) throws SQLException {
                    element.clear();
                    putTablePart(element, tableResultSet);
                    element.put("TABLE_TYPE", tableResultSet.getString("TABLE_TYPE"));

                    String remarks = tableResultSet.getString("REMARKS");
                    if (StringHelper.isEmpty(remarks)) {
                        String sql = "show table status " + (schema == null ? "" : " from " + schema + " ") + " like '"
                                + element.get("TABLE_NAME") + "' ";
                        PreparedStatement statement = getConnection().prepareStatement(sql);

                        ResultSet tableRs = statement.executeQuery();

                        if (tableRs.next()) {
                            remarks = tableRs.getString("COMMENT");
                        }
                    }
                    element.put("REMARKS", remarks);
                    return element;
                }

                protected Throwable handleSQLException(SQLException e) {
                    // schemaRs and catalogRs are only used for error reporting if
                    // we get an exception
                    String databaseStructure = getDatabaseStructure(catalog, schema);
                    throw getSQLExceptionConverter().convert(
                            e,
                            "Could not get list of tables from database. Probably a JDBC driver problem. "
                                    + databaseStructure, null);
                }
            };
        } catch (SQLException e) {
            // schemaRs and catalogRs are only used for error reporting if we get an exception
            String databaseStructure = getDatabaseStructure(xcatalog, xschema);
            throw getSQLExceptionConverter().convert(e,
                    "Could not get list of tables from database. Probably a JDBC driver problem. " + databaseStructure,
                    null);
        }
    }

外键默认生成List,修改org.hibernate.cfg.JDBCBinder

/**
     * @param rc
     * @param processed
     * @param table
     * @param object
     */
    private Property bindOneToMany(PersistentClass rc, ForeignKey foreignKey, Set processed, Mapping mapping) {

        Table collectionTable = foreignKey.getTable();
        Collection collection = new org.hibernate.mapping.Set(rc); // MASTER TODO: allow overriding collection type
        Collection collection = new org.hibernate.mapping.Bag(rc); // MASTER TODO: allow overriding collection type
。。。
 
关于无法取得MySQL5 Table的Comments的问题,楼主的修改源码的方案是可行的。当我从GitHub获取了hibernate-tools源码,并修改了“org.hibernate.cfg.reveng.dialect.JDBCMetaDataDialect”类之后,问题解决了。
这时候,我突然想到个问题,既然是通过JDBC获取数据库元信息,没有获取到,应该是数据库厂商JDBC驱动有问题,为什么要去修改hibernate-tools的源码呢?
最后我找到了MySQL驱动的这个bug(http://bugs.mysql.com/bug.php?id=65213),并从该bug页找到了解决方案:
修改MySQL JDBC驱动URL,在其后追加:“?useUnicode=true&amp;characterEncoding=UTF-8&amp;useInformationSchema=true”即可。
 
 
 
 

Hibernate Tools生成注释的更多相关文章

  1. 用hibernate tools生成对应的sql应用代码

    参考资料: eclipse在线配置hibernate tools http://jingyan.baidu.com/article/db55b609959d154ba20a2f5d.html [图]H ...

  2. 用Hibernate Tools生成Hibernate Mapping映射文件

    Eclipse中要集成安装Hibernate Tools组件 如果没有,请查看:Eclipse juno 中安装 JBoss Tools,集成Hibernate 一.确定环境: 1.Maven3.0. ...

  3. 【原创】Hibernate自动生成(1)

    本实战是博主初次学习Java,分析WCP源码时,学习HibernateTools部分的实战,由于初次接触,难免错误,仅供参考,希望批评指正. 开发环境: Eclipse Version: Photon ...

  4. Hibernate Tools for Eclipse的使用

    Hibernate Tools的官方网站:http://hibernate.org/tools/Step1.安装好Hibernate Tools,建立一个Dynamic web project,工程名 ...

  5. Hibernate Tools 自动生成hibernate的hbm文件

    本文有待商榷 当我们在新增插件的时候发现会出现duplicate location,意思是所选的anchive所包含的zip路径已经复用,现象如下: 如上图所示黄色标记部分“Duplicate loc ...

  6. 使用Hibernate Tools从数据库逆向生成Hibernate实体类

    自动生成model.java.*.hbm.xml 甚至是dao.java.*.ddl.*.html等等.一般也就如下三种方式1. MyEclipse 自带插件2. jboss的 hibernate-t ...

  7. Eclipse中通过Hibernate Tools插件实现从数据库逆向生成Hibernate带注解的实体类

    一.安装hibernate tools插件 1.在线安装 通过Eclipse的Help->Install New Software 在线安装插件,插件连接为: eclipse helios(3. ...

  8. 利用Hibernate注解生成表

    转自:http://blog.csdn.net/madison__/article/details/55677099 Hibernate4注释 @Entity(name = "tbl_use ...

  9. eclipse中怎么添加Hibernate tools

    最近在学习Hibernate框架,但是用eclipse的时候发现自己安装的过程不是很顺利,因此记下来,供自己和别人参考. Hibernate Tools是由JBoss推出的一个Eclipse集成开发工 ...

随机推荐

  1. K临近算法

    K临近算法原理 K临近算法(K-Nearest Neighbor, KNN)是最简单的监督学习分类算法之一.(有之一吗?) 对于一个应用样本点,K临近算法寻找距它最近的k个训练样本点即K个Neares ...

  2. Keepalived原理与实战精讲

    什么是Keepalived呢,keepalived观其名可知,保持存活,在网络里面就是保持在线了,也就是所谓的高可用或热备,用来防止单点故障(单点故障是指一旦某一点出现故障就会导致整个系统架构的不可用 ...

  3. C# 获取用户IP地址(转载)

    [ASP.NET开发]获取客户端IP地址 via C# 说明:本文中的内容是我综合博客园上的博文和MSDN讨论区的资料,再通过自己的实际测试而得来,属于自己原创的内容说实话很少,写这一篇是为了记录自己 ...

  4. Django Rest framework 之 解析器

    RESTful 规范 django rest framework 之 认证(一) django rest framework 之 权限(二) django rest framework 之 节流(三) ...

  5. fetch发送Form Data请求并携带cookie

    今天我们来说说如何fetch发送Form Data请求并携带cookie,直接进入正题好吧,别问我今天为啥不在开始吹两句了,累到一句牛逼不想吹...... 步骤1: 设置头部,"Conten ...

  6. python之类与类之间的关系

    在我们的世界中事物和事物之间总会有一些联系.   在面向对象中. 类和类之间也可以产生相关的关系 1. 依赖关系 执行某个动作的时候. 需要xxx来帮助你完成这个操作. 此时的关系是最轻的. 随时可以 ...

  7. 使用node.js进行API自动化回归测试

    概述 传统的QA自动化测试通常是基于GUI的,比如使用Selenium,模拟用户在界面上操作.但GUI测试的开发.维护成本和运行的稳定性一直是测试界的老大难问题.投入大量的人力物力开发.维护.运行,却 ...

  8. Salesforce小知识:在简档中设置Visualforce页面的权限

    简档(Profile)中的 Visualforce 页面访问权限 在Salesforce中,对于自定义的简档,可以设置"Visualforce 页面访问"的权限. Visualfo ...

  9. Apex简介

    Apex特点 Salesforce为开发者提供了Apex语言.它是一种语法上类似于Java的编程语言,有以下特点: 面向对象 完全在云端处理,包括保存.编译.执行 强类型 大小写不敏感(这一点和其他大 ...

  10. eclipse显示代码行数

    最近做的手机APP正在进行最后一部分了,在一个类中估计要写上千行代码,来回的拉动滚动条太麻烦了,于是发现为什么我得eclipse不显示代码行数呢  其他C什么的编译器都显示的. 于是百度了一下,一下子 ...