eclipse 类“model.Address”列在persistence.xml 文件中但未映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2242637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 19:11:24  来源:igfitidea点击:

Class "model.Address" is listed in the persistence.xml file but not mapped

eclipsejpajakarta-ee

提问by user246160

I have created a JPAproject. In that Eclipse displays the following error on the entity class.

我已经创建了一个JPA项目。在该 Eclipse 中,实体类显示以下错误。

Class "model.Address" is listed in the persistence.xmlfile but not mapped

类“model.Address”列在persistence.xml文件中但未映射

How am I supposed to map the entity class in persistance.xml?

我应该如何映射实体类persistance.xml

Here is the model.Addressentity:

这是model.Address实体:

package model;

import java.io.Serializable;

import javax.persistence.*;

@Entity
public class Address implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long id;
    private String city;
    private String country;
    private String province;
    private String postalCode;
    private String street;

    // Getters/setters omitted for brevity.
}

Here is the persistence.xml:

这是persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persistence 
    xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0" 
>
    <provider>org.eclipse.persistance.example.jpa.20.employee.annotations</provider>

    <persistence-unit name="employee" transaction-type="RESOURCE_LOCAL">
        <class>model.Employee</class>
        <class>model.Address</class>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
            <property name="javax.persistence.jdbc.user" value="scott" />
            <property name="javax.persistence.jdbc.password" value="tiger" />
        </properties>
    </persistence-unit>
</persistence>

回答by BalusC

This is an Eclipse quirk. I recently had exactly this problem when I created a new JPA project with the JPA library configuration disabled, but didn't manually configure the JPA libraries beforeI created the entities by the Eclipse New JPA Entitywizard. After creating the entities I configured the JPA libraries in project's Build Path(just by adding target Java EE server runtime in Libraries), but the validation error still remains. I could solve it in at least one of following ways:

这是一个 Eclipse 怪癖。我最近在创建一个禁用 JPA 库配置的新 JPA 项目时遇到了这个问题,但我通过 Eclipse新建 JPA 实体向导创建实体之前没有手动配置 JPA 库。创建实体后,我在项目的Build Path 中配置了 JPA 库(只需在Libraries 中添加目标 Java EE 服务器运行时),但验证错误仍然存​​在。我可以通过以下至少一种方式解决它:

  1. Rightclick persistence.xmlfile, JPA Tools-> Synchronize Class List.
  2. Or, rightclick project, Validate.
  3. Or, close/reopen project.
  1. 右键单击persistence.xml文件,JPA 工具->同步类列表
  2. 或者,右键单击项目,Validate
  3. 或者,关闭/重新打开项目。

This is consistently reproducible. I was using Eclipse Indigo SR1. When I create the entities afterconfiguring the JPA libraries, this validation error doesn't occur.

这是始终可重现的。我使用的是 Eclipse Indigo SR1。配置 JPA 库创建实体时,不会发生此验证错误。

回答by H. ELKINA

Right click on jpa project

右键单击 jpa 项目

then

然后

enter image description here

在此处输入图片说明

回答by Bozho

I think you have the wrong JPA provider class. It has to be:

我认为您有错误的 JPA 提供程序类。它一定要是:

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

(the one you've set doesn't seem to be a class at all, let alone a provider class)

(你设置的似乎根本不是一个类,更不用说提供者类了)

回答by olafure

Make sure your class is annotated with @Entity(from javax.persistence.Entity)

确保您的类使用@Entity(来自 javax.persistence.Entity)进行注释

I realise that's not the OP's problem, but I got caught by that and Google sent me here.

我意识到这不是 OP 的问题,但我被抓住了,谷歌把我送到了这里。

回答by Joe Tony Tagore

Any of the above didnt work.

以上任何一项都不起作用。

For anyone who is searching for the "Class xxxx is mapped, but is not included in any persistence unit" error in Eclipse or RAD:

对于在 Eclipse 或 RAD 中搜索“类 xxxx 已映射,但未包含在任何持久性单元中”错误的任何人:

  1. Right-click on the project and choose properties.
  2. Select JPA
  3. Select the radio button "Discover annotated classes automatically"
  4. OK and wait for the project to finish building.
  1. 右键单击项目并选择属性。
  2. 选择JPA
  3. 选择单选按钮“自动发现带注释的类”
  4. 确定并等待项目完成构建。

These steps worked for me.

这些步骤对我有用。

回答by Pratik Guha Ray

You need to create a persistence file and a ORM mapping file for JPA, refer the mapping file from persistence file.

您需要为 JPA 创建一个持久化文件和一个 ORM 映射文件,从持久化文件中引用映射文件。

<persistence-unit name="persistenceUnit"
    transaction-type="RESOURCE_LOCAL">
    <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
    <mapping-file>META-INF/hbase-orm.xml</mapping-file>
    <class>com.xxx.logcollector.entity.DefaultLogableEntity</class>
    <properties>
        <property name="datanucleus.jpa.addClassTransformer" value="false" />
        <property name="datanucleus.managedRuntime" value="false" />

....

....

Create ORM mapping file

创建 ORM 映射文件

<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
    version="1.0">
    <entity class="com.xxx.cne.logcollector.entity.DefaultLogableEntity"
        name="DefaultLogableEntity">
        <table name="RAW_LOG_COLLECTION" />
        <attributes>
            <id name="clientHostIP">
                <column name="ANALYTICS:CLIENT_IP" />
            </id>
            <basic name="requestDateTime">
                <column name="ANALYTICS:REQUEST_DATETIME" />
            </basic>

...

...

Create entity manager in spring

在 spring 中创建实体管理器

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="persistenceUnit"></property>

回答by Rahul Juluru

If eclipse is used , go to Window>Preference>Validationand uncheck Suspend all validatorsoption .Clean the project and problem get solved.

如果使用 Eclipse,请转到Window>Preference>Validation并取消选中Suspend all validators选项。清理项目并解决问题。

回答by Sumatrin

It should be written like:

应该这样写:

@Entity
**@Table(name="Address",schema="ABCD")** `

...or it could be written like :

...或者它可以写成:

@Entity
public class Address {

} 

回答by Venkat Keesara

i got similar error and when eclipse artifacts folder ".settings" is missed . After i generate eclipse artifacts using "maven-eclipse-plugin" , the error is gone

我遇到了类似的错误,并且当 eclipse artifacts 文件夹“.settings”丢失时。在我使用“maven-eclipse-plugin”生成eclipse工件后,错误消失了