I'm currently trying to learn how to use Spring Boot and have a problem I'm not sure how to solve.
I've followed the guide at http://spring.io/guides/gs/accessing-data-jpa/ and everything works fine. However, if I restart the server, then all the data that was saved is completely lost. Is there any way to keep the data in the repository/database so that if I shut down the application and start it again, all the previously saved data is still accessible?
Thank you in advance :)
Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.
Save and saveAndFlush both can be used for saving entities. They both are both belong to the Spring data library. save may or may not write your changes to the DB straight away. When we call saveAndFlush system are enforcing the synchronization of your model state with the DB.
persistence. xml is needed when you're using Hibernate through JPA, even though you're using Spring JPA. If you're using Hibernate directly, then persistence. xml isn't needed.
All examples use an embedded database with in memory persistence, which means, the data is only stored as long as the process is running. Just switch to a regular database like MySQL or use H2 with a file based storage url, which is also permanently saved on your disk. For the latter, just add the following property to your application.properties:
spring.datasource.url=jdbc:h2:tcp://localhost/${path/to/your/db/file}
and replace ${path/to/your/db/file}
with the path where you want to store the database (note, the folder you configure here will be created, if it doesn't exist).
If you want to keep your data on server restart then add the following property into application.properties
file :
`spring.jpa.hibernate.ddl-auto=update`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With