Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 如何在Spring Data JPA中更新@OnetoMany关系中的子实体_Hibernate_Jpa_Spring Data Jpa - Fatal编程技术网

Hibernate 如何在Spring Data JPA中更新@OnetoMany关系中的子实体

Hibernate 如何在Spring Data JPA中更新@OnetoMany关系中的子实体,hibernate,jpa,spring-data-jpa,Hibernate,Jpa,Spring Data Jpa,我有一个父类: @Entity(name = "Master") @Table(name = "master") public class Master { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) private Integer id; @Column(name = "name") private String name; @OneToMany(mappedBy = "

我有一个父类:

@Entity(name = "Master")
@Table(name = "master")
public class Master {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "name") 
    private String name;


    @OneToMany(mappedBy = "master", cascade = CascadeType.ALL, fetch = FetchType.Lazy)
    List<AdditionalAttribute> additionalAttributes = new ArrayList<>();


    public void addAttributes(AdditionalAttribute attribute) {
        additionalAttributes.add(attribute);
        attribute.setFindings(this);
    }

    public void removeAttributes(AdditionalAttribute attribute) {
        additionalAttributes.remove(attribute);
        attribute.setFindings(null);
    }
}
现在假设我已经在两个表中插入了数据,如:

  Master
 ================
 id.      name
 1.       xyz

 AdditionalAttribute
======================================================
 id.     attributeName.   attributeValue.    master_id
 1         Acme               test               1
 2.        loreal             red                1
现在给定
master\u id
,如何更新
AdditionalAttribute
表中该id的
attributeValue

我所做的是:

  • 从主实体(我们从会话中获得的)获取
    附加属性的列表

  • 迭代列表并获取每个
    AdditionAttribute
    对象

  • 将新值设置为对象


  • 这是更新的好方法吗?我们有更好的方法吗?

    如果只使用SQL查询更新实体会怎么样?
      Master
     ================
     id.      name
     1.       xyz
    
     AdditionalAttribute
    ======================================================
     id.     attributeName.   attributeValue.    master_id
     1         Acme               test               1
     2.        loreal             red                1