Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 无法将@IdClass属性用于@ManyToOne关系_Jpa_Jpa 2.0 - Fatal编程技术网

Jpa 无法将@IdClass属性用于@ManyToOne关系

Jpa 无法将@IdClass属性用于@ManyToOne关系,jpa,jpa-2.0,Jpa,Jpa 2.0,我有一个Gfh_i18n实体,带有一个复合键(@IdClass): 正如这篇文章所写,这是可行的。问题是我还有一个Gfh类,它将与Gfh\u i18n有@OneToMany关系: @OneToMany(mappedBy="gfh") @MapKey(name="localeId") private Map<String, Gfh_i18n> descriptions = null; 如果我只是试着去做,在Gfh\u 1i8n @Id @ManyToOne private Gfh g

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

正如这篇文章所写,这是可行的。问题是我还有一个
Gfh
类,它将与
Gfh\u i18n
@OneToMany
关系:

@OneToMany(mappedBy="gfh")
@MapKey(name="localeId")
private Map<String, Gfh_i18n> descriptions = null;
如果我只是试着去做,在
Gfh\u 1i8n

@Id @ManyToOne
private Gfh gfh = null;
它解决了前面的错误,但在
Gfh_i18n
中给出了一个错误,指出

The attribute matching the ID class attribute gfh does not have the correct type es.caib.gesma.petcom.data.entity.Gfh
与我的类似,但我不完全理解为什么我应该使用
@EmbeddedId
(或者如果有某种方法可以将
@IdClass
@ManyToOne
一起使用)

我在Hibernate上使用JPA2.0(JBoss6.1)

有什么想法吗?提前感谢。

您正在处理一个“派生身份”(在JPA 2.0规范第2.4.1节中描述)

您需要更改ID类,以便与“子”实体中的“父”实体字段相对应的字段(在您的案例中为
gfh
)具有与“父”实体的单个
@ID
字段相对应的类型(例如
String
),或者,如果“父”实体使用
IdClass
,则
IdClass
(例如,
Gfh\U id

Gfh\u 1i8n
中,您应该这样声明
Gfh

@Id @ManyToOne
private Gfh gfh = null;
public class Gfh_i18n_id implements Serializable {
  private String localeId = null;
  private String gfh = null;
  ...
}
假设
GFH
有一个类型为
String
@Id
字段,则Id类应如下所示:

@Id @ManyToOne
private Gfh gfh = null;
public class Gfh_i18n_id implements Serializable {
  private String localeId = null;
  private String gfh = null;
  ...
}

谢谢你的回答,但我已经尝试过了。在创建表时,我遇到了一个异常,因为DDL是
create table Gfh_i18n(localeId varchar(10)not null,description varchar(50)null,Gfh_id varchar(10)null,primary key(Gfh_id,localeId))
。由于它将
gfh\u id
标记为
null
,因此无法创建主键。尝试标记
optional=false
,结果相同,并且我无法将
@Column
@ManyToOne
一起使用。到目前为止,唯一的解决方法是使用
EmbeddedId
而不是
IdClass
,但我发现它是strange我不能直接使用
IdClass
。好的,我确实阅读了您的文档,发现它太支持您的声明了,所以我一直在做测试,JBoss6.1 Hibernate(3.6.6)似乎有一个bug。在独立程序中使用3.6.10也会带来同样的问题;在同一程序中使用4.1.8效果很好。谢谢,使用EclipseLink对我有效。在我的例子中,单个@Id类型是int。
public class Gfh_i18n_id implements Serializable {
  private String localeId = null;
  private String gfh = null;
  ...
}