Java 外键为空:Hibernate Spring
我尝试将对象运行保存到数据库。我定义了跑步和城市的关系。一个城市可能有很多次跑步。我对城市id有问题。为空Java 外键为空:Hibernate Spring,java,hibernate,hibernate-mapping,Java,Hibernate,Hibernate Mapping,我尝试将对象运行保存到数据库。我定义了跑步和城市的关系。一个城市可能有很多次跑步。我对城市id有问题。为空 Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: c
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
java.sql.SQLIntegrityConstraintViolationException: Column 'city_id' cannot be null
我的实体和控制员:
城市
控制器
@CrossOrigin
@RestController
@RequestMapping("/api/")
public class RunController {
private RunRepository runRepository;
private RunService runService;
public RunController(RunRepository runRepository, RunService runService) {
this.runRepository = runRepository;
this.runService = runService;
}
@GetMapping("runs")
public ResponseEntity<List<Run>> getRuns() {
return runService.getRuns();
}
@PostMapping("runs")
public ResponseEntity addRun(@RequestBody Run run) {
return new ResponseEntity<>(runRepository.save(run), HttpStatus.OK);
}
}
@交叉起源
@RestController
@请求映射(“/api/”)
公共类运行控制器{
私有运行库运行库;
私人运行服务;
公共RunController(RunRepository RunRepository、RunService RunService){
this.runRepository=runRepository;
this.runService=runService;
}
@GetMapping(“运行”)
公共响应getRuns(){
返回runService.getRuns();
}
@后映射(“运行”)
公共响应属性addRun(@RequestBody Run){
返回新的响应属性(runRepository.save(run),HttpStatus.OK);
}
}
我想在DB中保存运行。
我的测试请求如下所示:
{
“名称运行”:“测试”,
“距离”:“5.0”,
“日期”:“2020-12-12”,
“我的时间”:“50:40”,
“城市”:“测试1”
}
Intelijj中评估表达式的结果:
为什么城市=空?映射中有错误吗 首先,请使用
Long
作为id。最好也添加@Entity
注释
@Entity
public class City {
@Id
@GeneratedValue
private Long id;
@OneToMany(mappedBy = "city", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Run> runs = new ArrayList<>();
}
@Entity
public class Run {
@Id
@GeneratedValue
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private City city;
}
显然,数据库中应该有一个id为1L的城市
其他选择包括
使用类似于session.load()
Hibernate Simulation with Spring repository的方法创建City
,而无需从Database加载
完全按id加载城市
实体
如果你想保存任何跑步课程
Run run = new Run();
City city = new City();
city.getRuns().add(run);
runRepository.save(run);
如果要保存任何run类,首先需要插入city类的to(Arraylist)runs变量,如city.getRuns().add(run)在填充run之后,您可以运行Repository.save(run) 我的样品也在这里。你可以看看我的课程 第一类被称为病人
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@ToString
@Table(name = "aapatient")
public class Patient {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AA_PATIENT_SEQ")
@SequenceGenerator(sequenceName = "AA_PATIENT_SEQ", allocationSize = 1, name = "AA_PATIENT_SEQ")
@Column(name = "patientid")
private Long patientid;
private String name;
private String lastname;
@OneToMany(mappedBy = "patient", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Problem> problems;
}
您可以尝试使用此json,但需要在json中传递城市id
{
"nameRun": "test",
"distance": "5.0",
"date": "2020-12-12",
"myTime": "50:40",
"city": {
"id": 1,
"name": "test1"
}
}
谢谢您使用什么数据库?请提供您的
城市
和运行
表定义(ddl sql)。此请求无效。我得到的列“city\u id”不能为空
Run run = new Run();
City city = new City();
city.getRuns().add(run);
runRepository.save(run);
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@ToString
@Table(name = "aapatient")
public class Patient {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AA_PATIENT_SEQ")
@SequenceGenerator(sequenceName = "AA_PATIENT_SEQ", allocationSize = 1, name = "AA_PATIENT_SEQ")
@Column(name = "patientid")
private Long patientid;
private String name;
private String lastname;
@OneToMany(mappedBy = "patient", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Problem> problems;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Entity
@Table(name="aaproblem")
public class Problem{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AA_PATIENT_SEQ")
@SequenceGenerator(sequenceName = "AA_PATIENT_SEQ", allocationSize = 1, name = "AA_PATIENT_SEQ")
@Column(name = "problemid")
private Long problemid;
private String problemName;
private String problemDetail;
@Temporal(TemporalType.TIMESTAMP)
Date creationDate;
@NotNull
@ManyToOne(optional = true, fetch = FetchType.LAZY)
@JoinColumn(name = "patient_id")
private Patient patient;
}
{
"nameRun": "test",
"distance": "5.0",
"date": "2020-12-12",
"myTime": "50:40",
"city": {
"id": 1,
"name": "test1"
}
}