简体   繁体   English

JPA:关系的目标是否有可能成为非实体?

[英]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. 这很好用,但令我TestFailureTestFailure没有独立存在,并且实际上没有资格作为实体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.

相关问题 EclipseLink JPA在关系属性中使用非实体作为目标实体 - EclipseLink JPA uses a non-entity as target entity in the relationship attribute JPA实体关系? - JPA Entity relationship? JPA 可重用实体和关系 - JPA reusable entity and relationship 在JPA中,是否可以使用非pk字段合并实体以更新行 - in JPA is it possible to merge an entity using a non pk field to update the row Java EE - [类<ClassName] uses a non-entity [class <ClassName> ] 作为关系属性中的目标实体 - Java EE - [class <ClassName] uses a non-entity [class <ClassName>] as target entity in the relationship attribute 异常描述:xxx 使用非实体 xx 作为关系属性 [方法 xxx] 中的目标实体 - Exception Description: xxx uses a non-entity xx as target entity in the relationship attribute [method xxx] EclipseLink ValidationException-非实体[class long]作为关系属性[field providerId]中的目标实体 - EclipseLink ValidationException - non-entity [class long] as target entity in the relationship attribute [field providerId] Java Persistence api:class在关系属性中使用非实体类作为目标实体 - Java Persistence api: class uses non-entity class as target entity in the relationship attribute ValidationException异常描述:在关系属性中使用非实体作为目标实体 - ValidationException Exception Description: uses a non-entity as target entity in the relationship attribute 关系表映射为 JPA 中的实体 - Relationship table mapped as entity in JPA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM