【问题标题】:How to save child entity with shared primary key如何使用共享主键保存子实体
【发布时间】:2019-04-26 20:38:35
【问题描述】:

问题

我有两个实体,Parent 和 Child,我希望它们共享一个主键,但我希望关系是单向的。只有孩子应该知道父母。 spring-data-jpa 存储库可以做到这一点吗?

我尝试过的

  • 我已经尝试直接使用 EntityManager 并且它可以工作。我可以先保留Parent,然后再保留Child 实体。但是,当我尝试使用 Spring 的 JpaRepository 进行相同操作时,出现以下错误:org.springframework.orm.jpa.JpaSystemException: attempted to assign id from null one-to-one property。

  • 我尝试使用自己的 SERIAL id 和对父级的外键引用对 Child 实体进行不同的建模。这可行,但我更喜欢使用共享主键。

父实体:

@Entity
public class Parent {

  @Id
  @Column(name = "code")
  String code;

}

子实体:

@Entity
public class Child {

  @Id
  @Column(name = "code")
  String code;

  @MapsId
  @JoinColumn(name = "code")
  @OneToOne
  Parent parent;
}

测试:

@RunWith(SpringRunner.class)
@DataJpaTest
@Slf4j
public class MapsByIdTest {

  @Autowired
  ChildRepository childRepo;

  @Autowired
  ParentRepo parentRepo;

  @Autowired
  EntityManager entityManager;

  @Test // FAILS with org.springframework.orm.jpa.JpaSystemException
  public void testSpringDataJpaRepository() {

    Parent pA = parentRepo.save(Parent.builder().code("A").build());

    Child child = Child.builder().parent(pA).code("A").build();

    childRepo.save(child);
  }

  @Test // WORKS!
  public void testEntityManager() {

    Parent p = Parent.builder().code("A").build();
    entityManager.persist(p);

    Child child = Child.builder().code("A").parent(p).build();
    entityManager.persist(child);

    log.info(entityManager.find(Parent.class, "A").toString());
    log.info(entityManager.find(Child.class, "A").toString());

  }
}

【问题讨论】:

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


    【解决方案1】:

    以下代码可以正常工作:

    public class Child {
    
        @Id
        @Column(name = "code", insertable = false, updatable = false)
        String code;
    
        @MapsId
        @JoinColumn(name = "code")
        @OneToOne
        Parent parent;
    }
    

    测试

    @Test
    @Transactional
    public void testSpringDataJpaRepository() {
        Parent pA = parentRepo.save(Parent.builder().code("A").build());
        Child child = Child.builder().parent(pA).build();
        childRepo.save(child);
    }
    

    解释: 看SimpleJpaRepository.save的实现

    @Transactional
    public <S extends T> S save(S entity) {
    
        if (entityInformation.isNew(entity)) {
            em.persist(entity);
            return entity;
        } else {
            return em.merge(entity);
        }
    }
    

    比检查AbstractEntityInformation.isNew。 它得出结论,只有实体为空(或数字类型为 0)时,实体才是新的。 因此,您的childRepo.save(child); 等同于entityManager.merge(child); 检查如果您在第二次测试中调用合并,您收到的错误是相同的。

    解决问题:

    • 不要在你的孩子@Id 上设置值(可能你想强制lombok 也不为它生成setter)。这将导致persist 被调用而不是merge
    • 请注意,您有 2 个字段 code 和 parent 映射到同一个 db 列。为了使映射正确,我在@Id列上使用了强制insertable = false, updatable = false(parent字段的变化会导致生成正确的sql)

    【讨论】:

      猜你喜欢
      • 2022-11-29
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多