I downloaded a Spring Data JPA example from here Accessing Data with JPA and it works as expected.
@SpringBootApplication
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Bean
public CommandLineRunner demo(CustomerRepository repository) {
return (args) -> {
// 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
log.info("Customers found with findAll():");
log.info("-------------------------------");
for (Customer customer : repository.findAll()) {
log.info(customer.toString());
}
log.info("");
// fetch an individual customer by ID
Customer customer = repository.findOne(1L);
log.info("Customer found with findOne(1L):");
log.info("--------------------------------");
log.info(customer.toString());
log.info("");
// fetch customers by last name
log.info("Customer found with findByLastName('Bauer'):");
log.info("--------------------------------------------");
for (Customer bauer : repository.findByLastName("Bauer")) {
log.info(bauer.toString());
}
log.info("");
};
}
}
Then I tried to change from In-Memory database to a real one. For this I added in the current directory an application.properties
file with a single line:
spring.datasource.url=jdbc:h2:C:/users/semaphor/H2Database;DB_CLOSE_ON_EXIT=FALSE
Now when I run the application, I can see that in the expected directory the database file is created.
Now I want to see that the customers are stored in the database and are still there when I restart the application. So I removed some lines where customers were saved in the database at the beginning but they are then not listen with findAll()
.
I suppose this transaction is not commited but I can't figure out how I can do this. Adding simply the annotation @EnableTranscationManagement
to the Application class and @Transactional
to the demo()
method is not the solution.
What do I have to add to commit the transaction (if that is really the problem)?
See M. Deinum's comment below the question.
Adding
spring.jpa.hibernate.ddl-auto=update
to the application.properties
was the solution.
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.