【发布时间】: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