我使用带有嵌入式H2数据库的Spring Data JPA。我有两个sql文件:
schema.sql
CREATE TABLE SINGER (
ID INT NOT NULL AUTO_INCREMENT,
FIRST_NAME VARCHAR(60) NOT NULL,
LAST_NAME VARCHAR(60) NOT NULL,
BIRTH_DATE DATE,
CONSTRAINT UQ_SINGER UNIQUE (FIRST_NAME, LAST_NAME),
PRIMARY KEY (ID)
);
CREATE TABLE ALBUM(
ID INT NOT NULL AUTO_INCREMENT,
SINGER_ID INT,
TITLE VARCHAR(100) NOT NULL,
RELEASE_DATE DATE,
CONSTRAINT UQ_ALBUM UNIQUE (TITLE),
CONSTRAINT FK_ALBUM FOREIGN KEY (SINGER_ID) REFERENCES SINGER (ID),
PRIMARY KEY (ID)
);
data.sql
INSERT INTO SINGER (FIRST_NAME, LAST_NAME, BIRTH_DATE) VALUES ('John', 'Mayer', '1977-10-16');
INSERT INTO SINGER (FIRST_NAME, LAST_NAME, BIRTH_DATE) VALUES ('Eric', 'Clapton', '1945-03-30');
INSERT INTO SINGER (FIRST_NAME, LAST_NAME, BIRTH_DATE) VALUES ('Jorn', 'Butler', '1975-04-01');
INSERT INTO ALBUM (SINGER_ID, TITLE, RELEASE_DATE) VALUES (1, 'The Search For Everything', '2017-01-20');
INSERT INTO ALBUM (SINGER_ID, TITLE, RELEASE_DATE) VALUES (1, 'Battle Studies', '2009-11-17');
INSERT INTO ALBUM (SINGER_ID, TITLE, RELEASE_DATE) VALUES (2, 'From The Cradle', '1994-09-13');
数据库配置:
@Bean
public DataSource dataSource() {
try {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.H2)
.addScripts("db/schema.sql", "db/data.sql")
.build();
} catch (Exception ex) {
log.error("Cannot create DataSource", ex);
return null;
}
}
问题是::执行schema.sql
时未执行data.sql
。所有测试(查找所有数据,按ID查找,插入,更新,删除)均成功通过
INFO [main] org.springframework.jdbc.datasource.init.ScriptUtils:510 - Executed SQL script from class path resource [db/schema.sql] in 47 ms.
INFO [main] org.springframework.jdbc.datasource.init.ScriptUtils:444 - Executing SQL script from class path resource [db/data.sql]
DEBUG [main] org.springframework.jdbc.datasource.init.ScriptUtils:476 - 1 returned as update count for SQL: INSERT INTO SINGER (FIRST_NAME, LAST_NAME, BIRTH_DATE) VALUES ('John', 'Mayer', '1977-10-16')
DEBUG [main] org.springframework.jdbc.datasource.init.ScriptUtils:476 - 1 returned as update count for SQL: INSERT INTO SINGER (FIRST_NAME, LAST_NAME, BIRTH_DATE) VALUES ('Eric', 'Clapton', '1945-03-30')
DEBUG [main] org.springframework.jdbc.datasource.init.ScriptUtils:476 - 1 returned as update count for SQL: INSERT INTO SINGER (FIRST_NAME, LAST_NAME, BIRTH_DATE) VALUES ('Jorn', 'Butler', '1975-04-01')
DEBUG [main] org.springframework.jdbc.datasource.init.ScriptUtils:476 - 1 returned as update count for SQL: INSERT INTO ALBUM (SINGER_ID, TITLE, RELEASE_DATE) VALUES (1, 'The Search For Everything', '2017-01-20')
DEBUG [main] org.springframework.jdbc.datasource.init.ScriptUtils:476 - 1 returned as update count for SQL: INSERT INTO ALBUM (SINGER_ID, TITLE, RELEASE_DATE) VALUES (1, 'Battle Studies', '2009-11-17')
DEBUG [main] org.springframework.jdbc.datasource.init.ScriptUtils:476 - 1 returned as update count for SQL: INSERT INTO ALBUM (SINGER_ID, TITLE, RELEASE_DATE) VALUES (2, 'From The Cradle', '1994-09-13')
INFO [main] org.springframework.jdbc.datasource.init.ScriptUtils:510 - Executed SQL script from class path resource [db/data.sql] in 16 ms.
DEBUG [main] org.springframework.jdbc.datasource.DataSourceUtils:340 - Returning JDBC Connection to DataSource
似乎data.sql
已成功执行,但是只要插入一些数据,我就看不到数据(在测试方法中)。
更新:我有以下测试方法(它们均成功通过,但是在testFindAll()中我看不到任何数据):
@Test
void testFindAll() {
List<Singer> singers = singerRepository.findAll();
assertNotNull(singers);
displayAllSingers(singers);
}
@Test
void testInsert() {
Singer singer = createSinger();
singerRepository.insert(singer);
assertNotNull(singer.getId());
displayAllSingers(singerRepository.findAll());
}
您不必担心SingerRepository的实现,它可以正常工作(在testInsert()中,您可以看到singerRepository.findAll(),并且它可以工作,我看到插入的歌手)。