【问题标题】:Eclipselink unable to spot the persistence unitEclipselink 无法发现持久性单元
【发布时间】:2018-08-26 03:27:31
【问题描述】:

我正在尝试使用 EclipseLink 和 JPA 2.1.0 来尝试连接到数据库。但是,应用程序无法发现/识别文件夹中的 persistence.xml。请问你能帮忙吗?

pom.xml

    <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.0</version>
    </dependency>
    <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.1.0</version>
    </dependency>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="Employee" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.sap.cloud.sdk.model.model.Employee</class>
        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
        </properties>
    </persistence-unit>
</persistence>

以上persistence.xml放在文件夹结构中:

firstapp
 - application
 - src
    - main
       - java
         - package
       - resources
         - META-INF
            - persistence.xml
       - Webapp
         - WEB-INF
            - web.xml

模型如下图:

@Entity
@Table(name = "EMPLOYEE")
@Multitenant(value=MultitenantType.TABLE_PER_TENANT)
@TenantTableDiscriminator(type=TenantTableDiscriminatorType.SCHEMA, contextProperty="eclipselink-tenant.id")
@NamedQueries({ @NamedQuery(name = "Employees", query = "SELECT c FROM Employee c")})
public class Employee implements Serializable {
    /**
     * The (globally unique) ID of the object.
     */
    @Id
    @Column(name="GUID", length = 36)
    private String guid = UUID.randomUUID().toString();

    private static final long serialVersionUID = 1L;


    @Column(name = "NAME", length = 36, nullable = true)
    String name = null;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGuid()
    {
        return this.guid;
    }

    public void setGuid(String guid)
    {
        this.guid = guid;
    }
}

我正在尝试使用上述命名查询:

@PersistenceContext(unitName = "Employee")
    private EntityManager em;

    @Override
    protected void doGet( final HttpServletRequest request, final HttpServletResponse response )
        throws IOException
    {
        logger.info("I am running!");
//        response.getWriter().write("Hello World.. This is a sample application!");
        String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
        String title = "Sample Code for multitenancy Test";

        String tenantId = getTenantId();
        Map<String, String> props = new HashMap<String, String>();
        props.put("elipselink.tenant.id", tenantId);
        em = this.getEntityManagerFactory().createEntityManager(props);
        Query query = em.createNamedQuery("Employees");
        List<Employee> retVal = query.getResultList();

在集成测试中,我收到错误消息,即它无法发现实体管理器注释中提到的持久性单元。

SEVERE: FAIL ... 86e424ac-8560-4b85-9df3-4c7253559d80:  Missing required persistence.xml for @PersistenceContext ref "em" to unit "Employee"
Aug 26, 2018 8:33:40 AM org.apache.openejb.config.ReportValidationResults logResults
SEVERE: Invalid EjbModule(name=86e424ac-8560-4b85-9df3-4c7253559d80, path=/Users/i048564/Documents/Eclipse/neon/firstapp/integration-tests/target/arquillian-working-dir/1/a
rquillian-tomee-app-working-dir/0/86e424ac-8560-4b85-9df3-4c7253559d80)

我也尝试将 persistence.xml 放在 webapp 文件夹中,如其他帖子中所述。但是,它没有帮助。

【问题讨论】:

  • 您展示了一个看起来来自 IDE 项目的目录结构,而不是最终的部署存档。重要的是 META-INF 目录与已​​编译的包类文件放置在同一目录中以进行部署或运行​​以进行测试。您能否详细说明存档的构建方式和部署前的外观,或者您用于构建/部署/测试的内容?还要检查您的 pom 是否受到尊重,并且 EclipseLink 类已实际下载并位于您环境的类路径中。

标签: java database jpa eclipselink


【解决方案1】:

(免责声明:需要在下面写下帖子作为评论,但在这种情况下我不能这样做。我只是想伸出援助之手,因为我自己曾经在这个 JPA 上挣扎过。)

这个Link 展示了如何使用 emf (EntityManageFactory) 为用户 org.eclipse.persistence.jpa.PersistenceProvider 提供。据我了解,RESOURCE_LOCAL 事务类型是否需要与@PersistenceUnit 一起使用。这个description 澄清一下。

否则请与this联系。将提供者更改为 org.apache.openjpa.persistence.PersistenceProviderImpl。 persistence.xml 的路径是正确的。

【讨论】:

    【解决方案2】:

    您使用了资源本地单元,因此您可能希望使用 @PersistenceUnit 注入 EntityManagerFactory 并自己创建实体管理器。如果没有,可能会转移到 JTA 单位。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 2011-05-25
      • 2013-07-10
      相关资源
      最近更新 更多