通过restcontroller

时间:2018-01-24 11:56:26

标签: java jpa spring-boot spring-data-jpa

我在保存我的实体时遇到问题。我有以下两个实体:

@Entity
public class Flow {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @OneToMany(targetEntity = Step.class, cascade = {CascadeType.ALL})
    private List<Step> steps;

    @ManyToOne(targetEntity = ApplicationUser.class)
    private ApplicationUser user;

    public Flow() {

    }

    public Flow(List<Step> steps, ApplicationUser user) {
        this.steps = steps;
        this.user = user;
    }

    public long getId() {
        return id;
    }

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

    public List<Step> getSteps() {
        return steps;
    }

    public void setSteps(List<Step> steps) {
        this.steps = steps;
    }

    public ApplicationUser getUser() {
        return user;
    }

    public void setUser(ApplicationUser user) {
        this.user = user;
    }
}

@Entity
public class Step {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String duration;
    private String description;
    private String name;

    @ManyToMany(targetEntity = Step.class, cascade = {CascadeType.ALL})
    private List<Step> dependencies;

    public Step() {

    }

    public Step(String duration, String description, String name, List<Step> dependencies) {
        this.duration = duration;
        this.description = description;
        this.name = name;
        this.dependencies = dependencies;
    }

    public long getId() {
        return id;
    }

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

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

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

    public List<Step> getDependencies() {
        return dependencies;
    }

    public void setDependencies(List<Step> dependencies) {
        this.dependencies = dependencies;
    }
}

我的restcontroller有这个方法:

@PostMapping("/new")
public ResponseEntity insertFlow(Flow flow) {

只使用存储库中的默认保存功能

 if (flowRepository.save(flow) != null) {
                logger.info("Flow saved");

现在当我发送此邮件请求时:

{
  "steps" : [ {
    "id" : 0,
    "duration" : "test",
    "description" : "test",
    "name" : "Step 1",
    "dependencies" : []
  } ]
}

保存所有内容(在rest控制器中检索经过身份验证的用户,并在调用save之前将其放在流对象上)

现在,当我检索所有内容或查看数据库时,不会保存步骤。当我这样做时:

 Step step1 = new Step("test", "test", "test", emptyList());
    List<Step> steps = Arrays.asList(step1);

    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    String username = (String) principal;
    ApplicationUser user = applicationUserRepository.findByUsername(username);

    Flow flow = new Flow(steps, user);

    flowRepository.save(flow);

一切正常。

有什么区别?

1 个答案:

答案 0 :(得分:1)

你错过了@RequestBody注释,这就是为什么spring mvc无法从你的json请求体中正确地重新创建Flow并只调用默认构造函数(基本上你得到一个包含所有字段的对象)没有初始化。

public ResponseEntity insertFlow(@RequestBody Flow flow) {
    final Flow saved = flowRepository.save(flow);
    if (saved != null) {
        logger.info("Flow saved");
    }
    // ...
}

因此,在第一种情况下,当您尝试保存空对象时,hibernate只为Flow创建一条新记录,而不执行任何操作。

但是当您手动创建Flow对象时,您可以正确设置所有字段,因此Hibernate能够成功保存它和所有可传递实体。