有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何避免递归实体的溢出错误[Hibernate 5,MySQL 8]

我的问题在于如何获取实体字段中的递归表,而不获取“目标VM中发生异常:java.lang.StackOverflowerr”

我有一个实现递归表的活动类:

@Entity
@Table(name = "activities")
public class Activity {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "activity_id")
    private Long id;

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

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "parent_id")
    private Activity parent;

    @OneToMany(mappedBy="parent", cascade = CascadeType.ALL)
    private Set<Activity> childs;

    @Override
    public String toString() {
        return "";
    }
}

以及具有多对多关系的权限类:

@Entity
@Table(name = "permissions")
public class Permission {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "permission_id")
    private Long id;

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

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_at")
    private Date createdAt;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "updated_at")
    private Date updatedAt;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "permission_activity", joinColumns = @JoinColumn(name = "permission_id"), inverseJoinColumns = @JoinColumn(name = "activity_id"))
    private Set<Activity> activities;

}

我使用一个JpaRepository抽象来加载数据库的数据,当我试图访问活动字段时,会出现溢出错误。 这是在检索活动字段时直接设置命名查询的另一种方法


共 (0) 个答案