MyEclipse hibernate Use custom templates 总结
首先,在MyEclipse安装目录 myeclipse/eclipse/plugins/com.genuitec.myeclipse.doc_5.5.0/html/quickstarts/hibernate(myeclipse5.5)
下找到myeclipse_templates_5.5GA.zip,
复制到一个地方解压。读一下里面的readme.txt
MyEclipse Reverse-Engineering Templates
Version: 5.5GA
Minimum required MyEclipse version: 5.5GA
Using custom templates with the MyEclipse Reverse-Engineering Process:
1) Unzip this archive into a convenient location. We recommend using a project
in your Eclipse workspace.
2) Edit the velocity templates you wish to customize.
Note: POJO templates are present in the pojo folder
DAO template in the dao folder
HBM templates in the hbm folder
3) Optional: Though not necessary, it is recommended that you delete the
velocity templates you do not plan to customize. This will prevent future
confusion over which templates are being processed by MyEclipse.
4) Invoke the JPA/EJB3/Hibernate Reverse-Engineering wizard and select
"Use custom templates". Manually enter or click the browse button to select the
myeclipse_templates_5.5GA folder.
Warning: Do *not* select the dao, hbm or pojo folder, please select the
parent myeclipse_templates_5.5GA folder only.
During the code generation process, MyEclipse gives precedence to user
defined templates by searching the custom template location prior to
consulting the internal templates shipped with MyEclipse.
MyEclipse反向工程模板
版本: 5.5GA
最低要求MyEclipse版本: 5.5GA
使用自定义模板的MyEclipse逆向工程过程:
1 )解压缩这个档案到一个方便的位置。我们建议在您的Eclipse工程文件夹下使用一个新的项目 。
2 )编辑velocity模板根据你的要求。
注:POJO的模板在pojo 文件夹中。
DAO的模板在dao文件夹中。
HBM的模板在hbm文件夹中。
3 )可选:虽然没有必要,建议您删除 你不打算定制velocity模板。这将防止未来 MyEclipse处理时的模板混乱。
4 )调用JPA/EJB3/Hibernate逆向工程向导,并选择 “使用自定义模板” 。手动输入或点击浏览按钮来选择 myeclipse_templates_5.5GA文件夹路径。
警告:请不要选择道,dao, hbm 或pojo文件夹,请选择上一级myeclipse_templates_5.5GA文件夹。
在代码生成过程中, MyEclipse优先考虑用户定义的模板,然后才是查找MyEclipse内部模板。
我只是修改了一下daohome.vm文件,在里面增加了一删除的方法。
Velocity 代码:
public int deleteByProperty(String propertyName, Object value) {
// log.debug("finding ${declarationName} instance with property: " + propertyName + ", value: " + value);
$pojo.importType("org.hibernate.Transaction") ts = null;
int dels = 0;
try {
String queryString = "delete from ${declarationName} as model where model."
+ propertyName + "= ?";
#if($hibernateDaoSupport)
return ${currSession}.find(queryString, value);
#else
$pojo.importType("org.hibernate.Query") queryObject = ${currSession}.createQuery(queryString);
queryObject.setParameter(0, value);
dels = queryObject.executeUpdate();
ts.commit();
#end
} catch (RuntimeException re) {
if (ts!=null) ts.rollback();
// log.error("find by property name failed", re);
throw re;
}
return dels;
}
生成的java代码:
public int deleteByProperty(String propertyName, Object value) {
// log.debug("finding Archive instance with property: " + propertyName +
// ", value: " + value);
Transaction ts = null;
int dels = 0;
try {
String queryString = "delete from House as model where model."
+ propertyName + "= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
dels = queryObject.executeUpdate();
ts.commit();
} catch (RuntimeException re) {
if (ts != null)
ts.rollback();
// log.error("find by property name failed", re);
throw re;
}
return dels;
}