pokemans
pokemans

Reputation: 98

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'}]}

And assignments like this: {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

@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. I don't understand why, as it worked fine until I added the psuedo ManyToMany relationship (assignedUsers).

Any thoughts?

Upvotes: 0

Views: 515

Answers (1)

nkharche
nkharche

Reputation: 1027

Here problem is not excatly with @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

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.

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 i.e. objects should not be loaded unnecessarily.

Upvotes: 1

Related Questions