2017-05-31 136 views
0

我有一個簡單的彈簧啓動應用程序與H2內存數據庫,並試圖initalize數據。爲什麼Hibernate不初始化數據?

這是我application.config:

#Database connection 
spring.datasource.url=jdbc:h2:mem:practice_db 
spring.datasource.username=sa 
spring.datasource.password=123456 
spring.datasource.driverClassName=org.h2.Driver 

#spring.datasource.initialize=true 
#spring.datasource.schema=schema.sql 
#spring.datasource.data=schema.sql 


#Hibernate configuration 
#spring.jpa.hibernate.ddl-auto = none 

schema.sql文件

CREATE TABLE IF NOT EXISTS `Person` (
    `id`   INTEGER PRIMARY KEY AUTO_INCREMENT, 
    `first_name` VARCHAR(50) NOT NULL, 
    `age`  INTEGER NOT NULL 
); 

INSERT INTO `Person` (`first_name`, `age`) VALUES ('Peter', 20); 

和data.sql

INSERT INTO `Person` (`first_name`, `age`) VALUES ('John', 20); 

這是我的實體

/** 
* Dummy data model. 
* Just for test 
*/ 
@Entity 
@Table(name = "Person") 
public class Person { 

    @Id 
    @GeneratedValue 
    @Column(name = "Id") 
    private Long id; 

    /** 
    * For hibernate 
    */ 
    @Version 
    private Integer version; 

    /** 
    * Person name 
    */ 
    @Basic(optional = false) 
    @Column(name = "first_name") 
    private String name; 

    /** 
    * Age. Primitive type can't be nullable 
    */ 
    @Basic(optional = false) 
    @Column(name = "age") 
    private int age; 

表'人'成功創建。但它沒有數據,雖然腳本已執行:

09:06:29.711 5823 [main] INFO o.s.j.datasource.init.ScriptUtils - Executing SQL script from URL [file:/C:/Users/User/Desktop/students/materials/practice/target/classes/schema.sql] 
09:06:29.732 5844 [main] INFO o.s.j.datasource.init.ScriptUtils - Executed SQL script from URL [file:/C:/Users/User/Desktop/students/materials/practice/target/classes/schema.sql] in 21 ms. 
09:06:29.742 5854 [main] INFO o.s.j.datasource.init.ScriptUtils - Executing SQL script from URL [file:/C:/Users/User/Desktop/students/materials/practice/target/classes/data.sql] 
09:06:29.748 5860 [main] INFO o.s.j.datasource.init.ScriptUtils - Executed SQL script from URL [file:/C:/Users/User/Desktop/students/materials/practice/target/classes/data.sql] in 1 ms. 
09:06:29.915 6027 [main] INFO o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'default' 
09:06:29.949 6061 [main] INFO o.h.jpa.internal.util.LogHelper - HHH000204: Processing PersistenceUnitInfo [ 

當應用程序正在運行時,從該表中寫入和讀取數據時工作正常。

如何初始化應用程序啓動數據?

+0

可能的重複https://stackoverflow.com/questions/38040572/spring-boot-loading-initial-data – Afridi

+0

@Afridi我沒有PersonRepository。我用EntityManager.persist將數據保存在應用程序中。 g00glen00b sais他需要使用INSERT語句的data.sql文件。我有這樣的文件,但由於某種原因它不起作用 – Kirill

回答

2

根據Spring文檔,休眠具有以下默認值:

spring.jpa.hibernate.ddl-auto= # DDL mode。這實際上是hibernate.hbm2ddl.auto屬性的快捷方式。使用嵌入式數據庫時默認爲create-drop,否則爲「無」。

因此,對於嵌入式數據庫的SQL腳本啓動,然後休眠下降的數據庫,並重新創建模式,根據您的模型。因此,您不再看到插入。

作爲解決方案,您可以在application.properties中設置spring.jpa.hibernate.ddl-auto=none

+0

DML和DDL操作都正確執行(使用Spring JDBC),但在此之後,Hibernate將刪除模式並啓動它自己的DDL操作。因此,原始DML記錄丟失。 –

+0

謝謝!有用! – Kirill