Springboot中的一对多关系 [英] One to many relationship in springboot

查看:496
本文介绍了Springboot中的一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为data.sql的文件,其中包含SQL查询 INSERT INTO。
我有一个表User哪个模型是:

I have a file named data.sql which contains SQL queries 'INSERT INTO'. I have table User which model is:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// other fields
@OneToMany(cascade = CascadeType.ALL,
        fetch = FetchType.LAZY,
        mappedBy = "user")
@Column(name = "vacations")
private Set<Vacation> vacations = new HashSet<>();

我有Vacation模型,其中:

And I have model Vacation where is:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@NotNull
@Column(name = "begin_date")
private LocalDateTime beginDateOfVacation;

@NotNull
@Column(name = "end_date")
private LocalDateTime endDateOfVacation;

@NotEmpty
@Column(name = "type")
private String typeOfVacation;

@NotEmpty
@Column(name = "reason")
private String reasonOfVacation;

@ManyToOne(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;

在我的data.sql中,我试图插入具有现有ID的Vacation用户。
它传递了编译器,但是在localhost上我只能看到:

And in my data.sql I am trying to insert into vacation User with existing ID. It "passing" threw compiler, but on localhost I can see only this:


无法编写JSON:无限递归( StackOverflowError);嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无限递归(StackOverflowError)(通过参考链:com.springproject27.springproject.user.User [ vacations])

Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.springproject27.springproject.user.User["vacations"])

这是H2数据库引擎,我尝试传递的查询是:

It is H2 Database Engine, the query which I try to pass is:


INSERT INTO VACATION(ID,BEGIN_DATE,END_DATE,TYPE,REASON,USER_ID)值
(22,'2012-09-17 18:47:52.69','2012-09-20 18:47:52.69','Unpaid离开','病',10);

INSERT INTO VACATION(ID,BEGIN_DATE,END_DATE,TYPE,REASON,USER_ID) VALUES (22,'2012-09-17 18:47:52.69','2012-09-20 18:47:52.69','Unpaid leave','Sick',10);


推荐答案

由于 User 实体&之间的双向关系而尝试序列化数据时的JSON递归假期实体。在序列化过程中使用 @JsonIgnoreProperties (如果使用的是Jackson 2.0+版本)来破坏递归的首选方法。

As you see its happening because of JSON recursion when you are trying to serialize your data because of bi-directional relationship between User entity & Vacation entity. Preferred method to use @JsonIgnoreProperties (if you are using Jackson 2.0+ version) to break the recursion during serialization.

注意:打破JSON递归的其他方法是使用 JsonBackReference JsonManagedReference 。但是我更喜欢 @JsonIgnoreProperties ,因为在序列化过程中不会丢失数据。

NOTE: Other way to break the JSON recursion is using JsonBackReference and JsonManagedReference in combiation. But I prefer @JsonIgnoreProperties due to no data loss during serialization.


正在使用 Lombok ,仅使用@Getter& @Setter注释不是@ToString(或包括@ToString的@Data),因为它会导致相同的递归问题。

If you are using Lombok, only use @Getter & @Setter annotation not @ToString (or @Data which has @ToString included), as it will cause the same recursion issue.



class User{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    // other fields
    @OneToMany(cascade = CascadeType.ALL,
            fetch = FetchType.LAZY,
            mappedBy = "user")
    @Column(name = "vacations")
    @JsonIgnoreProperties("user")
    private Set<Vacation> vacations = new HashSet<>();
}

class Vacation{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotNull
    @Column(name = "begin_date")
    private LocalDateTime beginDateOfVacation;

    @NotNull
    @Column(name = "end_date")
    private LocalDateTime endDateOfVacation;

    @NotEmpty
    @Column(name = "type")
    private String typeOfVacation;

    @NotEmpty
    @Column(name = "reason")
    private String reasonOfVacation;

    @ManyToOne(cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)
    @JsonIgnoreProperties("vacations")
    private User user;
}

这篇关于Springboot中的一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆