File based h2 persisted but not loaded in Spring Boot

11,578

The default for spring.jpa.hibernate.ddl-auto is create-drop if you use an embedded database. You probably want it to be empty, or just validate (none might work as well but I think that's deprecated by hibernate).

Share:
11,578

Related videos on Youtube

Phil
Author by

Phil

Updated on June 05, 2022

Comments

  • Phil
    Phil almost 2 years

    I made a small application based on Spring Boot:

    • spring-boot-starter-web
    • spring-boot-starter-data-jpa

    The application has simply one domain class Post.java. Accordingly there is a RestController and a DAO. The data is supposed to be persisted in a file based hsql db.

    When the application is running everything seems fine. Data is stored. The h2 file is created and contains insert statements.

    However, when I kill the application and start it a second time. No data is loaded. (As if a brand new db file was created, which overwrote the old one).

    application.properties

    spring.datasource.url = jdbc:h2:file:~/testdb
    spring.datasource.username = sa
    spring.datasource.password = sa
    spring.datasource.driverClassName = org.h2.Driver
    

    pom.xml

    <!-- Spring Boot Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- Spring Boot Data JPA -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
    <!-- H2 DB -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.179</version>
    </dependency>
    

    PostDAO.java

    public interface PostDAO extends JpaRepository<Post, Integer>{
        public Post findByMessage(String message);
    }
    

    Post.java

    @Entity
    public class Post {
    
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Long id;
        private String message;
    
        public Post(){
        }
    
        public Post(String message) {
            super();
            this.message = message;
        }
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    }
    
    • Srki Rakic
      Srki Rakic over 7 years
      Could you please provide updated pom.xml, application.properties, and any other changes you made to get this to work. Thanks.
    • qiubix
      qiubix over 7 years
      In this case you just need to add spring.jpa.hibernate.ddl-auto=validate to your application.properties file. @SrkiRakic
  • Phil
    Phil almost 10 years
    That was it. Thanks! Even though I assumed Spring would detect the correct behaviour automatically: docs.spring.io/spring-boot/docs/1.0.1.RELEASE/reference/html‌​/…