0

Simple Spring Boot application with h2, which has 1 DB table populated via data.sql. Works 100% with spring-boot-starter-parent 1.5.9.RELEASE if I switch to 2.0.0.RELEASE the data.sql is no longer run on startup.

In my pom.xml I have

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

......

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
     <!-- <version>2.0.0.RELEASE</version> -->  
    <version>1.5.9.RELEASE</version>

    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
     <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>     
        <scope>runtime</scope>       
    </dependency>


</dependencies>

......

and an SQL file with inserts into DB table under src/main/resources/data.sql

In the application.properties file

spring.datasource.url=jdbc:h2:file:~/greeter;AUTO_SERVER=TRUE
spring.datasource.initialization-mode=always

spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop

Using 1.5.9.RELEASE on start up in logs you see :

2018-07-23 17:53:41.219  INFO 11404 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000227: Running hbm2ddl schema export
Hibernate: drop table greeting if exists
Hibernate: create table greeting (id bigint not null, action varchar(255), say varchar(255), primary key (id))
2018-07-23 17:53:41.238  INFO 11404 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
2018-07-23 17:53:41.341  INFO 11404 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-07-23 17:53:41.590  INFO 11404 --- [           main] o.s.jdbc.datasource.init.ScriptUtils     : Executing SQL script from URL [file:/..../classes/data.sql]

Using 2.0.0.RELEASE

Nothing from my application.properties file seems to be picked up. The DB is not being dropped/recreated and the data.sql is not imported into the DB. And log is as follows; Somehow it seems related to Maven. Startup log where its an issue

2018-07-23 21:16:39.532  INFO 3600 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2018-07-23 21:16:39.741  INFO 3600 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2018-07-23 21:16:39.880  INFO 3600 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2018-07-23 21:16:39.911  INFO 3600 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2018-07-23 21:16:40.114  INFO 3600 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate Core {5.2.14.Final}
2018-07-23 21:16:40.119  INFO 3600 --- [  restartedMain] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2018-07-23 21:16:40.382  INFO 3600 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2018-07-23 21:16:40.685  INFO 3600 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2018-07-23 21:16:41.840  INFO 3600 --- [  restartedMain] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@542d091e'
2018-07-23 21:16:41.850  INFO 3600 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-07-23 21:16:43.574  INFO 3600 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@29bb347c: startup date [Mon Jul 23 21:16:33 CAT 2018]; root of context hierarchy
2018-07-23 21:16:43.710  WARN 3600 --- [  restartedMain] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning

Nothing else but the version changed in pom.xml ...

What is needed in Spring 2.0.0.RELEASE to get this to run?

EDIT 1 EDIT 1 EDIT 1 Somehow it seems related to Maven dependencies ??????? If I include the repositories in pom.xml such as below all is ok... removing them I have the above mentioned issue

<repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>  
Melissa
  • 812
  • 2
  • 10
  • 24
  • 1
    where are you placing your `data.sql` file? – Raja Anbazhagan Jul 23 '18 at 17:15
  • 1
    src/main/resources/data.sql – Melissa Jul 23 '18 at 17:16
  • can you show me the startup log? I just tried with a simple project and the data.sql seems to have been picked and executed for me... – Raja Anbazhagan Jul 23 '18 at 17:54
  • @RajaAnbazhagan I edited the question to include startup log for when I am experiencing this issue. Have also added an oddity that seems to correct it. – Melissa Jul 23 '18 at 19:24
  • Without the repository configuration it runs fine for me... Will you be able to share us a simple reproducible example over GitHub...? – Raja Anbazhagan Jul 23 '18 at 19:26
  • Also if you don't want any of this but your end result is loading data to your application, try renaming `data.sql` to `import.sql`. hibernate will do the importing for you... This is completely a hibernate feature whereas data.sql is spring feature... Sometimes spring works differently if you define your own data source and stuff... – Raja Anbazhagan Jul 23 '18 at 19:29

1 Answers1

3

Make sure that you use jdbc:h2:mem:testdb as JDBC URL when connecting from h2-console. When we have the default url provided by h2 (jdbc:h2:~/test) then you won't see the script result in the database.

Also, to view the debugs on the console, just put debug=true in the application.properties file.

In-Memory

The URL jdbc:h2:mem:test means open an in-memory database named 'test'. Data is not persisted, and lost when the last connection to the database is closed. Multiple threads can access the same database, but data is only visible within the same process

For more information, see http://www.h2database.com/html/features.html#database_url