[英]Spring Boot / Spring Data import.sql doesn't run Spring-Boot-1.0.0.RC1
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. 我一直在关注Spring Boot的开发,有时在初始版本0.0.5-BUILD-SNAPSHOT
和我使用1.0.0.RC1
的当前版本之间。我不再运行我的import.sql
脚本。
Here is my configuration for LocalContainerEntityManager
and JpaVendorAdapter
这是我对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;
}
Interesting the hibernate.hbm2ddl.auto
still seems to run, which I think is part of the definition of my SpringBootServletInitializer
有趣的是hibernate.hbm2ddl.auto
似乎仍然运行,我认为这是我的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? 但是,我还注意到生成的表格不再有下划线并在生成时改变了它们的形状?
However, that could be the result of updating my org.postgresql
version like so: 但是,这可能是更新我的org.postgresql
版本的结果,如下所示:
Previously: 先前:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.2-1004-jdbc41</version>
</dependency>
Now: 现在:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3-1100-jdbc41</version>
</dependency>
I also had to change pggetserialsequence
to pg_get_serial_sequence
to get the script to run at all from pgadmin
? 我还必须将pggetserialsequence
更改为pg_get_serial_sequence
以使脚本从pgadmin
运行?
I guess I'm confusing what's going on, but most importantly I want to get back to having my import.sql
run. 我想我正在混淆发生了什么,但最重要的是我想回到我的import.sql
运行。
I have been following the sample project: https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-jpa 我一直在关注示例项目: https : //github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-jpa
And their import.sql
isn't running either on 1.0.0-BUILD-SNAPSHOT
并且他们的import.sql
也没有在1.0.0-BUILD-SNAPSHOT
上运行
The import.sql
script is a Hibernate feature I think (not Spring or Spring Boot). import.sql
脚本是我认为的Hibernate功能(不是Spring或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. 它必须在示例中运行,否则测试将失败,但无论如何只有在ddl-auto设置为创建表时它才会运行。 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, eg postgres). 使用Spring Boot,您应确保将spring.jpa.hibernate.ddl-auto
设置为“create”或“create-drop”(后者是Boot for Embedded数据库的默认设置,但不适用于其他数据库,例如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). 如果你要无条件地运行SQL脚本,默认情况下,春季启动,如果你把它在运行的休眠设置一个独立的classpath:schema.sql
(或classpath:schema-<platform>.sql
其中<platform>
是“Postgres的”在你的情况下)。
I think you can probably delete the JpaVendorAdapter
and also the LocalContainerEntityManagerFactoryBean
(unless you are using persistence.xml
) and let Boot take control. 我想您可以删除JpaVendorAdapter
以及LocalContainerEntityManagerFactoryBean
(除非您使用的是persistence.xml
)并让Boot获得控制权。 The packages to scan can be set using an @EntityScan
annotation (new in Spring Boot). 可以使用@EntityScan
注释(Spring Boot中的新增功能)设置要扫描的软件包。
The default table naming scheme was changed in Boot 1.0.0.RC1 (so nothing to do with your postgres dependency). Boot 1.0.0.RC1中更改了默认的表命名方案(因此与postgres依赖关系无关)。 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
. 我不确定在RC2中仍然会出现这种情况,但无论如何,你可以通过设置spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
来回到旧的Hibernate默认值。
Hey I came across similar issue. 嘿,我遇到过类似的问题。 My sql script was not getting invoked initially. 我的sql脚本最初没有被调用。 Then I tried renaming the file from "import.sql" to " schema.sql ", it worked. 然后我尝试将文件从“import.sql”重命名为“ schema.sql ”,它有效。 May be give this a shot. 可能会给这一点。 My code can be found here - https://github.com/sidnan/spring-batch-example 我的代码可以在这里找到 - https://github.com/sidnan/spring-batch-example
In addition to what was already said, it's worth noting you can use the data.sql file to import/intialize data into your tables. 除了已经说过的内容之外,值得注意的是,您可以使用data.sql文件将数据导入/初始化到表中。 Just put your data.sql into the root of the classpath (eg: if you're running a Spring Boot app, you put it in the src/main/resources path). 只需将data.sql放入类路径的根目录中(例如:如果您正在运行Spring Boot应用程序,则将其放在src / main / resources路径中)。
Like was said before, use it together with the property ddl-auto=create-drop , so that it won't crash trying to insert the existing data. 就像之前所说的那样,将它与属性ddl-auto = create-drop一起使用,这样它就不会在尝试插入现有数据时崩溃。
You can also set up which specific file to execute using the spring.datasource.data property. 您还可以使用spring.datasource.data属性设置要执行的特定文件。 Check out more info here: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html 在这里查看更多信息: http : //docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html
Note: the schema.sql mentioned before would contain the whole DB definition. 注意:之前提到的schema.sql将包含整个数据库定义。 If you want to use this, ensure that Hibernate doesn't try to construct the DB for you based on the Java Entities from your project. 如果要使用它,请确保Hibernate不会尝试根据项目中的Java实体为您构建数据库。 This is what de doc says: 这就是de doc所说的:
If you want to use the schema.sql initialization in a JPA app (with Hibernate) then ddl-auto=create-drop will lead to errors if Hibernate tries to create the same tables. 如果你想在JPA应用程序(使用Hibernate)中使用schema.sql初始化,那么如果Hibernate尝试创建相同的表,ddl-auto = create-drop将导致错误。 To avoid those errors set ddl-auto explicitly to "" (preferable) or "none" 为了避免这些错误,请将ddl-auto明确设置为“”(首选)或“无”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.