Spring JPA data removed after shutdown of application

戏子无情 提交于 2021-01-27 06:09:25

问题


i have an application thats build on Spring boot, using JPA repositories on HSQL database.

Problem is that while application is running, I create an entity,and it's persisted correctly to database(can be seen in database manager). But after application shutdown from eclipse, all data is removed;

Saving is performed like this

@Service
public class NotificationService {

    @Autowired
    private NotificationRepository notificationRepository;

    public void notifyRefreshArticles(){
        Notification notification = new Notification();
        notification.setCreatedAt(LocalDateTime.now());
        notification.setNotificationSeverity(NotificationSeverity.NORMAL);
        notification.setNotificationType(NotificationType.REFRESH_ARTICLES);

        notificationRepository.save(notification);
    }
}

I pretty sure its configuration issue,but with spring boot basically only configuration that i have is this configuration file.

spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
spring.datasource.url=jdbc:hsqldb:hsql://localhost/rr_app_database
spring.datasource.username=XXXXXX
spring.datasource.password=XXXXXX
spring.datasource.show-sql=true

回答1:


Do you have hbm2ddl text somewhere in your configuration properties. It should be set to update or none, apparently you might have create-drop.




回答2:


Specify a local filename in application.properties data source URL:

spring.datasource.url=jdbc:hsqldb:file:/home/username/testedb

You can remove the spring.datasource.driver-class-name property as Spring Boot detects it by URL property.




回答3:


  1. If you insert the data within a @Test, it's rolled back by default.
  2. Your database might get dropped, if you restart the application, like LearningPhase suggested.
  3. The insert is never really committed - because it's outside of an transaction or because an Exception is thrown and not handeled within the transaction.



回答4:


Check the properties files if there exists a configuration line as below

spring.jpa.hibernate.ddl-auto=create

Just remove it or change to

spring.jpa.hibernate.ddl-auto=update


来源:https://stackoverflow.com/questions/36605104/spring-jpa-data-removed-after-shutdown-of-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!