@SpringBootApplication
public class DatabaseDemoApplication implements CommandLineRunner {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
PersonJbdcDao dao;
public static void main(String[] args) {
SpringApplication.run(DatabaseDemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
// Thread.sleep(1000); --------------------------------------------------> line 1
logger.info("All users -> {}", dao.findAll());
}
}
@Repository
public class PersonJbdcDao {
@Autowired
JdbcTemplate jdbcTemplate;
public List<Person> findAll() {
return jdbcTemplate.query("select * from person",
new BeanPropertyRowMapper<Person>(Person.class));
}
}
In pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
I am using h2 database. Following is the application.properties
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
Person.class
private int id;
private String name;
private String location;
private Date birthDate;
// getter and setter
I also have a data.sql
file in the resources folder which have create and insert statements. If i keep line 1(Thread.sleep(1000)) commented and run the project I'm facing an error but I am able to access table Person via h2 console
java.lang.IllegalStateException: Failed to execute CommandLineRunner
Caused by: org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [select * from person]; nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PERSON" not found; SQL statement:
select * from person [42102-200]
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PERSON" not found; SQL statement:
select * from person [42102-200]
I have 2 scenarios
data.sql
file to schema.sql
file everything works perfect. I'm able to access why is that?data.sql
file and don't rename it I have to uncomment line 1 to get application working. Why is that? Since Spring Boot 2.3, JPA repository initialization is "deferred" . So run
method runs before initialized it (if you don't define any JPA repositories).
If set
spring.data.jpa.repositories.bootstrap-mode=default
on application.properties
, run as you expected.
see also:
DataSourceInitializerInvoker
javadoc says:
Bean to handle
DataSource
initialization by runningschema-*.sql
onInitializingBean#afterPropertiesSet()
anddata-*.sql
SQL scripts onDataSourceSchemaCreatedEvent
.
, and DataSourceSchemaCreatedEvent
says:
This happens when
schema-*.sql
files are executed or when Hibernate initializes the database.
So, on first scenario, data.sql
is executed after JPA(Hibernate) initialization, but it is "deferred". On the other hand, schema.sql
is executed before run()
, same as earlier version.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.