如何 POST 引用另一个 table 的新记录
How to POST a new record which is referenced to another table
正在尝试使用 Spring 数据剩余:
我有 2 个实体,具有@ManyToOne 关系:
@Data
@Entity
@NoArgsConstructor
@Table(name = "tasks")
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 150)
private String title;
@Lob
private String description;
}
@Data
@Entity
@NoArgsConstructor
@Table(name = "comments")
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "task_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Task task;
@Lob
@Column(nullable = false)
private String comment;
}
我无法添加应该关联到 1 个任务的新评论
POST http://localhost:9000/api/comments
Content-Type: application/json
{
"comment": "Some text",
"task_id": 1
}
这个请求returns:
POST http://localhost:9000/api/comments
HTTP/1.1 409
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Content-Type: application/json
Transfer-Encoding: chunked
Date: Wed, 01 Jul 2020 16:36:25 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"cause": {
"cause": {
"cause": null,
"message": "Column 'task_id' cannot be null"
},
"message": "could not execute statement"
},
"message": "could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement"
}
- 首先我创建了任务
- 已尝试向任务添加评论
如果我通过 id 获取任务:
GET http://localhost:9000/api/tasks/1
Accept: application/json
它会return它:
{
"title": "new task",
"description": "more content",
"priority": "HIGH",
"todoDate": "2020-07-02 10:50",
"_links": {
"self": {
"href": "http://localhost:9000/api/tasks/1"
},
"task": {
"href": "http://localhost:9000/api/tasks/1"
}
}
}
我做错了什么?
我应该使用双向方法并将@OneToMany 添加到任务实体还是可以以某种方式配置?
从 task
中删除 @JsonIgnore
public class Comment {
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "task_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
// @JsonIgnore
private Task task;
}
和POST这个:
{
"comment": "Some text",
"task": "http://localhost:9000/api/tasks/1"
}
对于简单的 ManyToOne
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Task task;
我做了 fetch = FetchType.EAGER(lazy fetch 只获取特定的值 table,如果我们也想要映射的实体值,则使用 eager),并删除了 @JoinColumn(hibernate by默认在评论 table),
中创建字段“task_id”
我的 Json 评论对象是,
{
"comment": "Some text",
"task": {
"id":1
}
}
正在尝试使用 Spring 数据剩余:
我有 2 个实体,具有@ManyToOne 关系:
@Data
@Entity
@NoArgsConstructor
@Table(name = "tasks")
public class Task {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 150)
private String title;
@Lob
private String description;
}
@Data
@Entity
@NoArgsConstructor
@Table(name = "comments")
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "task_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Task task;
@Lob
@Column(nullable = false)
private String comment;
}
我无法添加应该关联到 1 个任务的新评论
POST http://localhost:9000/api/comments
Content-Type: application/json
{
"comment": "Some text",
"task_id": 1
}
这个请求returns:
POST http://localhost:9000/api/comments
HTTP/1.1 409
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Content-Type: application/json
Transfer-Encoding: chunked
Date: Wed, 01 Jul 2020 16:36:25 GMT
Keep-Alive: timeout=60
Connection: keep-alive
{
"cause": {
"cause": {
"cause": null,
"message": "Column 'task_id' cannot be null"
},
"message": "could not execute statement"
},
"message": "could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement"
}
- 首先我创建了任务
- 已尝试向任务添加评论
如果我通过 id 获取任务:
GET http://localhost:9000/api/tasks/1
Accept: application/json
它会return它:
{
"title": "new task",
"description": "more content",
"priority": "HIGH",
"todoDate": "2020-07-02 10:50",
"_links": {
"self": {
"href": "http://localhost:9000/api/tasks/1"
},
"task": {
"href": "http://localhost:9000/api/tasks/1"
}
}
}
我做错了什么? 我应该使用双向方法并将@OneToMany 添加到任务实体还是可以以某种方式配置?
从 task
@JsonIgnore
public class Comment {
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "task_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
// @JsonIgnore
private Task task;
}
和POST这个:
{
"comment": "Some text",
"task": "http://localhost:9000/api/tasks/1"
}
对于简单的 ManyToOne
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Task task;
我做了 fetch = FetchType.EAGER(lazy fetch 只获取特定的值 table,如果我们也想要映射的实体值,则使用 eager),并删除了 @JoinColumn(hibernate by默认在评论 table),
中创建字段“task_id”我的 Json 评论对象是,
{
"comment": "Some text",
"task": {
"id":1
}
}