【问题标题】:@EmbeddedId : is not mapped as an embeddable@EmbeddedId :未映射为可嵌入对象
【发布时间】:2014-11-27 13:41:58
【问题描述】:

我收到 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


    【解决方案1】:

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

    【讨论】:

    • 我的 orm.xml 中没有列出 PersonPK。如何将其列为可嵌入的?
    • 如果您还没有orm.xml 文件,您可能不需要创建一个。但是,如果您没有orm.xml 文件,我不知道是什么导致了该特定错误。您使用的是什么版本的 Eclipse?它是否添加了任何第三方扩展?
    【解决方案2】:

    在您的 orm.xml 中尝试以下操作

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

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • 正是我的问题....谢谢!我想知道为什么我的应用程序适用于 glassfish...
        【解决方案5】:

        我有同样的错误。

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

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

        我从 PersonPK 中删除了@Entity,现在一切都很好。

        【讨论】:

          猜你喜欢
          • 2013-12-08
          • 2013-08-01
          • 1970-01-01
          • 2020-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-06-28
          • 1970-01-01
          相关资源
          最近更新 更多