简体   繁体   English

如何在 Spring Boot 的 @JoinColumn 列中插入数据?

[英]How do I insert data in a @JoinColumn column in Spring Boot?

When I pass data by POST request to my TodoItem model, only the columns specified by @column get filled in, but the @JoinColumn column is null even if I use the column name in the JSON I'm sending over.当我通过 POST 请求将数据传递给我的 TodoItem model 时,只有@column指定的列被填写,但@JoinColumn列是null即使我使用 Z0ECD11C1D7A287401FZD1'A 中的列名发送。 My GET request API work just fine however, so I omitted them from controller code.我的 GET 请求 API 工作得很好,所以我从 controller 代码中省略了它们。

TodoItem.java TodoItem.java

@Entity
@Table(name = "todo_item")
public class TodoItem {

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

@Column(name = "todo")
private String todo;

@Column(name = "completed")
private boolean completed;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

public TodoItem() {
}

public TodoItem(String todo, boolean completed) {
    this.todo = todo;
    this.completed = completed;
}

// setters and getters
}

My constructor doesn't mention user_id , don't think it needs it though, but I may be wrong.我的构造函数没有提到user_id ,但不认为它需要它,但我可能错了。

TodoController.java TodoController.java

@RestController
@RequestMapping("/api")
public class TodoController {

@Autowired
private UserRepository userRepository;

@Autowired
private TodoRepository todoRepository;

@PostMapping("/addItem")
public TodoItem addTodoItem(@RequestBody TodoItem todoItem) {
    return this.todoRepository.save(todoItem);
}
}

I send POST to http://localhost:8080/api/addItem我发送POSThttp://localhost:8080/api/addItem

Request:要求:

{
  "todo": "finish portfolio",
  "completed": false,
  "user_id": 1
}

However in my MySQL workbench, todo and completed get populated properly but user_id says null但是在我的 MySQL 工作台中, todocompleted得到正确填充,但user_idnull

In spring data (using hibernate or JPA), when saving entity which has reference to another entity.在 spring 数据中(使用 hibernate 或 JPA),当保存引用另一个实体的实体时。 you must first fetch the referenced object first by id and then set it using setter in persistence entity.您必须首先通过 id 获取引用的 object,然后在持久性实体中使用 setter 设置它。 after that you can save and get (FK column) to be saved.之后,您可以保存并获取(FK 列)进行保存。

for example you first must use user repository and call例如,您首先必须使用用户存储库并调用

User user = userRepository.findById(userId);

and then接着

todoItem.setUser(user);

after that you can save item and FK column will get populated.之后,您可以保存项目,FK 列将被填充。

I think this the way to save reference entity.我认为这是保存参考实体的方法。 you can not relay on int id only.您不能仅在 int id 上中继。

also your request body JSON must be like this:您的请求正文 JSON 也必须是这样的:

{
   "todo": "finish portfolio",
   "completed": false,
   "user": {
      "user_id":1
    }

} }

also it best practice to define and use DTO object instead of entity itself.最佳实践也是定义和使用 DTO object 而不是实体本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM