我使用Spring JPA和Hibernate作为持久性提供程序,使用Derby作为数据库。 (具体来说,jar版本是Spring版本4.3.4,Hibernate Entity Manager版本5.2.10和Derby 10.13.1.1)。
我想检查
的正确方法(请注意,这不是Spring Boot应用程序。)
我的应用程序配置文件是:
<context:annotation-config />
<context:component-scan base-package="org.service"/>
<context:component-scan base-package="org.repository"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan">
<array>
<value>org.model</value>
<value>org.repository</value>
<value>org.service</value>
</array>
</property>
<property name="dataSource" ref="dataSource"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyTenSevenDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider"></bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource"/>
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="url" value="jdbc:derby:C:\\tmp\\testdb"/>
<property name="username" value="test"/>
<property name="password" value="test"/>
</bean>
<jpa:repositories base-package="org.repository"></jpa:repositories>
对于步骤(1),我的调查和测试表明可以在jpaProperties属性中设置持久性模式生成,如下所示:
<prop key="javax.persistence.schema-generation.scripts.action">create</prop>
<prop key="javax.persistence.schema-generation.scripts.create-target">create.sql</prop>
这将生成一个create.sql文件。对于步骤(2),我使用了
<prop key="javax.persistence.schema-generation.create-source">script</prop>
<prop key="javax.persistence.schema-generation.create-script-source">create.sql</prop>
<prop key="javax.persistence.schema-generation.database.action">create</prop>
但是我还需要在URL jdbc:derby:C:\ tmp \ testdb中设置create = true吗? javax.persistence.schema-generation的上述行是否假设数据库已创建?