1

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

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hello Biju - I tried the update setting, however the database is still empty after the application is run. I know that the values is being picked up, because if I set it ton none then I get an exception saying that the Customer Table does not exists. Thoughts?
Strange, it appears to work for me cleanly!, I removed your HikariCP based datasource and retained the default with the following entries in application.properties spring.jpa.hibernate.ddl-auto=update spring.datasource.url=jdbc:h2:file:~/db1 and I see entries in the database at the end of the run.
Hi Biju - Sorry for the late reply. I ended up setting it to none, generating the schema via the Hibernate4 Maven Plugin, and applying the schema to h2 via the h2 console. I now get records as well.
Thank you, that was the only correct solution to this problem. I tried many other things..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.