JPA在Kotlin和Glassfish中以双向关系无限递归

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JPA在Kotlin和Glassfish中以双向关系无限递归相关的知识,希望对你有一定的参考价值。

我在kotlin中有2个数据类,每个类都有相互参考。 ProfileKweet(代码将在底部)。当使用EntityManager获取其中一个实体时,它可以成功获取单个对象。然而,它永远不会返回,因为JPA在后台持续获取递归关系。

当调用ProfileDao.getByIdProfileDao.getByScreenname时会出现问题。

Profile.kt

@Entity(name = "profile")
@NamedQueries(
(NamedQuery(name = "Profile.getByScreenName", query = "select p from profile p where p.screenname LIKE :screenname")),
(NamedQuery(name = "Profile.getAll", query = "select p from profile p"))
)
data class Profile(
@Id
@GeneratedValue
var id: Int? = null,

var screenname: String,

var created: Timestamp
) {

@OneToMany(mappedBy = "profile", fetch = FetchType.LAZY, cascade = [CascadeType.DETACH])
var kweets: List<Kweet> = emptyList()

@ManyToMany(fetch = FetchType.LAZY, cascade = [CascadeType.DETACH])
@JoinTable(
    name = "liked_kweets",
    joinColumns = [(JoinColumn(name = "profile_id", referencedColumnName = "id"))],
    inverseJoinColumns = [(JoinColumn(name = "kweet_id", referencedColumnName = "id"))]
)
var likes: List<Kweet> = emptyList()

@ManyToMany(fetch = FetchType.LAZY, cascade = [CascadeType.DETACH])
@JoinTable(
    name = "follows",
    joinColumns = [(JoinColumn(name = "follower_id", referencedColumnName = "id"))],
    inverseJoinColumns = [(JoinColumn(name = "followed_id", referencedColumnName = "id"))]
)
var follows: List<Profile> = emptyList()

@ManyToMany(mappedBy = "follows", fetch = FetchType.LAZY, cascade = [CascadeType.DETACH])
var followers: List<Profile> = emptyList()
}

Kweet.kt

@Entity(name = "kweet")
@NamedQuery(name = "Kweet.getAll", query = "select k from kweet k")
data class Kweet(
@Id
@GeneratedValue()
var Id: Int? = null,
var created: Timestamp,
var message: String,
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "profile_id")
@JsonBackReference
var profile: Profile,
@ManyToMany(mappedBy = "likes", fetch = FetchType.LAZY)
@JsonBackReference
var likedBy: List<Profile> = emptyList()
)

ProfileDao.kt

@Stateless
class ProfileDao {
@PersistenceContext
lateinit var em: EntityManager

fun getById(id: Int) = em.find(Profile::class.java, id)

fun getAll(): List<Profile> = em.createNamedQuery("Profile.getAll", Profile::class.java).resultList

fun getByScreenname(name: String) = em.createNamedQuery("Profile.getByScreenName", Profile::class.java)
    .setParameter("screenname", name)
    .resultList
    .firstOrNull()

fun create(profile: Profile) = em.persist(profile)

fun follow(follower: Profile, leader: Profile) {
    follower.follows += leader
    leader.followers += follower
    em.persist(follower)
    em.persist(leader)
}
}

更新:添加DTO并将其标记为正确打开可解决递归错误。例:

@Open
class ProfileFacade(
private val profile: Profile
) : Serializable {
var screenname: String
    get() = profile.screenname
    set(value) {
        profile.screenname = value
    }

var kweets: List<SimpleKweetFacade>
    get() = profile.kweets.map { SimpleKweetFacade(it) }
    set(value) = Unit

var follows: List<String>
    get() = profile.follows.map { it.screenname }
    set(value) = Unit

var created: Timestamp
    get() = profile.created
    set(value) = Unit
}

@Open注释是一个简单的annotation class Open(),然后由gradle处理以添加open和noarg构造函数

答案

您应该使用DTO来表示前端的数据,或者使用来自子对象的@JsonIgnore父引用(而不是@JsonBackReference)。

使用DTO可能是一个更明智的选择,因为您可以将前端表示与后端模型分离,这使您在两个层中都具有灵活性(即,更改一个层不会破坏/可能在另一个层中引入错误)。

以上是关于JPA在Kotlin和Glassfish中以双向关系无限递归的主要内容,如果未能解决你的问题,请参考以下文章

Glassfish - 无法使用 JPA 删除实体

Glassfish 4 JDBC 资源连接失败,Hibernate 4.3.5 和 JPA 2.1 连接分配由于数据库“null”而被拒绝

为什么我的应用程序客户端在GlassFish 4.1.2应用程序上更新JPA实体时会报告StreamCorruptedException?

如何将 Hibernate 配置为 Payara/Glassfish 4.1 的 JPA 提供程序? [关闭]

jpa hibernate中扫描太多实体时,glassfish启动很慢或内存泄漏

ios icloud 在设备中以编程方式设置开/关