英文:
Foreign key is null : Hibernate Spring
问题
我试图将 Run 对象保存到数据库中。我在 Run 和 City 之间定义了关联关系。一个城市可以有多个跑步记录。我遇到了关于 city_id 的问题,它是空的。
City 实体和控制器:
City
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "cities")
public class City {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "city_id")
private long id;
@OneToMany(mappedBy = "city", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Run> runs = new ArrayList<>();
private String name;
}
Run
@Entity
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "runs")
public class Run {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name_run")
private String nameRun;
@Column(name = "distance")
private double distance;
@Column(name = "date")
private Date date;
@Column(name = "my_time")
private String myTime;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "city_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private City city;
}
Controller
@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);
}
}
我想将运动记录保存到数据库中。我的测试请求如下:
{
"nameRun": "test",
"distance": "5.0",
"date": "2020-12-12",
"myTime": "50:40",
"city": "test1"
}
从 Intelijj 中评估表达式的结果如下:
为什么 City 为 null?在映射中是否存在错误?
英文:
I try to save object Run to database. I defined relation between Run and City. One city could have many runs. I got problem with city_id. Is null.
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
My entieties and controller:
City
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "cities")
public class City {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "city_id")
private long id;
@OneToMany(mappedBy = "city", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Run> runs = new ArrayList<>();
private String name;
}
Run
@Entity
@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "runs")
public class Run {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name_run")
private String nameRun;
@Column(name = "distance")
private double distance;
@Column(name = "date")
private Date date;
@Column(name = "my_time")
private String myTime;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "city_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private City city;
}
Controller
@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);
}
}
I would like to save the run in DB.
My test request looks like :
{
"nameRun": "test",
"distance":"5.0",
"date":"2020-12-12",
"myTime":"50:40",
"city":"test1"
}
Result from evaluate expresion in Intelijj:
Why the City = null? Is here error in mapping?
答案1
得分: 1
这是你要的翻译内容:
你能尝试一下这个 JSON,但你需要在 JSON 中传递城市 ID。
{
"nameRun": "test",
"distance": "5.0",
"date": "2020-12-12",
"myTime": "50:40",
"city": {
"id": 1,
"name": "test1"
}
}
谢谢
英文:
Can you try with this json but you need to pass city id in json.
{
"nameRun": "test",
"distance": "5.0",
"date": "2020-12-12",
"myTime": "50:40",
"city": {
"id": 1,
"name": "test1"
}
}
Thanks
答案2
得分: 0
首先,请使用 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;
}
当保存 Run
时,需要设置 city_id
。
最简单的方法是创建一个临时的虚拟 City
对象,并将 id 设置给它。
City city = new City();
city.setId(1L);
Run run = new Run();
run.setCity(city);
repository.save(run);
显然,你的数据库中应该有一个 id 为 1L 的城市记录。
其他选项包括:
- 使用类似于 Hibernate 中的
session.load()
方法,结合 Spring repository,可以创建一个不需要从数据库加载的City
对象。 - 根据 id 完全加载
City
实体。
英文:
First of all, use Long
for id please. It is better to add @Entity
annotation too.
@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;
}
You need to set city_id
when you save Run
.
The simplest way to do that is just create a fake transient City
and set id to it.
City city = new City();
city.setId(1L);
Run run = new Run();
run.setCity(city);
repository.save(run);
Obviously you should have a city with id
1L in the database.
Other options are
- Use something like
session.load()
Hibernate analogue with Spring repository to createCity
without loading it from datatbase. - Load
City
entity entirely by id.
答案3
得分: 0
if you wanna save any run class,
Run run = new Run();
City city = new City();
city.getRuns().add(run);
runRepository.save(run);
if you wanna save any run class, first you need to insert to (Arraylist) runs variable of city class like **city.getRuns().add(run)** after filling run then you can runRepository.save(run).
Also my samples are here. You can look at myclasses.
First class is called **Patient** .
@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;
}
Second Class called **Problem** is this one.
@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;
}
英文:
if you wanna save any run class,
Run run = new Run();
City city = new City();
city.getRuns().add(run);
runRepository.save(run);
if you wanna save any run class, first you need to insert to (Arraylist) runs variable of city class like city.getRuns().add(run) after filling run then you can runRepository.save(run).
Also my samples are here. You can look at myclasses.
First class is called Patient .
@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;
}
Second Class called Problem is this one.
@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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论