为什么双向 ManyToOne 会导致 Hibernate 中的循环依赖?

2024-03-05

我的项目中有实体(基于Spring-Boot + Hibernate):

@Entity
@Table(name = "user_account")
public class UserAccount {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @NotNull
    @Column(name = "username")
    private String userName;

    @NotNull
    @Column(name = "first_name")
    private String firstName;

    @NotNull
    @Column(name = "last_name")
    private String lastName;

    @NotNull
    @Column(name = "password")
    private String password;

    @CreationTimestamp
    @Column(name = "birthday")
    private LocalDateTime birthday;

    @NotNull
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "role", referencedColumnName = "id")
    private UserRole role;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "userAccount", cascade= CascadeType.ALL)
    private Set<RentInfo> rents;
}

and

@Entity
@Table(name = "rent_info")
public class RentInfo {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @NotNull
    @ManyToOne(cascade= CascadeType.ALL)
    @JoinColumn(name="user_id")
    private UserAccount userAccount;

    @CreationTimestamp
    @Column(name = "start_date")
    private LocalDateTime startDate;

    @CreationTimestamp
    @Column(name = "end_date")
    private LocalDateTime endDate;

    @Column(name = "status")
    private int status;
}

我想创建双向一对多关系,当一个用户可以有多个租金并且我们可以通过具体的 user_id 选择租金时,但出现问题

作为回应,我得到这个:

{
        "id": 1,
        "userName": "[email protected] /cdn-cgi/l/email-protection",
        "firstName": "fName",
        "lastName": "lName",
        "password": "test",
        "birthday": "2001-11-03T14:28:14",
        "role": {
            "name": "CLIENT"
        },
        "rents": [
            {
                "userAccount": {
                    "id": 1,
                    "userName": "[email protected] /cdn-cgi/l/email-protection",
                    "firstName": "fName",
                    "lastName": "lName",
                    "password": "test",
                    "birthday": "2001-11-03T14:28:14",
                    "role": {
                        "name": "CLIENT"
                    },
                    "rents": [
                        {
                            "userAccount": {
                                "id": 1,
                                "userName": "[email protected] /cdn-cgi/l/email-protection",
                                "firstName": "fName",
                                "lastName": "lName",
                                "password": "test",
                                "birthday": "2001-11-03T14:28:14",
                                "role": {
                                    "name": "CLIENT"
                                }
    .....

这是无限的,从逻辑上讲,我有内存不足错误。我怎样才能解决这个问题?我做错了什么?


您有两种解决方案:

  1. 在 @ManyToOne 上使用 @JsonIgnore
  2. 不要序列化您的实体。请改用 DTO,并在映射时小心以避免循环依赖
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么双向 ManyToOne 会导致 Hibernate 中的循环依赖? 的相关文章

随机推荐