spring+hibernate自动生成数据库表结构

时间:2022-07-03 21:43:33
    现在越来越多的java项目采用java EE开发,spring+hibernate+stuts或者spring+hibernate+jsp的模式越来越常见,于是,更多的是spring来整合和管理hibernate,而不是hibernate独立配置。在项目开发过程中,很多童鞋可能都会经常遇到进行项目移植的情况。这样数据库移植的问题就成了一个令人头疼的问题。也许你说可以从数据库导出表结构,然后再建立新的数据库,而且博主以前也是这么做的,但是当你实施起来的时候,往往会出现种种问题,比如数据类型不匹配,导出数据结构时候类型出差错等等一系列很头疼问题。
其实,当你已经逆向工程生成hibernate持久化类以后,完全可以利用hibernate将持久化类再转回到相应的数据库表。但是特别注意,hibernate只能给你生成表,不能生成数据库。此话的意思在于提醒各位博友,在利用hibernate自动生成数据库表之前,必须先建立相应的空的数据库。当然,如果你已经拥有了数据库,并且数据库中已经有表结构了,那么hibernate可以对这些表执行更新操作。
 下面就介绍这些操作,其实非常的简单,就是对applicationContext.xml(spring的配置文档)中的sessionFactory(Bean)的配置问题。sessionFactory的具体介绍参见http://blog.csdn.net/dreamrealised/article/details/9126389
前期工作需要spring整合hibernate,需要在applicationContext文档中集成hibernate,即添加sessionFactory的bean,并且将hibernate的持久化对象写入applicationContext文档。如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-lazy-init="true" default-autowire="byName">

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>WEB-INF/jdbc.properties</value>
</list>
</property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="poolPreparedStatements" value="true" />
<property name="defaultAutoCommit" value="true" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties">
<value> hibernate.dialect=${hibernate.dialect}
hibernate.query.substitutions=true
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
</value>
</property>
<property name="annotatedClasses">
<list>
<value>nju.software.xkxt.data.dataobject.UserDO</value>
</property>
</bean>
</beans>
这样只需要配置sessionFactory,在hibernateProperties中设置hibernate.hbm2dll.auto设置为update,如下。这样,sessionFactory启动的时候会去检查schema是否一致,如果不一致会做scheme更新。在该项目中,由于具有持久化类UserDO,则当sessionFactory启动的时候,如果项目数据库中没有user表,则在数据库中添加user表,如果存在user表,则就更新user表结构,使user表跟UserDO持久化类定义的结构相同。
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
hibernate.query.substitutions=true
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
</value>
</property>
<property name="annotatedClasses">
<list>
<value>nju.software.xkxt.data.dataobject.UserDO</value>
</list>
</property>
</bean>