本文介绍了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:

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

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


推荐答案

由于 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递归的其他方法是使用和。但是我更喜欢 @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.



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中的一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:02