如何获得在spring / hibernate / jpa中自动创建表的工作? [英] How to get automatic table creation working in spring / hibernate / jpa?
本文介绍了如何获得在spring / hibernate / jpa中自动创建表的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我的配置文件:
<?xml version =1.0encoding =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 =naveroTest>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< class> xxx< / class>
...
<属性>
< property name =hibernate.archive.autodetectionvalue =class,hbm/>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.connection.driver_classvalue =org.hsqldb.jdbcDriver/>
< property name =hibernate.connection.urlvalue =jdbc:hsqldb:file:/ tmp / naveroTestDB/>
< property name =hibernate.connection.usernamevalue =sa/>
< property name =hibernate.connection.passwordvalue =/>
< property name =hibernate.hbm2ddl.autovalue =create/>
< property name =hibernate.dialectvalue =org.hibernate.dialect.HSQLDialect/>
< / properties>
< / persistence-unit>
< /余辉>
context.xml
<?xml version =1.0encoding =UTF-8?>
< beans xmlns:tx =http://www.springframework.org/schema/tx
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
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd\">
<! - 用于自动创建表格 - >
< bean id =dataSourceclass =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassNamevalue =org.hsqldb.jdbcDriver/>
< property name =urlvalue =jdbc:hsqldb:file:/ tmp / naveroTestDB/>
< property name =usernamevalue =sa/>
< property name =passwordvalue =/>
< / bean>
< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =loadTimeWeaver>
< bean class =org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver/>
< / property>
< property name =jpaVendorAdapter>
< bean id =jpaAdapterclass =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
< property name =generateDdlvalue =true/>
< property name =databasePlatformvalue =org.hibernate.dialect.HSQLDialect/>
< property name =showSqlvalue =true/>
< / bean>
< / property>
< / bean>
< bean id =PictureBeanclass =de.navero.server.bl.PictureBean>
< property name =entityManagerFactory>< ref local =entityManagerFactory/>< / property>
< / bean>
< / beans>
任何想法可能是错误的?
感谢您的帮助:)。
解决方案
请尝试以下操作:
<?xml version =1.0encoding =UTF-8?>
< persistence version =1.0xmlns =http://java.sun.com/xml/ns/
xmlns:xsi =http://www.w3.org/2001/ XMLSchema实例
xsi:schemaLocation =http://.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd >
< persistence-unit name =naveroTesttransaction-type =RESOURCE_LOCAL/>
< / persistence>
<?xml version =1.0encoding =UTF-8?>
< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =jpaVendorAdapter>
< bean class =org。 springframework.orm.jpa.vendor.HibernateJpaVendorAdapter/>
< / property>
<属性名称=jpaProperties>
<道具>
..
< prop key>" hibernate.hbm2ddl.auto>> create-drop< / prop>
< / props>
< / property>
< PR operty name =dataSourceref =dataSource/>
这适用于我。
I can't get automatic table creation working in spring when using hibernate / jpa.
Here are my config files:
<?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="naveroTest">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>xxx</class>
...
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:file:/tmp/naveroTestDB"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
</properties>
</persistence-unit>
</persistence>
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd">
<!-- For auto creation of tables -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:file:/tmp/naveroTestDB" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
<property name="jpaVendorAdapter">
<bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<bean id="PictureBean" class="de.navero.server.bl.PictureBean">
<property name="entityManagerFactory"><ref local="entityManagerFactory" /></property>
</bean>
</beans>
Any ideas what may be wrong? Thanks for any help :).
解决方案
try the following:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="naveroTest" transaction-type="RESOURCE_LOCAL" />
</persistence>
<?xml version="1.0" encoding="UTF-8"?>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
...
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
</props>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
This works for me.
这篇关于如何获得在spring / hibernate / jpa中自动创建表的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文