简体   繁体   English

JPA 2 / Hibernate - 更新复杂实体的最佳方法?

[英]JPA 2/Hibernate - Best way to update complex entities?

I'm new to JPA/Hibernate and I'm wondering, what is usually the best way of updating a complex entity? 我是JPA / Hibernate的新手,我想知道,更新复杂实体的最佳方法是什么?

For example, consider the entity below: 例如,考虑下面的实体:

@Entity
public class Employee {

    @Id
    private long id;
    @Column
    private String name;
    @ManyToMany
    private List<Positions> positions;

    // Getters and setters...

}

What is the best way to update the references to positions? 更新职位参考的最佳方法是什么? Currently a service is passing me a list of positions that the employee should have. 目前,服务正在向我传递员工应该拥有的职位列表。 Creating them is easy: 创建它们很简单:

for (long positionId : positionIdList) {
    Position position = entityManager.find(positionId);
    employee.getPositions.add(position);
}

entityManager.persist(employee);

However, when it comes to updating the employee, I'm not sure what the best way of updating the employees positions would be. 但是,在更新员工时,我不确定更新员工职位的最佳方式是什么。 I figure there is two options: 我认为有两种选择:

  1. I parse through the list of position id's and determine if the position needs to be added/deleted (this doesn't seem like a fun process, and may end up with many delete queries) 我解析位置ID的列表,并确定是否需要添加/删除位置(这似乎不是一个有趣的过程,并可能最终有许多删除查询)

  2. I delete all positions and then re-add the specified positions. 我删除所有位置,然后重新添加指定的位置。 Is there a way in JPA/Hibernate to delete all children (in this case positions) with one sql command? 在JPA / Hibernate中有一种方法可以用一个sql命令删除所有子节点(在本例中为position)吗?

Am I thinking about this the wrong way? 我是否以错误的方式思考这个问题? What do you guys recommend? 你们推荐什么?

JPA/Hibernate has support for this. JPA / Hibernate对此有所支持。 It's called cascading. 它被称为级联。 By using @ManyToMany(cascade=CascadeType.ALL) (or limit the cascade type to PERSIST and MERGE ), you specify that the collection should be persisted (merged/deleted/etc) when the owning object is. 通过使用@ManyToMany(cascade=CascadeType.ALL) (或将级联类型限制为PERSISTMERGE ),指定在拥有对象时应该持久化(合并/删除/等)集合。

When deletion is concerned, there is a special case, when objects become "orphans" in the database. 当涉及删除时,有一种特殊情况,即对象在数据库中变成“孤儿”。 This is handled by setting orphanRemoval=true 这是通过设置orphanRemoval=true来处理的

How about 怎么样

employee.getPositions.clear(); // delete all existing one
  // add all of them again
 for (long positionId : positionIdList) {
    Position position = entityManager.find(positionId);
    employee.getPositions.add(position);
}

although it may not be the most efficient approach. 虽然它可能不是最有效的方法。 For a detail discussion see here . 有关详细讨论,请参见此处 Cascading won't help here much because in ManyToMany relation the positions may not get orphaned as they may be attached to other employee (s), or even they shouldn't be deleted at all, because they can exists on their own. 级联在这里没有多大帮助,因为在ManyToMany关系中,职位可能不会因为他们可能附加到其他员工而成为孤儿,或者甚至根本不应该被删除,因为他们可以独立存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM