繁体   English   中英

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
}

这很好用,但令我TestFailureTestFailure没有独立存在,并且实际上没有资格作为实体IMO。 是否可以在不使TestFailure成为实体的情况下保持所示的关系?

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
}

您要求使用@Embeddable,但不适用于@OneToMany。 解决方法,请参阅在JPA中将单个表映射到可嵌入集合。

暂无
暂无

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

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