Spring Boot/Spring Data import.sql 不运行 Spring-Boot-1.0.0.RC1 [英] Spring Boot / Spring Data import.sql doesn't run Spring-Boot-1.0.0.RC1
问题描述
我一直在关注 Spring Boot 的开发,有时在初始版本 0.0.5-BUILD-SNAPSHOT
和我使用的当前版本之间的某个时间 1.0.0.RC1
我不再运行我的 import.sql
脚本.
I've been following the development of Spring Boot, and sometime between the initial version 0.0.5-BUILD-SNAPSHOT
and the current version I am using 1.0.0.RC1
I am no longer running my import.sql
script.
这是我对 LocalContainerEntityManager
和 JpaVendorAdapter
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
lef.setDataSource(dataSource);
lef.setJpaVendorAdapter(jpaVendorAdapter);
lef.setPackagesToScan("foo.*");
return lef;
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql(true);
hibernateJpaVendorAdapter.setGenerateDdl(true);
hibernateJpaVendorAdapter.setDatabase(Database.POSTGRESQL);
return hibernateJpaVendorAdapter;
}
有趣的 hibernate.hbm2ddl.auto
似乎仍在运行,我认为这是我的 SpringBootServletInitializer
定义的一部分
Interesting the hibernate.hbm2ddl.auto
still seems to run, which I think is part of the definition of my SpringBootServletInitializer
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {
然而,我也注意到生成的表格不再有下划线并且在生成时改变了它们的形状?
However, I also noticed that the tables generated no longer have underscores and changed their shape when generated?
但是,这可能是更新我的 org.postgresql
版本的结果,如下所示:
However, that could be the result of updating my org.postgresql
version like so:
以前:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.2-1004-jdbc41</version>
</dependency>
现在:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3-1100-jdbc41</version>
</dependency>
我还必须将 pggetserialsequence
更改为 pg_get_serial_sequence
才能让脚本完全从 pgadmin
运行?
I also had to change pggetserialsequence
to pg_get_serial_sequence
to get the script to run at all from pgadmin
?
我想我对正在发生的事情感到困惑,但最重要的是我想回到让我的 import.sql
运行.
I guess I'm confusing what's going on, but most importantly I want to get back to having my import.sql
run.
I have been following the sample project: https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-jpa
他们的 import.sql
也没有在 1.0.0-BUILD-SNAPSHOT
推荐答案
import.sql
脚本是我认为的 Hibernate 功能(不是 Spring 或 Spring Boot).它必须在示例中运行,否则测试将失败,但无论如何它仅在 ddl-auto 设置为创建表时运行.使用 Spring Boot,您应该确保 spring.jpa.hibernate.ddl-auto
设置为create"或create-drop"(后者是嵌入式数据库的 Boot 中的默认值,但不是对于其他人,例如 postgres).
The import.sql
script is a Hibernate feature I think (not Spring or Spring Boot). It must be running in the sample otherwise the tests would fail, but in any case it only runs if ddl-auto is set to create the tables. With Spring Boot you should ensure that spring.jpa.hibernate.ddl-auto
is set to "create" or "create-drop" (the latter is the default in Boot for an embedded database, but not for others, e.g. postgres).
如果你想无条件地运行一个 SQL 脚本,如果你把它放在 classpath:schema.sql
(或 classpath:schema-<platform>.sql
其中
在您的情况下是postgres").
If you want to unconditionally run a SQL script, By default Spring Boot will run one independent of Hibernate settings if you put it in classpath:schema.sql
(or classpath:schema-<platform>.sql
where <platform>
is "postgres" in your case).
我认为您可以删除 JpaVendorAdapter
以及 LocalContainerEntityManagerFactoryBean
(除非您使用的是 persistence.xml
)并让 Boot 控制.可以使用 @EntityScan
注释(Spring Boot 中的新功能)设置要扫描的包.
I think you can probably delete the JpaVendorAdapter
and also the LocalContainerEntityManagerFactoryBean
(unless you are using persistence.xml
) and let Boot take control. The packages to scan can be set using an @EntityScan
annotation (new in Spring Boot).
Boot 1.0.0.RC1 中的默认表命名方案已更改(因此与您的 postgres 依赖项无关).我不确定 RC2 中的情况仍然如此,但无论如何你可以通过设置 spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
.
The default table naming scheme was changed in Boot 1.0.0.RC1 (so nothing to do with your postgres dependency). I'm not sure that will still be the case in RC2, but anyway you can go back to the old Hibernate defaults by setting spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
.
这篇关于Spring Boot/Spring Data import.sql 不运行 Spring-Boot-1.0.0.RC1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!