简体   繁体   English

应用程序关闭后删除了 Spring JPA 数据

[英]Spring JPA data removed after shutdown of application

i have an application thats build on Spring boot, using JPA repositories on HSQL database.我有一个基于 Spring Boot 构建的应用程序,使用 HSQL 数据库上的 JPA 存储库。

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;但是从eclipse关闭应用程序后,所有数据都被删除了;

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 boot 基本上只有我拥有的配置是这个配置文件。

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

Do you have hbm2ddl text somewhere in your configuration properties.您的配置属性中是否有 hbm2ddl 文本。 It should be set to update or none , apparently you might have create-drop .它应该设置为updatenone ,显然你可能有create-drop

Specify a local filename in application.properties data source URL:在 application.properties 数据源 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.您可以删除spring.datasource.driver-class-name属性,因为 Spring Boot 通过 URL 属性检测到它。

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
  1. If you insert the data within a @Test , it's rolled back by default.如果在@Test插入数据,默认情况下它会回滚。
  2. Your database might get dropped, if you restart the application, like LearningPhase suggested.如果您重新启动应用程序,您的数据库可能会被删除,就像 LearningPhase 建议的那样。
  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. insert从未真正提交 - 因为它在事务之外,或者因为抛出了Exception并且没有在事务中处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM