Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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
Spring JPA一对多关系在更新时返回null_Spring_Jpa_Spring Data Jpa - Fatal编程技术网

Spring JPA一对多关系在更新时返回null

Spring JPA一对多关系在更新时返回null,spring,jpa,spring-data-jpa,Spring,Jpa,Spring Data Jpa,我在学年和科目之间有一对多的关系(一个学年有许多科目)。 这是学年的型号: package com.sms.entity; import javax.persistence.*; import com.fasterxml.jackson.annotation.JsonManagedReference; import org.hibernate.annotations.UpdateTimestamp; import io.swagger.v3.oas.annotations.media.Sch


我在
学年
科目
之间有一对多的关系(一个学年有许多科目)。 这是
学年的型号

package com.sms.entity;

import javax.persistence.*;

import com.fasterxml.jackson.annotation.JsonManagedReference;
import org.hibernate.annotations.UpdateTimestamp;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import java.util.Date;
import java.util.Set;

@Entity
@Setter
@Getter
@Table(name = "academic_years")
@NoArgsConstructor
@AllArgsConstructor
public class AcademicYear {

    public AcademicYear(long id, String name, Date updatedAt) {
        this.id = id;
        this.name = name;
        this.updatedAt = updatedAt;
    }

    @Schema(description = "Unique identifier of the academic year.", example = "1")
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Schema(description = "Name of the Academic Year.", example = "First Year Primary", required = true)
    @Column(name = "name")
    private String name;

    @JsonManagedReference
    @OneToMany(mappedBy="academicYear", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<Subject> subjects;

    @UpdateTimestamp
    @Column(name = "updated_at", columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
    private Date updatedAt;

    public Set<Subject> getSubjects() {
        return subjects;
    }

    public void setSubjects(Set<Subject> subjects) {
        this.subjects = subjects;
    }

}
当我尝试更新
AcademicYear
name
属性时,我发送了一个带有以下主体的
PUT
请求:

{
    "id": 2,
    "name": "Second Year"
}
我得到的答复如下:

{
    "id": 2,
    "name": "Second Year",
    "subjects": null,
    "updatedAt": "2020-03-27T18:01:16.163+0000"
}
我的
主题
null
。此实体已经有记录,当我发送
GET
请求并将2作为pathvariable获取实体时,我得到以下响应:

{
    "id": 2,
    "name": "Second Year",
    "subjects": [
        {
            "id": 3,
            "name": "english",
            "updatedAt": "2020-03-27T17:39:09.000+0000"
        },
        {
            "id": 4,
            "name": "physics",
            "updatedAt": "2020-03-26T21:45:09.000+0000"
        },
        {
            "id": 5,
            "name": "chemistry",
            "updatedAt": "2020-03-26T21:45:09.000+0000"
        },
        {
            "id": 2,
            "name": "math",
            "updatedAt": "2020-03-27T17:39:09.000+0000"
        }
    ],
    "updatedAt": "2020-03-27T18:01:16.000+0000"
}

我的获取类型为
EAGER
,不知道为什么我在更新实体名称时将
subjects
获取为
null
。有什么帮助吗?

对于部分更新,您需要使用
PATCH
而不是
PUT

这就是原因

基于RFC 7231,在幂等运算中,PUT只能用于表示的完全替换。修补程序应用于部分更新

根据您的输入,请求将null设置为subjects。 如果仍要使用PUT,则需要提供要更新/替换的整个请求对象

你可以在这里找到更多细节

{
    "id": 2,
    "name": "Second Year",
    "subjects": [
        {
            "id": 3,
            "name": "english",
            "updatedAt": "2020-03-27T17:39:09.000+0000"
        },
        {
            "id": 4,
            "name": "physics",
            "updatedAt": "2020-03-26T21:45:09.000+0000"
        },
        {
            "id": 5,
            "name": "chemistry",
            "updatedAt": "2020-03-26T21:45:09.000+0000"
        },
        {
            "id": 2,
            "name": "math",
            "updatedAt": "2020-03-27T17:39:09.000+0000"
        }
    ],
    "updatedAt": "2020-03-27T18:01:16.000+0000"
}