Spring boot @OneToMany 关联用 null 值保存 id
- <块引用>
大家好,我不知道为什么,但是当我尝试定义关联时 @一对多我在service_id和question_id中得到一个空值。
第一个实体
@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;
您可以找到下表的图像
Hello guys i don't know why but when i tried to define an association
@one to many i get a null in the service_id and question_id.
first entity
@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;
Second entity
@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;
third entity
@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;
you can find the image for the tables bellow
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要做的就是从两个模型中删除以下行:
在此行之后:
@GenerateValue(strategy = GenerationType.IDENTITY)
这将解决您的问题。
All you need to do is remove the following line from Both of your models:
After this line:
@GeneratedValue(strategy = GenerationType.IDENTITY)
That will solve your problem.