@EmbeddedId:未映射为可嵌入

问题描述 投票:4回答:5

我得到了一个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

你能告诉我为什么以及如何解决这个问题?

eclipse jpa
5个回答
8
投票

老问题,我知道,但今天早上已经 - 并解决了 - 这个问题,这是另一种可能性:

在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>

0
投票

有可能PersonPKEntity文件中列为orm.xmlorm.xml文件中的设置会覆盖Java注释。从PersonPK文件中删除orm.xml(或将其列为Embeddable),错误将消失。


0
投票

orm.xml中尝试以下操作

<entity-mappings> 
<entity class="package.Person"> 
<attributes> 
... 
<embedded-id name="personPK"/> 
... 
</attributes> 
</entity> 
<embeddable class="package.PersonPK"/> 
</entity-mappings>

0
投票

您还可以禁用构建的JPA验证。

右键单击项目并选择属性。选择验证并选中启用项目特定设置。取消选中JPA Validator。清理项目。

资料来源:Problem with JPA Project in Eclipse - error in class annotated @Entity: Table "xxx" cannot be resolved


0
投票

我有同样的错误。

在我的例子中,我在PersonPK(Embeddable类)上有一个@Entity注释。

不要将@Entity和@Embeddable组合在同一个类上:)

我从PersonPK中移除了@Entity,现在所有人都对这个世界很好。

© www.soinside.com 2019 - 2024. All rights reserved.