英文:
Values are not saved in the database
问题
为什么我发送了数值,但id_house和id_tenant仍然为null?日期已正确保存。如何为公寓设置租户,以及正在租用哪间公寓?
我接受以下数值:
@PostMapping("/rent")
@PreAuthorize("hasAuthority('user:write')")
public void homeRent(@RequestBody Contract contract) {
contractRepository.save(contract);
}
合同类:
@Data
@Entity
@Table(name = "contract", schema = "public")
public class Contract {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@ManyToOne
@JoinColumn(name = "id_house")
@ToString.Exclude
@EqualsAndHashCode.Exclude
@JsonIgnore
private House house;
@ManyToOne
@JoinColumn(name = "id_tenant")
@EqualsAndHashCode.Exclude
@ToString.Exclude
@JsonIgnore
private User user;
@Column(name = "end_date")
private LocalDate end_date;
@Column(name = "start_date")
private LocalDate start_date;
}
Postman 请求示例:
{
"start_date": "2020-03-12",
"end_date": "2020-03-19",
"id_house": 2,
"id_tenant": 3
}
英文:
Why do I have null id_house and id_tenant if I send values? The date is saved correctly. How do I set the tenant for an apartment and which apartment is being rented?
I accept values:
@PostMapping("/rent")
@PreAuthorize("hasAuthority('user:write')")
public void homeRent(@RequestBody Contract contract) {
contractRepository.save(contract);
}
Contract:
@Data
@Entity
@Table(name = "contract", schema = "public")
public class Contract {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@ManyToOne
@JoinColumn(name = "id_house")
@ToString.Exclude
@EqualsAndHashCode.Exclude
@JsonIgnore
private House house;
@ManyToOne
@JoinColumn(name = "id_tenant")
@EqualsAndHashCode.Exclude
@ToString.Exclude
@JsonIgnore
private User user;
@Column(name = "end_date")
private LocalDate end_date;
@Column(name = "start_date")
private LocalDate start_date;
}
Postman:
{
"start_date" : "2020-03-12",
"end_date" : "2020-03-19",
"id_house" : 2,
"id_tenant" : 3
}
答案1
得分: 0
首先,我看到的问题是,为什么在你的JSON请求中有一个 id_tenant
,而实际上应该是 user
?请记住,id_tenant
只是列名,不是字段名。
另外,你应该提供一个对象 {}
,而不是整数作为 user
,所以你的请求会类似于
{
...
"user": {"id": 1....}
...
}
英文:
First what I see wrong is why do you have id_tenant
in your JSON request when it is actually user
? Keep in mind id_tenant
is just column name, not field name.
Also you should provide object {}
not integer for user
so your request would look something like
{
...
"user": {"id": 1....}
...
}
答案2
得分: 0
@PostMapping("/rent")
@PreAuthorize("hasAuthority('user:write')")
public void homeRent(@RequestBody ContractDto contractDto) {
Contract contract = new Contract();
House house = houseRepository.findById(contractDto.getId_house()).orElseThrow();
User user = userRepository.findById(contractDto.getId_tenant()).orElseThrow();
contract.setHouse(house);
contract.setUser(user);
contract.setStart_date(contractDto.getStart_date());
contract.setEnd_date(contractDto.getEnd_date());
contractRepository.save(contract);
}
英文:
@PostMapping("/rent")
@PreAuthorize("hasAuthority('user:write')")
public void homeRent(@RequestBody ContractDto contractDto) {
Contract contract = new Contract();
House house = houseRepository.findById(contractDto.getId_house()).orElseThrow();
User user = userRepository.findById(contractDto.getId_tenant()).orElseThrow();
contract.setHouse(house);
contract.setUser(user);
contract.setStart_date(contractDto.getStart_date());
contract.setEnd_date(contractDto.getEnd_date());
contractRepository.save(contract);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论