Posted on 2009-02-16 21:08
啥都写点 阅读(1866)
评论(0) 编辑 收藏 所属分类:
J2EE
在persistence.xml中加上<property name="hibernate.hbm2ddl.auto"
value="update" />或设置在Spring配置中,在Spring 的sessionFactory中设置,当运行Spring时会自动生成Table
<!--applicationContext.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
xmlns:tx="http://www.springframework.org/schema/tx">
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property
name="persistenceUnitName" value="onlinephotoPU" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property
name="entityManagerFactory"
ref="entityManagerFactory" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property
name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/photo" />
<property name="username"
value="root" />
<property name="password"
value="root" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource"
ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.tst.Person</value>
</list>
</property>
<property
name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop
key="hibernate.hbm2ddl.auto">create-drop</prop>
</props>
</property>
</bean>
<tx:annotation-driven
transaction-manager="transactionManager" />
<bean id="PersonDAO"
class="com.tst.PersonDAO">
<property
name="entityManagerFactory"
ref="entityManagerFactory" />
</bean>
</beans>
<!--persistence.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="onlinephotoPU"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.tst.Person</class>
<properties>
<property
name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver" />
<property
name="hibernate.connection.url"
value="jdbc:mysql://127.0.0.1:3306/photo" />
<property
name="hibernate.connection.username" value="root" />
<property
name="hibernate.connection.password" value="root" />
<property
name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
-- 学海无涯