Hibernate 使用Spring数据REST更新多个多对一嵌套实体

Hibernate 使用Spring数据REST更新多个多对一嵌套实体,hibernate,spring-data-rest,Hibernate,Spring Data Rest,我有一个SpringDataREST应用程序,其中Hibernate作为持久层。我有一个实体,有两个不同的@ManyToOne引用。这是我的映射: @Entity public class Menu { @Id @GeneratedValue private Long id; @ManyToOne private Menu parent; @ManyToOne private Resource resource; } 我有几个Sp

我有一个SpringDataREST应用程序,其中Hibernate作为持久层。我有一个实体,有两个不同的
@ManyToOne
引用。这是我的映射:

@Entity
public class Menu {

    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne
    private Menu parent;

    @ManyToOne
    private Resource resource;

}
我有几个Spring数据存储库:

public interface MenuRepository extends CrudRepository<Menu, Long> {}

public interface ResourceRepository extends CrudRepository<Resource, Long> {}
问题是,只有一个(随机的一个)引用被附加,另一个保持
null

原因:

根据Hibernate SQL日志判断,Hibernate在引擎盖下生成的两个更新查询都会更新所有字段,甚至那些未更改的字段,并且这些并发更新会相互覆盖:

update menu set parent_id=1, resource_id=null where id=2
update menu set parent_id=null, resource_id=2 where id=2
解决方法:

如果向实体添加
@DynamicUpdate
注释,则生成的SQL仅限于更改的属性:

update menu set resource_id=2 where id=2
update menu set parent_id=1 where id=2
这就解决了问题。但这似乎是一个非常脆弱和模糊的解决方案

我的问题:

  • 有没有其他(最好是事务性的)惯用解决方案 使用Spring数据更新多个嵌套引用的问题 REST,而不必发送两个单独的HTTP请求

  • 如果我使用两个独立的PUT请求,我希望它们是幂等的, 不管怎样,幂等性应该由REST层处理 底层持久性层的。幂等性是明显的 在本案中被违反。这是Spring Data REST的一个错误/功能吗, 冬眠,还是两者的结合

  • 也许我错过了其他的东西,或者其他的解决方案,而这根本就不应该发生

  • update menu set resource_id=2 where id=2
    update menu set parent_id=1 where id=2