Spring boot@OneToMany关联使用null值保存id


  • 大家好,我不知道为什么,但当我试图定义一个关联时@一对多我在service_id和question_id中得到一个null。

第一个实体

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Inheritance(strategy = InheritanceType.JOINED)
public class Services implements Serializable{
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(nullable = false, updatable = false)
private Long id;
private String serviceName;
private String description;
private String image;
@OneToMany(cascade = CascadeType.ALL)
private List<Questions> questions;

第二实体

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Questions implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(nullable = false, updatable = false)
private Long id;
private String question;
@Column(name="service_id")
private String service_id;
@OneToMany(cascade = CascadeType.ALL)
private List<Answers> answers;

第三实体

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Answers implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, updatable = false)
private Long id;
private String answer;
private double cost;
@Column(name="question_id")
private String question_id;

您可以在下面的表格中找到图像

在此处输入图像描述

您所需要做的就是从两个模型中删除以下行:

@Column(nullable = false, updatable = false)

此行之后:@GeneratedValue(strategy = GenerationType.IDENTITY)

这将解决你的问题。

最新更新