Ole
Ole

Reputation: 46940

Spring Boot JPA & H2 Records Not Persisted

As a baseline I'm using the Spring Boot demo Accessing Data JPA.

My goal is to be able to view the persisted entities using the h2 console. I'm able to run the application with Maven, but when I subsequently connect with the h2 console, the database is empty.

If I set spring.jpa.hibernate.ddl_auto=none then the application does not run, so I know that this value is being picked up from src/main/resources, however it I set it to create or update, the database is still empty at the end of the mvn spring-boot:run run.

In the past versions of Spring and Hibernate I have set auto_dll=create, and Hibernate has created the database, if it did not already exists. Does this no longer work?

This is what the updated example application looks like, minus import declarations:

@Configuration
@EnableAutoConfiguration
public class Application {

    @Bean
    public DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setDriverClassName("org.h2.Driver");
        config.setJdbcUrl("jdbc:h2:file:~/db1");
        config.setUsername("sa");
        config.setPassword("");
        return new HikariDataSource(config);
    }

public static void main(String[] args) {

    ConfigurableApplicationContext context = SpringApplication.run(Application.class);
    CustomerRepository repository = context.getBean(CustomerRepository.class);

    // save a couple of customers
    repository.save(new Customer("Jack", "Bauer"));
    repository.save(new Customer("Chloe", "O'Brian"));
    repository.save(new Customer("Kim", "Bauer"));
    repository.save(new Customer("David", "Palmer"));
    repository.save(new Customer("Michelle", "Dessler"));

    // fetch all customers
    Iterable<Customer> customers = repository.findAll();
    System.out.println("Customers found with findAll():");
    System.out.println("-------------------------------");
    for (Customer customer : customers) {
        System.out.println(customer);
    }
    System.out.println();

    // fetch an individual customer by ID
    Customer customer = repository.findOne(1L);
    System.out.println("Customer found with findOne(1L):");
    System.out.println("--------------------------------");
    System.out.println(customer);
    System.out.println();

    // fetch customers by last name
    List<Customer> bauers = repository.findByLastName("Bauer");
    System.out.println("Customer found with findByLastName('Bauer'):");
    System.out.println("--------------------------------------------");
        for (Customer bauer : bauers) {
            System.out.println(bauer);
        }

        context.close();
    }

}

TIA, - Ole

Upvotes: 1

Views: 4262

Answers (1)

Biju Kunjummen
Biju Kunjummen

Reputation: 49915

By default the JPA database configuration is set to create the tables at the beginning and drop at the end. This can be changed by the following entry in your application.properties file:

spring.jpa.hibernate.ddl-auto=update

See reference here.

Upvotes: 4

Related Questions