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

发布于 2025-01-11 03:36:30 字数 1661 浏览 0 评论 0原文

  • <块引用>

    大家好,我不知道为什么,但是当我尝试定义关联时 @一对多我在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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

计㈡愣 2025-01-18 03:36:30

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

@Column(nullable = false, updatable = false)

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

这将解决您的问题。

All you need to do is remove the following line from Both of your models:

@Column(nullable = false, updatable = false)

After this line: @GeneratedValue(strategy = GenerationType.IDENTITY)

That will solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文