Hibernate正向工程hbm2ddl

一般在项目开发过程中,使用比较多的就是先建好表,再利用hibernate反向工程生成*.hbm.xml文件跟POJO类,个人认为由于目前所使用的数据库都是关系数据库,而hibernate作为一个ORM,把对数据库的操作都对象化了,更应当从对象出发,生成数据库里面相关表,这样更加符合人认知事物的习惯。

由于hibernate3提供了自带的工具hbm2ddl,建立根据你的对象建立数据库是一件非常简单的事情。

SchemaExport工具:Hibernate的hbm2dll提供SchemaExport工具,给定一个连接字符串和映射文件,不需输入其他东西就可以按照持久化类和映射文件自动生成数据库架构,现在SchemaExport工具还不是很强大,但是一般应用足够了,它还是一个相当原始的API还在不断改进。SchemaExport工具就是把DDL脚本输出到标准输出,同时/或者执行DDL语句。SchemaExport工具提供了三个方法,分别是Drop()、Create()、Execute(),前两个方法实质是调用Execute()方法。通常使用Execute()方法来生成数据库架构的。

SchemaUpdate工具:在Hibernate2.0中新添加SchemaUpdate工具,可以用来更新数据库架构。但是我觉得没有什么作用,因为它不能Drop现有的表或列,也不能更新现有的列,只能添加新的表和列。如果我需要删除表或者列或者修改其中列,SchemaUpdate工具就显得无能为力了。

SchemaValidator工具:SchemaValidator工具,可以用来检查数据库架构。通过映射文件中配置的相关数据库表及各自的字段属性来检查数据库中对应表结构是否存在或正确,如果不存在表或字段不一致,则抛出异常。

对于单纯的Hibernate,配置有hibernate.cfg.xml数据库配置文件,或是其它SSH整合版本中也配置有相关.cfg.xml文件的工程来说,这种类型的情况下使用Hibernate的SchemaExport实现正向工程比较简单如下例:

publicboolean createTableByBean(String mappingFile) {

Configuration cfg =new Configuration().configure(cfgFile);

cfg.addFile(mappingFile);

boolean flag =true;

SchemaExport dbExport =new SchemaExport(cfg);

try {

dbExport.create(true,true);

} catch (Exception e) {

flag =false;

}

return flag;

}

而我所做的数据迁移工具中使用的是Spring+Hibernate整合框架,而且在数据库配置中没有使用hibernate.properties和hibernate.cfg.xml文件来配置,

因为在使用Hibernate的SchemaExport/SchemaUpdate/SchemaValidator工具时,构建对象时需要Hibernate的Configuration对象实例作为参数。如:

var export = new SchemaExport(cfg); 其中cfg参数由

Configuration cfg = new Configuration().configure(cfgFile);建立,configure默认读取hibernate.cfg.xml配置文件(configure无参函数),

也可以通过传递参数指定自己定义的.cfg.xml文件。我的SessionFactory是借助于Spring提供的

org.springframework.orm.hibernate3.LocalSessionFactoryBean来实现的:

<beanid="targetSessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

……

</bean>

在没有配置.cfg.xml文件的情况下,可以通过配置的数据源的SessionFactory中获取,如下:

publicclass DaoUtilextends HibernateDaoSupport {

private Configurationcfg;

private Settingssettings;

publicvoid init() {

String webRootPath = CommonMethod.getWebRootPath();

ApplicationContext dataSource_ctx = new FileSystemXmlApplicationContext(webRootPath +"/WEB-INF/datatransferConf/applicationContext-targetDataSource.xml");

SessionFactoryImplementor sessionFactoryImpl = (SessionFactoryImplementor) dataSource_ctx.getBean("targetSessionFactory");

LocalSessionFactoryBean localSessionFactory = (LocalSessionFactoryBean) dataSource_ctx.getBean("&targetSessionFactory");

SessionFactory sessionFactory = (SessionFactory) dataSource_ctx.getBean("targetSessionFactory");

this.cfg =localSessionFactory.getConfiguration();

this.settings =sessionFactoryImpl.getSettings();

super.setSessionFactory(sessionFactory);

}

publicvoid createTableFromCfg() {

if (settings.isAutoCreateSchema()) {

new SchemaExport(cfg,settings).create(true,true);

} elseif (settings.isAutoUpdateSchema()) {

new SchemaUpdate(cfg,settings).execute(true,true);

} elseif (settings.isAutoDropSchema()) {

new SchemaExport(cfg,settings).drop(true,true);

} elseif (settings.isAutoValidateSchema()) {

new SchemaValidator(cfg).validate();

}

}

publicvoid createTableFromMapFile(String mappingFile) {

Resource mappingLocation = new ClassPathResource(mappingFile);

try {

cfg.addInputStream(mappingLocation.getInputStream());

} catch (MappingException e1) {

e1.printStackTrace();

} catch (IOException e1) {

e1.printStackTrace();

}

createTableFromCfg();

}

}

SchemaExport 的create(script,export)方法根据持久类和映射文件先删除架构后创建删除数据库架构。有两个参数,第一个为True就是把DDL语句输出到控制台,第二个为True就是根据持久类和映射文件先执行删除再执行创建操作,经过调试可以发现这个方法其实质是执行execute(script,export, false, true)方法。execute(script, export, justDrop, format)方法根据持久类和映射文件先删除架构后创建删除数据库架构。有四个参数,第一个为True就是把DDL语句输出到控制台;第二个为True就是根据持久类和映射文件在数据库中先执行删除再执行创建操作;第三个为false表示不是仅仅执行Drop语句还执行创建操作,这个参数的不同就扩展了上面两个方法;第四个参数为false表示不是格式化输出DDL语句到控制台,是在一行输出的。

Spring配置文件中所定义的LocalSessionFactoryBean实现了org.springframework.beans.factory.FactoryBean接口,在使用ApplicationContext对象读取的时候可以自动转型为多种不同类型的SessionFactory,spring在装配的时候, 如果发现实现了org.springframework.beans.factory.FactoryBean接口,就会使用

FactoryBean#getObject() 方法返回的对象装配,具体的可以看下文档。如果你想拿到LocalSessionFactoryBean实例, 在id前面加个'&'就可以了,在你的配置文件中BeanFactory.getBean('&sessionFactory')拿到的就是LocalSessionFactoryBean的实例。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows 2003 & 2008 & 2008 R2下自编程序FlexlmAu08-x86.exe破解Rational Rose2003&2007 Windows Server 2003 & 2008 & 2008R2下破解 Rational Rose Enterprise Edition 2003 Windows Server 2003 & 2008 & 2008R2下破解 Rational Rose Enterprise Edition 2007也同样可以 以前发布的FlexlmAu07.exe只能在64位环境下正常运行,在32位的Windows Server 2003运行报错。现在进行了修正,并基于x86重新编译。已经测试,新程序FlexlmAu08-x86.exe在32位和64位环境下都能正常运行,在Windows Server 2003下能够正常使用。 之所以还保留Windows Server 2003下运行Rational Rose Enterprise Edition 2007,原因如下: --------------------------------------- 这是IBM工程师的文章: 利用Rational Rose进行C++代码和数据库结构分析 http://www.ibm.com/developerworks/cn/rational/r-shenzj/index.html 一.Rational Rose逆向工程介绍二.如何用Rational Rose进行C++代码分析三.如何用Rational Rose进行数据库结构分析四.如何得到逆向工程的模型图五.总结注释参考资料 评论 2004年10月,IBM推出了支持最新的UML2.0的可视化建模工具 Rational Software Architect(见注释①)和IBM Rational Software Modeler(见注释②)。虽然它们支持在建模功能上有了更好的改进、支持了更新的标准,但是RSA的精彩功能主要是集中在对Java应用的支持,而IBM Rational Software Modeler则是主要关注系统的模型设计,如果要从结构上分析C++编写的系统的代码,Rational Rose还是首选的工具。 --------------------------------------- 另外已经证实: 在Windows Server 2003下面,Rose 2007能够正常完成VC++逆向工程 在Windows Server 2008R2下面,Rose 2007 VC++逆向工程报错: 20:46:12 Rational Rose 20:46:12 Updating Model for VC++ projects 20:46:13 Error: IDE support for VC++ cannot be instantiated 20:46:13 Update end 20:46:13 --- 20:46:13 1 error(s), 0 warning(s) --------------------------------------- 资料:

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值