Spring Boot关系返回null

时间:2018-10-29 08:58:15

标签: java spring-boot relation

我对Spring Boot关系有疑问。当方法创建返回方法读取(NullPointerException)时会发生问题,因为它无法从与项目类(project.getColors()。getValue())具有一对一关系的对象访问方法。

package Main.Controller;

import Main.Model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/project")
public class ProjectController {

@Autowired
private ProjectRepository projectRepository;

@RequestMapping(path = "/create", method = RequestMethod.POST)
@ResponseBody
public Object create(@RequestBody CreateProjectObject object) {

    Project project=new Project();
    project.setName(object.getName());
    project.setColor(object.getValue());
    projectRepository.save(project);
    return this.read();
}

@RequestMapping(path = "/read", method = RequestMethod.GET)
@ResponseBody
public List<Map<String, Object>> read() {
    List<Map<String, Object>> readProject = new ArrayList<>();

    for (Project project: projectRepository.findAll()
    ) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("value", project.getId());
        map.put("color", project.getColors().getValue());//           <--------
        map.put("label", project.getName());
        readProject.add(map);
    }
    return readProject;
}

但是事实是,方法读取可以毫无问题地读取之前创建的所有其他项目。如果我在几秒钟后调用该方法,它将读取之前无法读取的相同项目。 这是项目模型:

package Main.Model;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;
import java.util.HashSet;

@Entity
@Table(name = "projects")
public class Project {

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

@Column
@CreationTimestamp
private LocalDateTime created_at;

@Column
@UpdateTimestamp
private LocalDateTime updated_at;

private String name;

private Integer color;

@OneToOne
@JoinColumn(name = "color", insertable =  false, updatable = false)
private Color colors;

public Color getColors() {
    return colors;
}

public void setColors(Color colors) {
    this.colors = colors;
}

public Integer getId() {
    return id;
}

public String getName() {
    return name;
}

public Integer getColor() {
    return color;
}

public void setId(Integer id) {
    this.id = id;
}

public void setName(String name) {
    this.name = name;
}

public void setColor(Integer color) {
    this.color = color;
}

}

编辑:这是Color类

package Main.Model;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
@Table(name = "colors")
public class Color {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Integer id;

@Column
@CreationTimestamp
private LocalDateTime created_at;

@Column
@UpdateTimestamp
private LocalDateTime updated_at;

private String value;

public String getValue() {
    return value;
}

public void setValue(String name) {
    this.value = value;
}


public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

}

2 个答案:

答案 0 :(得分:0)

您必须在@OneToOne关系中添加CascadeType.ALL。另外,Docker cp doc是了解JPA关系的简单教程。

答案 1 :(得分:0)

默认情况下,用户@OneToOne(fetch=FetchType.EAGER)将加载LAZY

还有一点,如果您从Color类中加入的颜色然后不需要private Integer color;,或者它的其他用途可能与@JoinColumn冲突

@OneToOne((cascade = CascadeType.ALL), fetch=FetchType.EAGER)
@JoinColumn(name = "colors", insertable =  false, updatable = false)
private Color colors;

并且您的项目创建方法还应该设置颜色:

@RequestMapping(path = "/create", method = RequestMethod.POST)
    public Object create(@RequestBody CreateProjectObject object) {

        Project project=new Project();
        project.setName(object.getName());
        project.setColor(object.getValue());
        Color color = new Color();
        color.setId(object.getValue());
        project.setColors(color);
        projectRepository.save(project);
        return this.read();
    }