不能将@IdClass属性用于@ManyToOne关系

SJu*_*n76 2 jpa jpa-2.0

我有一个Gfh_i18n实体,带有一个复合键(@IdClass):

@Entity @IdClass(es.caib.gesma.petcom.data.entity.id.Gfh_i18n_id.class)
public class Gfh_i18n implements Serializable {

  @Id @Column(length=10, nullable = false)
  private String localeId = null;

  @Id <-- This is the attribute causing issues
  private Gfh gfh = null;
  ....
}
Run Code Online (Sandbox Code Playgroud)

和id类

public class Gfh_i18n_id implements Serializable {
  private String localeId = null;
  private Gfh gfh = null;
  ...
}
Run Code Online (Sandbox Code Playgroud)

正如这写的,这是有效的.问题是我还有一个Gfh与之@OneToMany关系的课程Gfh_i18n:

@OneToMany(mappedBy="gfh")
@MapKey(name="localeId")
private Map<String, Gfh_i18n> descriptions = null;
Run Code Online (Sandbox Code Playgroud)

使用Eclipse Dali,这给了我以下错误:

 In attribute 'descriptions', the "mapped by" attribute 'gfh' has an invalid mapping type for this relationship.
Run Code Online (Sandbox Code Playgroud)

如果我只是尝试做,在 Gfh_1i8n

@Id @ManyToOne
private Gfh gfh = null;
Run Code Online (Sandbox Code Playgroud)

它解决了之前的错误,但给出了一个Gfh_i18n,说明了

The attribute matching the ID class attribute gfh does not have the correct type es.caib.gesma.petcom.data.entity.Gfh
Run Code Online (Sandbox Code Playgroud)

这个问题类似于我的,但我不完全理解为什么我应该使用@EmbeddedId(或有某种方式使用@IdClass@ManyToOne).

我使用JPA 2.0而不是Hibernate(JBoss 6.1)

有任何想法吗?提前致谢.

Bri*_*rgh 7

您正在处理"派生身份"(在JPA 2.0规范第2.4.1节中描述).

您需要更改您的ID类,以便与"子"实体(在您的情况下gfh)中的"父"实体字段对应的字段具有与"父"实体的单个@Id字段(例如String)对应的类型,或者,如果"父"实体使用a IdClass,IdClass(例如Gfh_id).

Gfh_1i8n,您应该声明gfh如下:

@Id @ManyToOne
private Gfh gfh = null;
Run Code Online (Sandbox Code Playgroud)

假设GFH有一个@Id类型的字段String,您的ID类应如下所示:

public class Gfh_i18n_id implements Serializable {
  private String localeId = null;
  private String gfh = null;
  ...
}
Run Code Online (Sandbox Code Playgroud)