简体   繁体   English

休眠@OneToMany联接表抛出StackOverflowException

[英]Hibernate @OneToMany join table throws StackOverflowException

I'm working on a hibernate entity relationship that is set up as follows: 我正在建立如下的休眠实体关系:

A user can create assignments. 用户可以创建分配。 The user can split the assignment with other users, giving them tasks. 用户可以与其他用户拆分任务,从而为他们分配任务。 My goal is to have a user that is as follows: 我的目标是拥有如下用户:

{ id: 1, assignments: [{name: 'test'}], partialAssignment: [{task: 'bla'}]} {id:1,作业:[{name:'test'}],partialAssignment:[{task:'bla'}]}

And assignments like this: {id: 1, user: {...}, assignedUsers: [{...}], ...} 分配如下:{id:1,user:{...},AssignedUsers:[{...}],...}

Assignment: 分配:

    @Entity
    @Table(name = "Assignment")
    data class Assignment (

      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "id", nullable = false, updatable = false)
      val id: Long? = null,

      @Column(name = "name")
      val name: String? = null,

      @Column(name = "dueDate")
      @Temporal(TemporalType.TIMESTAMP)
      val dueDate: Date? = null,

      @ManyToOne
      @JoinColumn(name = "userId")
      var user: User? = null,

      @OneToMany(mappedBy = "assignment")
      var assignedUsers: List<AssignmentUser> = emptyList()
    )

User 用户

@Entity
@Table(name = "Users")
data class User (

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, updatable = false)
    @JsonView(UserView.UserSummary::class)
    val id: Long? = null,

    @Column(name = "email", nullable = false)
    @JsonView(UserView.UserSummary::class)
    val email: String = "",

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

    @OneToMany(mappedBy = "user")
    val assignments: List<Assignment> = emptyList(),

    @OneToMany(mappedBy = "user")
    val partialAssignment: List<AssignmentUser> = emptyList()
)

AssignmentUser AssignmentUser

@Entity
@Table(name = "AssignmentUsers")
data class AssignmentUser(
    @Column(name = "task")
    val task: String? = null,

    @Id
    @ManyToOne(cascade = arrayOf(CascadeType.ALL))
    @JoinColumn(name = "userId")
    var user: User? = null,

    @Id
    @ManyToOne(cascade = arrayOf(CascadeType.ALL))
    @JoinColumn(name = "assignmentId")
    var assignment: Assignment? = null
) : Serializable {

}

When I fetch the Assignment class using a JPARepository .findOne(id) call, I get a StackOverflow exception on the Assignment.toString() method. 当我使用JPARepository .findOne(id)调用获取Assignment类时,在Assignment.toString()方法上收到了StackOverflow异常。 I don't understand why, as it worked fine until I added the psuedo ManyToMany relationship (assignedUsers). 我不明白为什么,因为在我添加psuedo ManyToMany关系(assignedUsers)之前,它工作得很好。

Any thoughts? 有什么想法吗?

Here problem is not excatly with @OneToMany. @OneToMany并不是问题所在。

In case of serialization (in this case printing object's string form) the stated code will go in cyclic loop. 如果进行序列化(在这种情况下为打印对象的字符串形式),则说明的代码将循环循环。

In stated code snippet cyclic relationships are as follow: 1. Assignment has List 2. Assignement has User 3. User has List 4. User has List 5. AssignmentUser has User 6. AssignmentUser has Assignment 在所述代码段中,循环关系如下:1.分配具有列表2.分配具有用户3.用户具有列表4.用户具有列表5. AssignmentUser具有用户6. AssignmentUser具有Assignment

In hibernate all mappings are by default set to LAZY, hence it will not cause problem bacause we will not load associated object unless needed. 在休眠状态下,所有映射默认情况下都设置为LAZY,因此不会造成问题,因为除非需要,否则我们不会加载关联的对象。

But, in case of serialization it will try to load associated objects, and hence to break this we need to implement the custom serialization. 但是,在序列化的情况下,它将尝试加载关联的对象,因此,要打破这一点,我们需要实现自定义序列化。 In your case you need to override the toString method of above objects properly ie objects should not be loaded unnecessarily. 在您的情况下,您需要正确地覆盖上述对象的toString方法,即,不应不必要地加载对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM