我的工作,它被設置如下Hibernate的實體關係:休眠@OneToMany連接表拋出StackOverflowException
用戶可以創建任務。用戶可以與其他用戶分配任務,給他們任務。我的目標是有如下用戶:
{ID:1,作業:[{名稱: '測試'}],partialAssignment:[{任務: '喇嘛'}]}
而且分配是這樣的: {ID:1,用戶:{...},assignedUsers:[{...}],...}
分配:
@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()
)
用戶
@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 {
}
當我取使用JPARepository .findOne(ID)調用的分配類的,我上Assignment.toString()方法的StackOverflow例外。我不明白爲什麼,因爲它工作正常,直到我添加了許多ManyToMany關係(assignedUsers)。
有什麼想法?
我不想序列化對象呢。只要取它。我不明白爲什麼JpaRepository甚至調用toString()? – pokemans
這工作,我只是不明白爲什麼它這樣做。 – pokemans
如果您將此對象發送給視圖並在javascript中使用它,則視圖引擎(thymeleaf?)將序列化該對象。 – sedooe