Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实体类的属性是一个List (OneToMany关系), 前端如何将数据一次性传给后端 #6

Closed
zhanyeye opened this issue Dec 11, 2019 · 1 comment
Assignees
Labels
question Further information is requested

Comments

@zhanyeye
Copy link
Member

如下,一个Application包含多个AcItem。前端如何一次性将这一对多的关系提交?

public class Application {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private int month;
    private int week;
    private int DC;
    private boolean isCheck;   // 是否已审核
    @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP", updatable = false, insertable = false)
    private LocalDateTime insertTime;   //插入时间

    @ManyToOne
    private User applicant;    // 申请人
    @ManyToOne
    private User auditor;      // 审核人


    @OneToMany(mappedBy = "application")
    private List<AcItem> acItems;  //本次绩效申请包含的 AC申请
}
public class AcItem {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private int AC;
    private String reason;

    @ManyToOne
    private Application application;  //ac申请属于的周绩效申请
}
@zhanyeye zhanyeye added the question Further information is requested label Dec 12, 2019
@zhanyeye zhanyeye self-assigned this Dec 12, 2019
@zhanyeye
Copy link
Member Author

zhanyeye commented Dec 12, 2019

前端传来的application如下

{
  "applicant": {
    "id": 1
  },
  "auditor": {
    "id": 1
  },
  "month": 9,
  "week": 4,
  "dc": 2,
  "acItems": [
    {
      "ac": 1,
      "reason": "组会汇报"
    },
    {
      "ac": 1,
      "reason": "论文评审"
    },
    {
      "ac": 1,
      "reason": "组会评审"
    }
  ]
}

controller处,将json反序列化为application对象,json中的acItems也被反序列化为List<AcItem>,但是并不能被持久化!
https://github.com/zhanyeye/dingtalk-springboot/blob/df23baf3fb9d74bffb0555e87f046248bd35c8cc/src/main/java/com/softeng/dingtalk/controller/ApplicationController.java#L47-L54
需要自行插入application,并持久化
https://github.com/zhanyeye/dingtalk-springboot/blob/df23baf3fb9d74bffb0555e87f046248bd35c8cc/src/main/java/com/softeng/dingtalk/service/AcItemService.java#L25-L36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant