java - 在 JPA 中映射 Map<Entity, Entity> 结果表中不存在于该描述符中

标签 java hibernate jpa eclipselink jpa-2.0

我需要映射一个 Java Map,其中键和值都是 JPA 实体。我在 JPA 2 书籍 (Pro JPA 2) 中读到,这应该使用 @OneToMany@ManyToMany 注释而不是 ElementCollection 来完成,因为两者都是 JPA 实体。

我可以让 @ManyToMany 注释正常工作,并且没有任何属性或任何内容。但据我了解,这种关系是一对多的。对于任何值实体,只能有一个保存映射的类实体(我是否正确理解 @OneToMany 这里,因为它与映射相关?)。

只需使用 @OneToMany 注释 map ,如下所示:

@OneToMany
private Map<Stage, ScoreCard> scoreCards;

导致此异常:

[EL Severe]: 2018-01-11 21:11:10.755--ServerSession(53937593)--Exception [EclipseLink-0] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.IntegrityException Descriptor

Exceptions:

Exception [EclipseLink-93] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.DescriptorException Exception Description: The table [SCORECARD] is not present in this descriptor. Descriptor: RelationalDescriptor(fi.ipsc_result_server.domain.ResultData.CompetitorResultData --> [DatabaseTable(COMPETITORRESULTDATA)])

Exception [EclipseLink-41] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.DescriptorException Exception Description: A non-read-only mapping must be defined for the sequence number field. Descriptor: RelationalDescriptor(fi.ipsc_result_server.domain.ResultData.CompetitorResultData --> [DatabaseTable(COMPETITORRESULTDATA)])

我发现了许多 Map 的 JPA 注释示例,但没有一个示例能够展示如何使用实体作为键和值来注释 map 。

这两个类都用 @Entity 进行注释,并具有 Id,并且可以毫无问题地持久保存,除非尝试持久保存此 Map,因此问题出在 Map 的注释上。

作为测试,我编写了一个具有 Map 的测试类: 包 fi.ipsc_result_server.domain.ResultData;

@Entity
public class TestClass {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    Long id;

    @OneToMany
    @MapKeyJoinColumn(name="testKey")
    Map<TestKeyClass, TestValueClass> testMap;

[getters and setters]
}
package fi.ipsc_result_server.domain.ResultData;
@Entity
public class TestKeyClass {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    Long id;

[getter and setter for id]
}

package fi.ipsc_result_server.domain.ResultData;

@Entity
public class TestValueClass {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    Long id;

[getter and setter for id]
}

这会导致相同的异常:

Descriptor Exceptions:

Exception [EclipseLink-93] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.DescriptorException Exception Description: The table [TESTVALUECLASS] is not present in this descriptor. Descriptor: RelationalDescriptor(fi.ipsc_result_server.domain.ResultData.TestClass --> [DatabaseTable(TESTCLASS)])

Exception [EclipseLink-41] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.DescriptorException Exception Description: A non-read-only mapping must be defined for the sequence number field. Descriptor: RelationalDescriptor(fi.ipsc_result_server.domain.ResultData.TestClass --> [DatabaseTable(TESTCLASS)])

最佳答案

Map<Entity,Entity>可以与 @OneToMany 一起使用和@ManyToMany 。 您只需添加一个附加注释:

@OneToMany
@MapKeyJoinColumn(name="stage")
private Map<Stage, ScoreCard> scoreCards;

名称是:

The name of the foreign key column for the map key.

更新

您需要确保值实体具有对键实体的引用:

public clas ScoreCard{

  @ManyToOne
  private Stage stage;
}

关于java - 在 JPA 中映射 Map<Entity, Entity> 结果表中不存在于该描述符中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48214554/

相关文章:

java - 内部类泛型 - 不兼容的类型

spring - 自由配置文件控制日志记录

java - Hibernate-Mysql 读取循环异常

java - 如何使用spring JPA查询列表元素

java - 如何让 Session 或 EntityManager 绑定(bind)到线程?

java - Android 反射注解失败

java - Joda Time : First day in this year's ISO week 1

java - 如何在 NetBeans 中添加 JAR

java - 当您有多个实体时,JPA @Version 注释如何工作

java - Hibernate:我们可以在双方都是拥有方的实体之间建立关系吗?