无法将@IdClass属性用于@ManyToOne关系 [英] Cannot use an @IdClass attribute for a @ManyToOne relationship
问题描述
我有一个Gfh_i18n
实体,带有复合键(@IdClass
):
I have a Gfh_i18n
entity, with a composite key (@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;
....
}
和id类
public class Gfh_i18n_id implements Serializable {
private String localeId = null;
private Gfh gfh = null;
...
}
在撰写本文时,此方法有效.问题是我还有一个Gfh
类,该类与Gfh_i18n
的关系为@OneToMany
:
As this is written, this works. The issue is that I also have a Gfh
class which will have a @OneToMany
relationship to Gfh_i18n
:
@OneToMany(mappedBy="gfh")
@MapKey(name="localeId")
private Map<String, Gfh_i18n> descriptions = null;
使用Eclipse Dali,这会给我以下错误:
Using Eclipse Dali, this gives me the following error:
In attribute 'descriptions', the "mapped by" attribute 'gfh' has an invalid mapping type for this relationship.
如果我只是想尝试,请在Gfh_1i8n
If I just try to do, in Gfh_1i8n
@Id @ManyToOne
private Gfh gfh = null;
它解决了先前的错误,但在Gfh_i18n
中给出了一个,说明了这一点
it solves the previous error but gives one in Gfh_i18n
, stating that
The attribute matching the ID class attribute gfh does not have the correct type es.caib.gesma.petcom.data.entity.Gfh
这个问题与我的相似,但我不完全理解为什么我应该使用@EmbeddedId
(或者是否可以将@IdClass
与@ManyToOne
一起使用).
This question is similar to mine, but I do not fully understand why I should be using @EmbeddedId
(or if there is some way to use @IdClass
with @ManyToOne
).
我在Hibernate(JBoss 6.1)上使用JPA 2.0
I am using JPA 2.0 over Hibernate (JBoss 6.1)
有什么想法吗?预先感谢.
Any ideas? Thanks in advance.
推荐答案
您正在处理派生身份"(在JPA 2.0规范的2.4.1节中进行了描述).
You are dealing with a "derived identity" (described in the JPA 2.0 spec, section 2.4.1).
您需要更改ID类,以便与子"实体中的父"实体字段相对应的字段(在您的情况下为gfh
)具有与父"实体的单个String
),或者,如果父"实体使用IdClass
,则IdClass
(例如Gfh_id
).
You need to change your ID class so the field corresponding to the "parent" entity field in the "child" entity (in your case gfh
) has a type that corresponds to either the "parent" entity's single @Id
field (e.g. String
) or, if the "parent" entity uses an IdClass
, the IdClass
(e.g. Gfh_id
).
在Gfh_1i8n
中,您应该这样声明gfh
:
In Gfh_1i8n
, you should declare gfh
like this:
@Id @ManyToOne
private Gfh gfh = null;
假设GFH
具有单个@Id
类型为String
的字段,则您的ID类应如下所示:
Assuming GFH
has a single @Id
field of type String
, your ID class should look like this:
public class Gfh_i18n_id implements Serializable {
private String localeId = null;
private String gfh = null;
...
}
这篇关于无法将@IdClass属性用于@ManyToOne关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!