`
neo
  • 浏览: 265667 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

自定义 Hibernate Tools 的模板

阅读更多
在主界面Hibernate Code Generation Configuration的某个具体配置中,勾选
use custom templates,并且指定template文件(.ftl)所在的目录
注意一点,template目录下的文件,依然要保持 hibernate-tools.jar这个包的路径,
即 daohome.ftl, 要放在 dao\daohome.ftl,不可直接放在template目录的根路径

hibernate-tools.jar的路径在
hibernateTools/plugins/org.hibernate.eclipse_3.2.4.GA-R200905070146-H18/lib/tools下

如果指示同步生成/更新hibernate.cfg.xml,这个文件会自动生成在 output directory下,和pojo同目录

生成后的refresh策略,不要选择entire workspace,很容易死锁,可以选择只刷新资源所在的项目

有几篇文章值得参考:
http://www.blogjava.net/pauliz/archive/2009/11/13/302162.html
http://docs.jboss.org/tools/2.1.0.Beta1/hibernatetools/html_single/#plugins
http://xuliangyong.iteye.com/blog/148136
http://windforwings.blogspot.com/2008/11/customizing-code-generation-in.html(需翻墙,已转贴在下面,包括附件)

Customizing Code Generation in Hibernate
Labels: Java, Language, Technology

Hibernate includes set of tools that can generate Java code from hbm xml definitions. The code generation is rather less documented, and it is not straight forward to customize the code generation. While implementing hibernate in one of my projects, we needed to do just that - customize the code generation. Though many propose to use hibernate annotations instead of hbm xml files, we were not quite ready for that. Since during the design and development phase, most of our database structure was in flux, I felt more comfortable generating the bulk of our code rather than writing them by hand - less errors! So I took a bit of time to delve into hibernate tools and here's what we did...

Hibernate tools (hibernate-tools.jar) uses freemarker templates for code generation. The templates are located inside the jar file. We extracted the hibernate-tools.jar contents into a folder. Among other things there were two folders containing the templates used for code generation - pojo (to generate pojo classes) and dao (to generate the data access objects).

Our intention was to modify the code generation to:

   1. Incorporate logging using log4j logger in the DAO methods.
   2. Externalize the creation of hibernate session as our application wants to control the session scope. We would not use the session factory method of DAO.
   3. The DAO contains many utility methods to fetch, persist and search data (finder methods). Since we externalized the hibernate session, all the utility methods in our DAO can now become static. This will make it easier for our application to use the utility methods without instantiating an object every time.
   4. We would like to add finder methods for arbitrary properties as specified in the hbm, apart from the finders based on id and natural id into the DAO as static methods, instead of generating it with the POJO.
   5. For ease of use our application would sometimes like to instantiate the DAO object and treat it as the POJO object while accessing the static methods as a DAO. This is not really a 'required' feature (and is against the design principles) and can be skipped though.
   6. While generating POJO classes, we would like to limit the extra class code specified in hbm files to the parent class and not generate them in the component classes.
   7. Add setter and getter validations to the property values in POJOs.
   8. Since we added setter and getter validations, we would like to use them in class constructors as well. So we need to modify the class constructors to call the setter methods instead of directly assigning values to fields.


Now here's how we went about changing the templates (each item below corresponds to the objectives above) ...

   1. Modified dao/daohome.ftl and introduced a static variable named log which is an instance of org.apache.log4j.Logger initialized with the name of the DAO class.
   2. Modified all utility methods in dao/daohome.ftl and included a method parameter of type org.hibernate.Session and named _session. Inside the methods, we used _session instead of a call to sessionFactory.getCurrentSession(). We also removed the getSessionFactory method which was not used anymore.
   3. Modified dao/daohome.ftl and made all utility methods static.
   4. Created a new ftl in dao folder named PojoFinderMethods.ftl and included it in dao/daohome.ftl inside the class declaration. In the PojoFinderMethods.ftl, we examined each property declaration and if a finder attribute was declared, generated a method with name as the attribute value that fired a hbm query to match the property to a passed argument.
   5. Modified dao/daohome.ftl and added extends clause to the class declaration.
   6. Modified pojo/Pojo.ftl and included a condition !pojo.isComponent() around the line that included PojoExtraClassCode.ftl.
   7. Modified pojo/PojoPropertyAccessors.ftl to look for "pre-cond" and "post-cond" attributes in the hbm specification. We expect java code in the attribute values which we plonk into the output. In cases where we used any external validators (like apache validator framework) we included them as additional imports in the hbm.
   8. Modified pojo/PojoConstructors.ftl and changed the direct assignments to call the setter methods.


Once these were done, we modified our build.xml to point the template directory to the modified templates.


<hibernatetool destdir="${sourcedir}" templatepath="${hbmtemplatedir}">
   ...
<configuration configurationfile="...hibernate.cfg.xml"/>
<hbm2java jdk5="true" ejb3="false"/>
<hbm2dao/>
</hibernatetool>
分享到:
评论

相关推荐

    Hibernate Tools使用指南

    在Eclipse中,右键选择项目,依次点击“Hibernate” -&gt; “Generate Hibernate Artifacts”,然后选择相应的模板进行代码生成。 **5. 数据库同步** 当数据库表结构发生变化时,Hibernate Tools的数据库同步功能可以...

    hibernateTools3.3

    hibernateTools3.3还允许用户自定义模板,以满足特定的代码风格和项目需求。例如,可以定制实体类的生成模板,包含特定的注释、序列化实现或自定义构造函数。 总之,hibernateTools3.3作为Hibernate的强力助手,能...

    HIbernate-tools修改记录

    2. 自定义模板:利用Hibernate-tools的模板引擎,开发者可以定制自己的代码生成规则,以满足特定的编码风格和项目需求。 3. 版本选择:根据项目的具体需求和依赖环境,选择合适的Hibernate-tools版本,避免版本不...

    Eclipes里HibernateTools的使用

    - 如果你有自定义模板需求,可以勾选“Use custom templates(for custom file generation)”,否则保持不选。 3. **导出Java代码** - 在“Exporters”面板中,确保“General settings”下的两个选项(生成Java...

    Hibernate-tools解决hbm.xml中文注释乱码和生成实体类注释

    `Hibernate-tools`允许开发者自定义模板,以满足特定的代码风格和注释需求。例如,你可能希望实体类的每个字段注释包含字段长度、是否为主键等信息。这可以通过编写Freemarker或Velocity模板文件实现,然后在配置...

    Hibernate tool 源代码

    总的来说,研究Hibernate Tool的源代码,不仅可以掌握Eclipse Plugin的开发技术,也能深化对Hibernate ORM的理解,同时还能学习到代码生成和模板引擎的实战经验。这是一次宝贵的旅程,有助于提升Java开发者的技能...

    代码自动生成工具 hibernate配置文件工具 MagicalTools

    除了基本的代码生成,MagicalTools还提供了定制化选项,允许开发者自定义生成模板,满足特定项目需求。例如,可以在生成的DAO类中添加自定义的方法,或者调整实体类的命名规则。 此外,MagicalTools不仅仅是一个...

    hibernate实体生成工具

    - **可定制化**:多数实体生成工具允许用户自定义生成模板,比如字段命名规则、注释等。 - **兼容性**:工具通常支持多种数据库,如MySQL、Oracle、PostgreSQL等。 - **版本控制**:随着数据库表结构的修改,工具也...

    java根据实体类生成Hibernate映射文件

    此外,对于大型项目,通常会使用代码生成工具(如MyBatis的MyBatis Generator或Hibernate的Hibernate Tools)来自动化这个过程,它们提供了更全面的功能和配置选项。 总的来说,通过编写Java程序自动根据实体类生成...

    day37 08-Hibernate的反向工程

    这通常涉及到在IDE中创建一个新的Hibernate逆向工程任务,选择数据库连接,指定要生成代码的目录,以及自定义生成模板等。 4. 实体类与映射文件:解析生成的实体类和映射文件,理解它们之间的关系。实体类代表...

    代码自动生成_连接数据库生成entity(可自定义)_mysql

    在实际应用中,开发者可能会使用诸如MyBatis Generator、Hibernate Tools或一些IDE内置的插件来实现这个功能。这些工具通常提供了丰富的配置选项,可以根据项目需求进行调整。此外,生成的代码通常需要进行一些手工...

    读取数据库表结构生成自定义代码.zip

    对于Java开发者,可能会使用Hibernate的`hibernate-tools`来反向工程数据库生成实体类和映射文件。 在实际应用中,这种自动化代码生成的场景常见于ORM(对象关系映射)框架。ORM允许开发者用面向对象的方式来操作...

    ssh(structs,spring,hibernate)框架中的上传下载

     您可以使用Hibernate Middlegen、HIbernate Tools、Hibernate Syhchronizer等工具或手工的方式,编写Hibernate的领域对象和映射文件。其中对应T_FILE表的领域对象Tfile.java为:  代码 1 领域对象Tfile 1. ...

    mybatis-generator.zip

    相比于Hibernate的Hibernate Tools,MyBatis Generator更专注于MyBatis框架,生成的代码更加简洁,更便于理解和调试。而Hibernate Tools则更全面,支持多种ORM框架。 8. **集成到构建工具** MBG可以轻松地集成到...

    java之表反向生成类

    在Java中,有许多开源工具可以实现这一功能,如Hibernate的`hibernate-tools`模块、MyBatis的`mybatis-generator`、Apache的`DBUtils`等。它们提供了API或命令行工具,根据数据库元数据生成Java实体类、Mapper接口...

    代码生成工具语法(DOC)

    常见的代码生成工具有很多,例如MyBatis的MyBatis Generator、Hibernate的Hibernate Tools等,它们通常支持自定义模板语言,允许用户通过特定的语法来定义代码生成的逻辑。 在提到的文档中,特别提到了一种名为...

    PublicCMSV1.0使用手册_20151012.docx

    - 自定义指令:FreeMarker模板引擎允许用户创建自己的指令,增强模板语言的灵活性和扩展性。 - 0关联查询:系统设计时尽量避免数据库中的关联查询,以优化性能。 - 0xml配置:减少XML配置文件,使得系统更加简洁...

Global site tag (gtag.js) - Google Analytics