【问题标题】:Spring JPA data removed after shutdown of application关闭应用程序后删除 Spring JPA 数据
【发布时间】:2016-08-04 22:40:38
【问题描述】:

我有一个基于 Spring Boot 构建的应用程序,使用 HSQL 数据库上的 JPA 存储库。

问题是,当应用程序运行时,我创建了一个实体,并且它被正确地持久化到数据库中(可以在数据库管理器中看到)。但是从eclipse关闭应用程序后,所有数据都被删除;

保存是这样进行的

@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);
    }
}

我很确定它的配置问题,但是使用 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

【问题讨论】:

  • 显示您的数据源。看起来您正在使用通常在内存中使用的 hsqldb。如果是这样,当你关闭应用程序时它当然会死。
  • stackoverflow.com/questions/6364171/… 查看此链接。这可能是一个类似的问题。

标签: java spring jpa spring-boot spring-data


【解决方案1】:

您的配置属性中是否有 hbm2ddl 文本。它应该设置为updatenone,显然你可能有create-drop

【讨论】:

  • 这是正确的答案,我添加了属性 spring.datasource.hbm2ddl.auto=update 并且效果很好
  • 很高兴我能提供帮助
  • 该属性也可能是 spring.jpa.hibernate.ddl-auto=update。
【解决方案2】:

在 application.properties 数据源 URL 中指定本地文件名:

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

您可以删除 spring.datasource.driver-class-name 属性,因为 Spring Boot 通过 URL 属性检测到它。

【讨论】:

    【解决方案3】:

    如果存在如下配置行,请检查属性文件

    spring.jpa.hibernate.ddl-auto=create
    

    只需将其删除或更改为

    spring.jpa.hibernate.ddl-auto=update
    

    【讨论】:

      【解决方案4】:
      1. 如果您在@Test 中插入数据,默认情况下会回滚。
      2. 如果您按照 LearningPhase 的建议重新启动应用程序,您的数据库可能会被删除。
      3. insert 永远不会真正提交 - 因为它在事务之外,或者因为 Exception 被抛出并且未在事务中处理。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-20
        • 2018-03-06
        • 2021-06-19
        • 2019-08-12
        相关资源
        最近更新 更多