JAVA JPA:尝试为项目配置JPA的异常

时间:2015-11-12 21:20:18

标签: java jpa

我正在尝试将JPA合并到我的项目中。这样做时,我会收到下面发布的堆栈跟踪和异常。似乎这个问题的常见原因是persistence.xml中没有定义持久性单元,但我已经这样做了(见下文)。

persistence.xml(url / user / pass为了安全而擦除):

<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 persistence_1_0.xsd"
version="1.0">
<persistence-unit name="samples" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
        <property name="javax.persistence.jdbc.url" value="" />
        <property name="javax.persistence.jdbc.user" value="" />
        <property name="javax.persistence.jdbc.password" value="" />
        <property name="hibernate.default_schema" value="gc_image_relational"/>
    </properties>
</persistence-unit>

RelationalTableWrapper.java:

@Entity
@Table(name = "runs")
public class RelationalTableWrapper implements ITableWrapper {

@Id
@Column(name = "id")
private int id = 0;

@Column(name = "name")
private String name = "";

@Column(name = "attributes")
private String attributes = new String();

// This is the constructor of the class RelationalTableWrapper
public RelationalTableWrapper(Integer id, String name, String attributes) {
  this.id = id;
  this.name = name;
  this.attributes = attributes;
}

public RelationalTableWrapper() {}

// Returns the id related to the table
public int getId() {
  return id;
}

// Returns the name of the Run or Sample
public String getName() {
  return name;
}

// Setter to change the name of the Run or Sample
 public void setName(String name) {
  this.name = name;
}

// Getter to retrieve the HashMap that relates to the dynamic columns of
// the Run or Sample
public String getAttributes() {
  return attributes;
}

// Setter to change values in the HashMap relating to the dynamic columns
// of the Run or Sample
public void setAttributes(String attributes) {
  this.attributes = attributes;
}

}

最后是客户端代码(在EntityManagerFactory factory = Persistence.createEntityManagerFactory("samples"); JpaIntegrationTest.java发生异常:

package com.gcimage.relational;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.metamodel.EntityType;

import org.junit.Test;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

public class JpaIntegrationRelationalTest {

  @Test
  // Retrieves data from data_file with reference to the service_request_id
  // field.
  public void testRelationalJpaIntegration() throws SQLException,  ClassNotFoundException,
  JsonParseException, JsonMappingException, IOException {
    EntityManagerFactory factory =
    Persistence.createEntityManagerFactory("samples");
    EntityManager manager = factory.createEntityManager();

    Set<EntityType<?>> results = manager.getMetamodel().getEntities();

    for (EntityType<?> type : results) {
       System.out.println(type.getAttributes());
    }
  }
}

堆栈跟踪:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named samples
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at com.gcimage.relational.JpaIntegrationRelationalTest.testRelationalJpaIntegration(JpaIntegrationRelationalTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

2 个答案:

答案 0 :(得分:0)

我看不到为以下属性提供的值。

  <property name="javax.persistence.jdbc.url" value="" />
  <property name="javax.persistence.jdbc.user" value="" />
  <property name="javax.persistence.jdbc.password" value="" />

还可以请更新堆栈跟踪以进行进一步分析吗?

答案 1 :(得分:0)

右键单击Eclipse的resources目录,然后选择&#34; Build Path&#34; &GT; &#34;用作源文件夹&#34;。 我认为问题是META-INF / persistence.xml不在类路径中。