小编And*_*dre的帖子

使用 @ManyToMany 时出现 StackOverflowError

在尝试并谷歌搜索类似问题数小时后,我仍然无法解决问题:

我正在构建一个使用 JPA(因此也是休眠)和 H2(不认为它相关)的冲刺启动应用程序(在 Kotlin 中)。我想对 User 和 Achievement 类之间的多对多关系进行建模(因此一个用户可以拥有多个成就,多个用户可以实现一个成就)。以下是模型类:

@Entity
data class User(

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        val id: Int = 0,

        @Column(nullable = false)
        val name: String,

        @ManyToMany(cascade = [ CascadeType.PERSIST, CascadeType.MERGE ])
        val achievements: MutableSet<Achievement> = HashSet()

)
Run Code Online (Sandbox Code Playgroud)
@Entity
data class Achievement(

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        val id: Int = 0,

        val key: String,

        @ManyToMany(mappedBy = "achievements")
        val users: MutableSet<User> = HashSet()

)
Run Code Online (Sandbox Code Playgroud)

当我看到以下日志时,此模型似乎工作正常:

Hibernate: create table achievement (id integer not null, key varchar(255), priority integer not …
Run Code Online (Sandbox Code Playgroud)

hibernate jpa h2 kotlin spring-boot

2
推荐指数
1
解决办法
721
查看次数

标签 统计

h2 ×1

hibernate ×1

jpa ×1

kotlin ×1

spring-boot ×1