【问题标题】:Repository.save only commit when creating, not updatingRepository.save 仅在创建时提交,不更新
【发布时间】:2017-05-26 08:09:52
【问题描述】:

我有一个使用 Spring Boot、Hibernate 和 Spring Data Jpa 的微服务。问题是,当我尝试保存实体时,它仅在实体是新实体时提交事务,而不是在更新实体时(当 id 不为空时)。这很奇怪,因为我在创建或更新实体时使用了相同的代码。

我激活了日志,只有在创建实体时才能看到 insert into 语句。有什么提示吗?

@Transactional
public class MyEntityService {

    @UserCanCud
    public Entity save(final Entity entity) {
        Assert.notNull(entity, "The entity to store cannot be null");
        return repository.save(entity);
    }
}

PS:当我返回实体时,它是“更新”的实体,但如果我再次检索它,则值不会更新。

UserCanCud 检查数据库是否允许用户对该实体进行 cud 操作。

【问题讨论】:

  • 请提供更多信息。配置?代码是怎么调用的?交易在哪里开启?
  • 我可以知道为什么entity 对象被标记为final 吗?
  • 您好,shaharavmb,没有具体原因,您认为这可能是问题所在吗?
  • 如果没有具体原因,可以去掉试试吗?
  • 你能告诉我们存储库的保存实现吗?

标签: spring hibernate spring-data spring-data-jpa


【解决方案1】:

最后我犯了这个问题.... pojo 有注释@Immutable。

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多