[英]JPA: Is it possible for the target of a relationship to be a non entity?
The following code is in Kotlin. 以下代码在Kotlin中。
I've a TestExecutionSummary
entity 我有一个TestExecutionSummary
实体
@Entity
class TestExecutionSummary : Serializable {
@EmbeddedId
lateinit var id: TestExecutionId
...
@OneToMany(cascade = [CascadeType.ALL], mappedBy = "testExecutionSummary")
var failures: List<TestFailure> = mutableListOf()
}
@Embeddable
data class TestExecutionId(
var stepExecutionId: Long,
var jobExecutionId: Long
) : Serializable
and a TestFailure
entity 和一个TestFailure
实体
@Entity
class TestFailure : Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
var id: Long? = null
@Column(length = 999)
lateinit var testId: String
@JoinColumns(
JoinColumn(name = "stepExecutionId", referencedColumnName = "stepExecutionId"),
JoinColumn(name = "jobExecutionId", referencedColumnName = "jobExecutionId")
)
@ManyToOne
lateinit var testExecutionSummary: TestExecutionSummary
@Column(length = 999)
var message: String? = null
}
This works fine, but what's bugging me is that TestFailure
has no independent existence, and doesn't really qualify as an entity IMO. 这很好用,但令我TestFailure
是TestFailure
没有独立存在,并且实际上没有资格作为实体IMO。 Is it possible to maintain the shown relationship without making TestFailure
an entity? 是否可以在不使TestFailure
成为实体的情况下保持所示的关系?
JPA 2.0 has ElementCollection which is exactly what I'm looking for. JPA 2.0具有ElementCollection ,这正是我想要的。
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(
name = "TEST_FAILURE",
joinColumns = [
JoinColumn(name = "stepExecutionId", referencedColumnName = "stepExecutionId"),
JoinColumn(name = "jobExecutionId", referencedColumnName = "jobExecutionId")
]
)
var failures: List<TestFailure> = mutableListOf()
@Embeddable
class TestFailure : Serializable {
@Column(length = 999)
lateinit var testId: String
@Column(length = 999)
var message: String? = null
}
You are asking for @Embeddable but it is not available for @OneToMany. 您要求使用@Embeddable,但不适用于@OneToMany。 See Mapping Single Table to Embeddable Collection in JPA for workaround 解决方法,请参阅在JPA中将单个表映射到可嵌入集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.