JPA:关系的目标有可能是非实体吗?
JPA: Is it possible for the target of a relationship to be a non entity?
以下代码使用 Kotlin。
我有一个 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
和一个 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
}
这很好用,但令我烦恼的是 TestFailure
没有独立存在,并且不真正符合 IMO 实体的条件。是否可以在不使 TestFailure
成为实体的情况下保持所示关系?
您正在请求@Embeddable,但它不适用于@OneToMany。
请参阅 Mapping Single Table to Embeddable Collection in JPA 解决方法
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
}
以下代码使用 Kotlin。
我有一个 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
和一个 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
}
这很好用,但令我烦恼的是 TestFailure
没有独立存在,并且不真正符合 IMO 实体的条件。是否可以在不使 TestFailure
成为实体的情况下保持所示关系?
您正在请求@Embeddable,但它不适用于@OneToMany。 请参阅 Mapping Single Table to Embeddable Collection in JPA 解决方法
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
}