我得到了一个Eclipse错误:@EmbeddedId
。
这是实体:
@Entity
@Table(name = "PERSON")
public class Person implements Serializable {
@EmbeddedId
private PersonPK PersonPK;
@Column(name = "AGE")
private int age;
}
和嵌入类:
@Embeddable
public class PersonPK implements Serializable {
@Column(name = "FIRSTNAME")
private String firstName;
@Column(name = "LASTNAME")
private String lastName;
public PersonPK() {
}
}
Eclipse向我展示了这个错误:PersonPK is not mapped as an embeddable
你能告诉我为什么以及如何解决这个问题?
老问题,我知道,但今天早上已经 - 并解决了 - 这个问题,这是另一种可能性:
在persistence.xml文件中,如果exclude-unlisted-classes
设置为true
,则需要在持久性单元中将@Embeddable
类列为托管类。所以在上面的例子中,你会做这样的事情:
<persistence-unit name="default" transaction-type="JTA">
<!-- ..... -->
<class>com.example.foobar.Person</class>
<class>com.example.foobar.PersonPK</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<!-- ..... -->
</persistence-unit>
有可能PersonPK
在Entity
文件中列为orm.xml
。 orm.xml
文件中的设置会覆盖Java注释。从PersonPK
文件中删除orm.xml
(或将其列为Embeddable
),错误将消失。
在orm.xml
中尝试以下操作
<entity-mappings>
<entity class="package.Person">
<attributes>
...
<embedded-id name="personPK"/>
...
</attributes>
</entity>
<embeddable class="package.PersonPK"/>
</entity-mappings>
您还可以禁用构建的JPA验证。
右键单击项目并选择属性。选择验证并选中启用项目特定设置。取消选中JPA Validator。清理项目。
我有同样的错误。
在我的例子中,我在PersonPK(Embeddable类)上有一个@Entity注释。
不要将@Entity和@Embeddable组合在同一个类上:)
我从PersonPK中移除了@Entity,现在所有人都对这个世界很好。