由中间表映射的多对多关系中的无限 JSON

问题描述

我有 2 个相互关联的实体。这 2 个实体应该在多对多关系中相互映射,但是,我还需要有它们各自关系的时间戳(发生时),因此我尝试使用中间表映射它们。>

最初,关系是一对多的,但我意识到我实际上需要多对多,因为业务逻辑需要这样做。结构还是一样的,因为有父子关系,但是这次,一个孩子也应该有多个父母。

My BaseEntity 是一个抽象类,包含所有其他实体中存在的字段:

@Data
@MappedSuperclass
public abstract class BaseEntity {

    @Id
    @Min(100)
    @Max(Integer.MAX_VALUE)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Long id;

    @CreationTimestamp
    @Column(name = "Created_At",updatable = false)
    protected zoneddatetime createdDate;

    @UpdateTimestamp
    @Column(name = "Updated_At")
    protected zoneddatetime updatedDate;

    @NotNull
    @Column(name = "Is_Active")
    protected Boolean active = true;
}

然后我有我的 2 个实体,它们应该以多对多的方式关联。这是我的第一个实体,应该是父实体:

@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "User")
@EqualsAndHashCode(callSuper = true)
@TypeDefs( {
               @TypeDef(name = "json",typeClass = JsonStringType.class),@TypeDef(name = "jsonb",typeClass = JsonBinaryType.class)
           })
public class UserEntity extends BaseEntity {

    @NotBlank
    @Column(name = "User_Name",columnDeFinition = "varchar(255) default 'N/A'")
    private String userName;

    @Nullable
    @JoinColumn(name = "User_Id")
    @OnetoMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    private List<UserRole> roleList = new ArrayList<>();
}

我的第二个实体被视为子实体:

@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "Role")
@Where(clause = "is_active = true")
@EqualsAndHashCode(callSuper = true)
public class RoleEntity extends BaseEntity {

    @NotBlank
    @Column(name = "Name")
    private String name;

    @JsonIgnore
    @JoinColumn(name = "Role_Id")
    @OnetoMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    private List<UserRole> userList = new ArrayList<>();
}

我也有我的中介实体:

@Data
@Entity
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Where(clause = "is_active = true")
@EqualsAndHashCode(callSuper = true)
@Table(name = "User_Role",uniqueConstraints= @UniqueConstraint(columnNames={"User_Id","Role_Id"}))
public class UserRole extends BaseEntity {

    // Adding @JsonIgnore here will only cause an error
    @JoinColumn(name = "User_Id")
    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY,optional = false,targetEntity = UserEntity.class)
    private UserEntity user;

    @JoinColumn(name = "Role_Id")
    @ManyToOne(cascade = CascadeType.ALL,targetEntity = RoleEntity.class)
    private RoleEntity role;
}

现在的问题是,当我尝试获取我的 UserEntity 时,我得到了无限递归。

到目前为止,我已经尝试使用@JsonIgnore、@JsonManagedReference、@JsonBackReference,但它没有用,或者我根本不知道在哪里或如何正确使用它们。

回顾:

  • 由多对多关系映射的2个实体;
  • 使用中介实体和一对多 + 多对一关联实现多对多;
  • 显示我的 UserEntity 时得到递归;

更新:我使用我对此问题的回答中描述的不同方法设法解决了这个问题。

解决方法

我通过实现复合键结构并仅使用 @JsonIgnore 注释来解决此问题:

@Getter
@Setter
@Embeddable
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class UserRoleKey implements Serializable {
    @Column(name = "User_Id")
    Long userId;

    @Column(name = "Role_Id")
    Long roleId;
}

这将在中间实体中使用,现在不再使用我的 BaseEntity。

@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "User_Role",uniqueConstraints= @UniqueConstraint(columnNames={"User_Id","Role_Id"}))
public class UserRole {

    @JsonIgnore
    @EmbeddedId
    private UserRoleKey id;

    @JsonIgnore
    @MapsId("userId")
    @JoinColumn(name = "User_Id")
    @ManyToOne(optional = false,targetEntity = UserEntity.class)
    private UserEntity user;

    @MapsId("roleId")
    @JoinColumn(name = "Role_Id")
    @ManyToOne(optional = false,targetEntity = RoleEntity.class)
    private RoleEntity role;

    @CreationTimestamp
    @Column(name = "Created_At",updatable = false)
    private ZonedDateTime createdDate;
}

现在,对于我的两个实体,我有这个定义:

UserEntity 类(角色定义):

@Nullable
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,mappedBy = "user",orphanRemoval = true)
private List<UserRole> roleList = new ArrayList<>();

RoleEntity 类(用户定义)

@Nullable
@JsonIgnore
@OneToMany(cascade = CascadeType.ALL,mappedBy = "role",orphanRemoval = true)
private List<UserRole> userList = new ArrayList<>();

这似乎有效,不再返回无限的 JSON 递归。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...