最近准备把进销存项目激活,这样一方面可以让更多的人有机会参与到开源开发中来,另一方面也把sql翻译器、sql优化器、jdbmonitor应用到这个项目中,这样这三个基础模块就可以在实际项目应用中得到验证和增强。
我准备用hibernate实现持久层,于是到hibernate的网站上把hibernate3下载下来,看了看有个hibernatetools,是个hibernate在eclipse下的辅助工具,也down了下来,用了用感觉不错。可以直接从数据库表生成pojo和hbm.xml(其实我不是很喜欢这种开发方式,我更喜欢我用建模工具来写pojo,然后用工具生成hbm.xml和ddl,不过好像hibernate3现在还没有这种工具,如果哪位朋友知道有这种工具,希望赐教)。
美中不足的是它生成的javabean的字段名是完全和数据库字段名一致、生成的javabean的类名是完全和数据库表名一致。出于清晰以及可移植的考虑(也是公司的开发规范养成的习惯),我设计的表名全部以"t_"开头,中间再加上子系统名,最后才是表意的表名,比如用“t_ps_bom”表示生产管理系统中的物料清单表;字段名全部以“f”开头,比如fid,fname。
这样就导致生成了如下的javabean:
public class tpsbom
{
.......
public string getfid()
...
public string getfnumber()
}
看起来很不直观。我刚刚想放弃这个工具,想了想,“拿来就用,不好用就换”可不是做开源人该有的精神呀。钻研一下。
看看了hibernate code generation页签中有一个“reveng strategy”,什么意思?“反向工程策略”??好像有门儿,点击“browse”弹出一个类选择对话框,竟然看到了它默认显示的“defaultreverseengineeringstrategy”类了,我在hibernatetools的安装目录找来找去,终于在plugins\org.hibernate.eclipse_3.2.0.beta6\lib\tools下的hibernate-tools.jar中找到了这个类的影子,用反编译工具反编译一下(懒得去网上下源码了,呵呵)。一个个方法名展现在我面前:
tabletoclassname
columntopropertyname
columntohibernatetypename
。。。
这不就是在把数据库相应的项映射成java相应的项吗?
开工!
新建一个类cownewreverseengineeringstrategy,继承自defaultreverseengineeringstrategy,override tabletoclassname、
columntopropertyname这两个方法,在这两个方法中写入自己的转换逻辑。
然后打包成jar包,放到plugins\org.hibernate.eclipse_3.2.0.beta6\lib\tools下,然后在plugins\org.hibernate.eclipse_3.2.0.beta6\lib\tools\manifest.mf中把这个新增包的内容加上,关闭eclipse,加个-clean参数启动eclipse,然后点击“hibernate code generation”,把“reveng strategy”填成“com.cownew.devtools.hibtools.reveng.cownewreverseengineeringstrategy”,“run”!!!
晕倒,竟然报错“com.cownew.devtools.hibtools.reveng.cownewreverseengineeringstrategy
exception while generating code
reason org.hibernate.console.hibernateconsoleruntimeexception:could not create or find com. with one argument deleate constructor”
看来是反射调用的时候出了问题,重新打开hibernate-tools.jar,仔细观察,竟然发现了一个delegatingreverseengineeringstrategy,它多 了一个参数为“reverseengineeringstrategy delegate”的构造函数,而其他调用都是转发给reverseengineeringstrategy了,晕倒,搞不懂它在做什么,也没时间研究了,给cownewreverseengineeringstrategy也曾街一个参数为“reverseengineeringstrategy delegate”的构造函数,重新打包,重新启动eclipse,哈哈,一切搞定,终于生成我可爱的,
public class personinfo
{
public string getnumber()。。。
public string getid()。。。
}
了。
附全部代码:
package com.cownew.devtools.hibtools.reveng;
import java.beans.introspector;
import org.hibernate.cfg.reveng.defaultreverseengineeringstrategy;
import org.hibernate.cfg.reveng.reverseengineeringsettings;
import org.hibernate.cfg.reveng.reverseengineeringstrategy;
import org.hibernate.cfg.reveng.reverseengineeringstrategyutil;
import org.hibernate.cfg.reveng.tableidentifier;
import org.hibernate.util.stringhelper;
public class cownewreverseengineeringstrategy extends
defaultreverseengineeringstrategy
{
public cownewreverseengineeringstrategy(reverseengineeringstrategy delegate)
{
super();
}
private reverseengineeringsettings settings = new reverseengineeringsettings();
public string tabletoclassname(tableidentifier table)
{
string tablename = table.getname();
if (tablename != null && tablename.touppercase().startswith("t_"))
{
string pkgname = settings.getdefaultpackagename();
int lastindex = tablename.lastindexof('_');
tablename = tablename.substring(lastindex + 1, tablename.length())
+ "info";
string classname = touppercamelcase(tablename);
if (pkgname.length() > 0)
return stringhelper.qualify(pkgname, classname);
return classname;
} else
{
return super.tabletoclassname(table);
}
};
public string columntopropertyname(tableidentifier table, string column)
{
if (column != null && column.touppercase().startswith("f"))
{
string cownewcolname = column.substring(1, column.length());
string decapitalize = introspector
.decapitalize(touppercamelcase(cownewcolname));
return keywordcheck(decapitalize);
} else
{
return super.columntopropertyname(table, column);
}
}
private string keywordcheck(string possiblekeyword)
{
if (reverseengineeringstrategyutil
.isreservedjavakeyword(possiblekeyword))
possiblekeyword += "_";
return possiblekeyword;
}
public void setsettings(reverseengineeringsettings settings)
{
super.setsettings(settings);
this.settings = settings;
}
public static void main(string[] args)
{
tableidentifier table = new tableidentifier("t_bd_person");
//tableidentifier table = new tableidentifier("t_person");
//tableidentifier table = new tableidentifier("person");
cownewreverseengineeringstrategy reveng = new cownewreverseengineeringstrategy(null);
string classname = reveng.tabletoclassname(table);
system.out.println(classname);
system.out.println(reveng.columntopropertyname(table, "fid"));
system.out.println(reveng.columntopropertyname(table, "id"));
}
}
闽公网安备 35060202000074号