无法在一对多映射中删除子项并添加新子项 [Hibernate]

huangapple 未分类评论42阅读模式
英文:

Unable to remove childs in OneToMany Mapping and add new childs [Hibernate]

问题

我知道这个问题已经被问过很多次,但是没有一个解决方案适用于我。

所以我有一个父类:

  1. class User{
  2. @Id
  3. @NotNull
  4. @Column(name = "UserId", nullable = false)
  5. private Long userId;
  6. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
  7. @JoinColumn(name = "UserId")
  8. private Set<Phone> phoneList;
  9. }

还有一个子类:

  1. class Phone {
  2. @ManyToOne(fetch = FetchType.EAGER)
  3. @JoinColumn(name = "UserId")
  4. private User user;
  5. }

现在当我收到一个更新用户类的新手机列表时,我想要删除所有旧的手机并添加新的手机。请注意,所有这些操作都发生在同一个 @Transactional 中。

我尝试过的解决方案:

  1. user.getPhoneList().clear();
  2. user.getPhoneList().addAll(新的手机列表);

当我尝试上述逻辑时,Hibernate 试图将旧的手机的 userId 设置为 null。在这个位置上,我得到一个 DataIntegrityViolation,因为 Phone 表中的 userId 是非空列。

请提供一个适当的解决方案,可以在这里工作。

英文:

I know this question has been asked many times but none of the solution is working for me.

So I have a Parent class :

  1. class User{
  2. @Id
  3. @NotNull
  4. @Column(name = &quot;`UserId`&quot;, nullable = false)
  5. private Long userId;
  6. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
  7. @JoinColumn(name = &quot;`UserId`&quot;)
  8. private Set&lt;Phone&gt; phoneList;
  9. }

And a child class:

  1. class Phone {
  2. @ManyToOne(fetch = FetchType.EAGER)
  3. @JoinColumn(name = &quot;`UserId`&quot;)
  4. private User user;
  5. }

Now when I received a update for User class with new phone list, I want to remove all the old phones and add new phones. Please note that this all operation is happening in same @Transactional.

Solution I tried:

user.getPhoneList().clear()
user.getPhoneList().addAll(new phone list)

When I try the above logic, Hibernate is trying to set old phone with userId as null. At this position I am getting DataIntegrityViolation as userId in Phone table is non null column.

Please provide any appropriate solution which can work here.

答案1

得分: 0

  1. Hhhmmm... 我有完全相同的逻辑而且我的代码能正常运行这是我的类
  2. @Data
  3. @Entity
  4. public class ProductReference {
  5. @Id
  6. @GeneratedValue(strategy = GenerationType.AUTO)
  7. private long id;
  8. @OneToMany(mappedBy = "productReference", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE, orphanRemoval = true)
  9. private Set<Attribute> attributes = new HashSet<>();
  10. }
  11. 唯一我看到的区别是 `CascadeType.REMOVE`
  12. @Data
  13. @Entity
  14. public class Attribute {
  15. @Id
  16. @GeneratedValue(strategy = GenerationType.AUTO)
  17. private long id;
  18. @ManyToOne
  19. private ProductReference productReference;
  20. }
  21. 我的删除操作
  22. productReference.getAttributes().clear();
  23. 你使用的 Hibernate 版本是多少我使用的是 `org.hibernate.Version - HHH000412: Hibernate Core {5.4.10.Final}`
英文:

Hhhmmm... I have the exact same logic and it works fine by me. Here are my classes

  1. @Data
  2. @Entity
  3. public class ProductReference {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private long id;
  7. @OneToMany(mappedBy = &quot;productReference&quot;, fetch = FetchType.EAGER, cascade = CascadeType.REMOVE, orphanRemoval = true)
  8. private Set&lt;Attribute&gt; attributes = new HashSet&lt;&gt;();
  9. }

The only difference I see is the CascadeType.REMOVE

  1. @Data
  2. @Entity
  3. public class Attribute {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private long id;
  7. @ManyToOne
  8. private ProductReference productReference;
  9. }

My deletion:

  1. productReference.getAttributes().clear();

Which Hibernate version you have? by me it is org.hibernate.Version - HHH000412: Hibernate Core {5.4.10.Final}

huangapple
  • 本文由 发表于 2020年7月24日 14:48:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/63068266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定