Hibernate Tools 自定义模板

1,默认使用的模板文件位置

eclipse home/plugins/org.jboss.tools.hibernate3_6_3.7.0.Beta2-v20130614-1624-B67.jar  将此jar包解开,\lib\tools\hibernate-tools-3.6.0.CR1

全路径:eclipse home\plugins\org.jboss.tools.hibernate3_6_3.7.0.Beta2-v20130614-1624-B67\lib\tools\hibernate-tools-3.6.0.CR1

 

2,将此目录COPY出来,保持目录结构,修改相应目录下的ftl文件即可,主要修改dao\daohome.ftl

 

3,使用方式,在Hibernate Code Generation 的时候选择 use custom templates , 定位到step 2的目录即可。

 

4,注意,当使用sessionFactory.getCurrentSession()获取session时,需要在Hibernate配置文件中增加 <property name="hibernate.current_session_context_class">thread</property>

 

5,注意,Hibernate配置文件中不能给session-factory命名,如<session-factory name="xxxxx"> 这样会报错,保持<session-factory> ,不要名字

 

一个修改了SessionFactory获取方式,增加了insert和批量insert以及事务处理的daohome.ftl如下:

 

${pojo.getPackageDeclaration()}
// Generated ${date} by Hibernate Tools ${version}

<#assign classbody>
<#assign declarationName = pojo.importType(pojo.getDeclarationName())>/**
 * Home object for domain model class ${declarationName}.
 * @see ${pojo.getQualifiedDeclarationName()}
 * @author Hibernate Tools
 */
<#if ejb3>
@${pojo.importType("javax.ejb.Stateless")}
</#if>

public class ${declarationName}Home {

    private static final ${pojo.importType("org.apache.commons.logging.Log")} log = ${pojo.importType("org.apache.commons.logging.LogFactory")}.getLog(${pojo.getDeclarationName()}Home.class);

<#if ejb3>
    @${pojo.importType("javax.persistence.PersistenceContext")} private ${pojo.importType("javax.persistence.EntityManager")} entityManager;
    
    public void persist(${declarationName} transientInstance) {
        log.debug("persisting ${declarationName} instance");
        try {
            entityManager.persist(transientInstance);
            log.debug("persist successful");
        }
        catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }
    
    public void remove(${declarationName} persistentInstance) {
        log.debug("removing ${declarationName} instance");
        try {
            entityManager.remove(persistentInstance);
            log.debug("remove successful");
        }
        catch (RuntimeException re) {
            log.error("remove failed", re);
            throw re;
        }
    }
    
    public ${declarationName} merge(${declarationName} detachedInstance) {
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值