我正在尝试使用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文件夹中,如其他文章所述。但是,它没有帮助。
答案 0 :(得分:0)
(免责声明:需要在帖子下方写评论,但在这种情况下我不能这样做。我只想伸出援手,因为我自己一次也为此JPA感到苦恼。)
此Link显示了如何向用户org.eclipse.persistence.jpa.PersistenceProvider
提供emf(EntityManageFactory)。根据我的理解,RESOURCE_LOCAL
是否需要与@PersistenceUnit
一起使用事务类型。 description对此进行了澄清。
否则,请检查this。将提供者更改为org.apache.openjpa.persistence.PersistenceProviderImpl
。 persistence.xml的路径正确。
答案 1 :(得分:0)
您使用了资源本地单元,因此您可能想使用@PersistenceUnit注入EntityManagerFactory并自己创建实体管理器。如果没有,也许转移到JTA部门。