当我运行我的项目时,我收到以下错误:
Compiling 1 source file to C:\Users\Ebola\Desktop\JPASE\JPA\build\classes
warning: [options] bootstrap class path not set in conjunction with -source 1.5
1 warning
compile:
run:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named pu1
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at client.Client.main(Client.java:45)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
我已经检查了持久性单元的名称,这是正确的。
这是客户端类:
public class Client {
private static EntityManagerFactory emf;
private static EntityManager em;
public static void main(String[] args) {
// Create EntityManagerFactory for persistent unit named "pu1"
// to be used in this test
emf = Persistence.createEntityManagerFactory("pu1");
// Persist all entities
createTransactionalEntityManager();
System.out.println("Inserting Customer and Orders... " + testInsert());
closeTransactionalEntityManager();
// Test query and navigation
createEntityManager();
System.out.println("Verifying that all are inserted... " + verifyInsert());
closeEntityManager();
// Get a detached instance in a new EntityManager
createEntityManager();
Customer c = findCustomer("Joe Smith");
closeEntityManager();
// Remove all entities
createTransactionalEntityManager();
System.out.println("Removing all... " + testDelete(c));
closeTransactionalEntityManager();
// Query the results
createEntityManager();
System.out.println("Verifying that all are removed... " + verifyDelete());
closeEntityManager();
}
private static String testInsert() {
// Create new customer
Customer customer0 = new Customer();
customer0.setId(1);
customer0.setName("Joe Smith");
// Persist the customer
em.persist(customer0);
// Create 2 orders
Order order1 = new Order();
order1.setId(100);
order1.setAddress("123 Main St. Anytown, USA");
Order order2 = new Order();
order2.setId(200);
order2.setAddress("567 1st St. Random City, USA");
// Associate orders with the customer. The association
// must be set on both sides of the relationship: on the
// customer side for the orders to be persisted when
// transaction commits, and on the order side because it
// is the owning side.
customer0.getOrders().add(order1);
order1.setCustomer(customer0);
customer0.getOrders().add(order2);
order2.setCustomer(customer0);
return "OK";
}
private static String verifyInsert() {
Customer c = findCustomer("Joe Smith");
Collection<Order> orders = c.getOrders();
if (orders == null || orders.size() != 2) {
throw new RuntimeException("Unexpected number of orders: "
+ ((orders == null)? "null" : "" + orders.size()));
}
return "OK";
}
private static String testDelete(Customer c) {
// Merge the customer to the new persistence context
Customer c0 = em.merge(c);
// Delete all records.
em.remove(c0);
return "OK";
}
private static String verifyDelete() {
Query q = em.createQuery("select c from Customer c");
List results = q.getResultList();
if (results == null || results.size() != 0) {
throw new RuntimeException("Unexpected number of customers after delete");
}
q = em.createQuery("select o from Order o");
results = q.getResultList();
if (results == null || results.size() != 0) {
throw new RuntimeException("Unexpected number of orders after delete");
}
return "OK";
}
private static Customer findCustomer(String name) {
Query q = em.createQuery("select c from Customer c where c.name = :name");
q.setParameter("name", name);
return (Customer)q.getSingleResult();
}
private static void createTransactionalEntityManager() {
// Create a new EntityManager
em = emf.createEntityManager();
// Begin transaction
em.getTransaction().begin();
}
private static void closeTransactionalEntityManager() {
// Commit the transaction
em.getTransaction().commit();
// Close this EntityManager
em.close();
}
private static void createEntityManager() {
// Create a new EntityManager
em = emf.createEntityManager();
}
private static void closeEntityManager() {
// Close this EntityManager
em.close();
}
}
现在这是我的持久性课程:
package javax.persistence;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import javax.persistence.spi.PersistenceProvider;
public class Persistence {
public static final String PERSISTENCE_PROVIDER = "javax.persistence.spi.PeristenceProvider";
protected static final Set<PersistenceProvider> providers;
private static final String SERVICE_NAME;
private static final String PERSISTENCE_XML_NAME = "META-INF/persistence.xml";
private static final Pattern nonCommentPattern;
public Persistence() {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aload_0
* 1: invokespecial java/lang/Object."<init>":()V
* 4: return
* */
// </editor-fold>
}
public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName) {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aload_0
* 1: aconst_null
* 2: invokestatic javax/persistence/Persistence.createEntityManagerFactory:(Ljava/lang/String;Ljava/util/Map;)Ljavax/persistence/EntityManagerFactory;
* 5: areturn
* */
// </editor-fold>
}
public static EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
// <editor-fold defaultstate="collapsed" desc="Compiled Code">
/* 0: aconst_null
* 1: astore_2
* 2: aconst_null
* 3: astore_3
* 4: invokestatic javax/persistence/Persistence.findAllProviders:()Ljava/util/Set;
* 7: astore_3
* 8: goto 13
* 11: astore 4
* 13: new java/util/HashMap
* 16: dup
* 17: invokespecial java/util/HashMap."<init>":()V
* 20: astore 4
* 22: new java/util/HashSet
* 25: dup
* 26: invokespecial java/util/HashSet."<init>":()V
* 29: astore 5
* 31: aload_3
* 32: invokeinterface java/util/Set.iterator:()Ljava/util/Iterator;
* 37: astore 6
* 39: aload 6
* 41: invokeinterface java/util/Iterator.hasNext:()Z
* 46: ifeq 123
* 49: aload 6
* 51: invokeinterface java/util/Iterator.next:()Ljava/lang/Object;
* 56: checkcast javax/persistence/spi/PersistenceProvider
* 59: astore 7
* 61: aload 7
* 63: aload_0
* 64: aload_1
* 65: invokeinterface javax/persistence/spi/PersistenceProvider.createEntityManagerFactory:(Ljava/lang/String;Ljava/util/Map;)Ljavax/persistence/EntityManagerFactory;
* 70: astore_2
* 71: aload_2
* 72: ifnull 78
* 75: goto 123
* 78: aload 5
* 80: aload 7
* 82: invokevirtual java/lang/Object.getClass:()Ljava/lang/Class;
* 85: invokevirtual java/lang/Class.getName:()Ljava/lang/String;
* 88: invokeinterface java/util/Set.add:(Ljava/lang/Object;)Z
* 93: pop
* 94: goto 120
* 97: astore 8
* 99: aload 4
* 101: aload 7
* 103: invokevirtual java/lang/Object.getClass:()Ljava/lang/Class;
* 106: invokevirtual java/lang/Class.getName:()Ljava/lang/String;
* 109: aload 8
* 111: invokestatic javax/persistence/Persistence.createErrorMessage:(Ljava/lang/Throwable;)Ljava/lang/String;
* 114: invokeinterface java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
* 119: pop
* 120: goto 39
* 123: aload_2
* 124: ifnonnull 481
* 127: new java/lang/StringBuffer
* 130: dup
* 131: new java/lang/StringBuilder
* 134: dup
* 135: invokespecial java/lang/StringBuilder."<init>":()V
* 138: ldc No Persistence provider for EntityManager named
* 140: invokevirtual java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
* 143: aload_0
* 144: invokevirtual java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
* 147: ldc :
* 149: invokevirtual java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
* 152: invokevirtual java/lang/StringBuilder.toString:()Ljava/lang/String;
* 155: invokespecial java/lang/StringBuffer."<init>":(Ljava/lang/String;)V
* 158: astore 6
* 160: ldc META-INF/persistence.xml
* 162: invokestatic javax/persistence/Persistence.exists:(Ljava/lang/String;)Z
* 165: ifne 179
* 168: aload 6
* 170: ldc No META-INF/persistence.xml was found in classpath.\n
* 172: invokevirtual java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
* 175: pop
* 176: goto 468
* 179: invokestatic javax/persistence/Persistence.getReasons:()Ljava/util/Map;
* 182: astore 7
* 184: aload 7
* 186: invokeinterface java/util/Map.entrySet:()Ljava/util/Set;
* 191: invokeinterface java/util/Set.iterator:()Ljava/util/Iterator;
* 196: astore 8
* 198: aload 8
* 200: invokeinterface java/util/Iterator.hasNext:()Z
* 205: ifeq 283
* 208: aload 8
* 210: invokeinterface java/util/Iterator.next:()Ljava/lang/Object;
* 215: checkcast java/util/Map$Entry
* 218: astore 9
* 220: aload 6
* 222: ldc Provider named
* 224: invokevirtual java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
* 227: pop
* 228: aload 6
* 230: aload 9
* 232: invokeinterface java/util/Map$Entry.getKey:()Ljava/lang/Object;
* 237: invokevirtual java/lang/StringBuffer.append:(Ljava/lang/Object;)Ljava/lang/StringBuffer;
* 240: pop
* 241: aload 6
* 243: ldc threw exception at initialization:
* 245: invokevirtual java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
* 248: pop
* 249: aload 6
* 251: new java/lang/StringBuilder
* 254: dup
* 255: invokespecial java/lang/StringBuilder."<init>":()V
* 258: aload 9
* 260: invokeinterface java/util/Map$Entry.getValue:()Ljava/lang/Object;
* 265: invokevirtual java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
* 268: ldc \n
* 270: invokevirtual java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
* 273: invokevirtual java/lang/StringBuilder.toString:()Ljava/lang/String;
* 276: invokevirtual java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
* 279: pop
* 280: goto 198
* 283: aload 4
* 285: invokeinterface java/util/Map.entrySet:()Ljava/util/Set;
* 290: invokeinterface java/util/Set.iterator:()Ljava/util/Iterator;
* 295: astore 8
* 297: aload 8
* 299: invokeinterface java/util/Iterator.hasNext:()Z
* 304: ifeq 382
* 307: aload 8
* 309: invokeinterface java/util/Iterator.next:()Ljava/lang/Object;
* 314: checkcast java/util/Map$Entry
* 317: astore 9
* 319: aload 6
* 321: ldc Provider named
* 323: invokevirtual java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
* 326: pop
* 327: aload 6
* 329: aload 9
* 331: invokeinterface java/util/Map$Entry.getKey:()Ljava/lang/Object;
* 336: invokevirtual java/lang/StringBuffer.append:(Ljava/lang/Object;)Ljava/lang/StringBuffer;
* 339: pop
* 340: aload 6
* 342: ldc threw unexpected exception at create EntityManagerFactory: \n
* 344: invokevirtual java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
* 347: pop
* 348: aload 6
* 350: new java/lang/StringBuilder
* 353: dup
* 354: invokespecial java/lang/StringBuilder."<init>":()V
* 357: aload 9
* 359: invokeinterface java/util/Map$Entry.getValue:()Ljava/lang/Object;
* 364: invokevirtual java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
* 367: ldc \n
* 369: invokevirtual java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
* 372: invokevirtual java/lang/StringBuilder.toString:()Ljava/lang/String;
* 375: invokevirtual java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
* 378: pop
* 379: goto 297
* 382: aload 5
* 384: invokeinterface java/util/Set.isEmpty:()Z
* 389: ifne 468
* 392: aload 6
* 394: ldc The following providers:\n
* 396: invokevirtual java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
* 399: pop
* 400: aload 5
* 402: invokeinterface java/util/Set.iterator:()Ljava/util/Iterator;
* 407: astore 8
* 409: aload 8
* 411: invokeinterface java/util/Iterator.hasNext:()Z
* 416: ifeq 460
* 419: aload 8
* 421: invokeinterface java/util/Iterator.next:()Ljava/lang/Object;
* 426: checkcast java/lang/String
* 429: astore 9
* 431: aload 6
* 433: new java/lang/StringBuilder
* 436: dup
* 437: invokespecial java/lang/StringBuilder."<init>":()V
* 440: aload 9
* 442: invokevirtual java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
* 445: ldc \n
* 447: invokevirtual java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
* 450: invokevirtual java/lang/StringBuilder.toString:()Ljava/lang/String;
* 453: invokevirtual java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
* 456: pop
* 457: goto 409
* 460: aload 6
* 462: ldc Returned null to createEntityManagerFactory.\n
* 464: invokevirtual java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer;
* 467: pop
* 468: new javax/persistence/PersistenceException
* 471: dup
* 472: aload 6
* 474: invokevirtual java/lang/StringBuffer.toString:()Ljava/lang/String;
* 477: invokespecial javax/persistence/PersistenceException."<init>":(Ljava/lang/String;)V
* 480: athrow
* 481: aload_2
* 482: areturn
* Exception table:
* from to target type
* 4 8 11 Class java/io/IOException
* 61 75 97 Class java/lang/Throwable
* 78 94 97 Class java/lang/Throwable
* */
// </editor-fold>
}
**我该怎么做才能解决这个问题? **