2014-08-29 128 views
0

我是java新手。我試圖連接我的應用程序在我的機器上的SQL Server數據庫,但得到以下錯誤:無法使用persistence.xml持久保存到sql server數據庫

javax.persistence.PersistenceException:No Persistence provider for EntityManager named Sub:  The following providers: 
oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider 
oracle.toplink.essentials.PersistenceProvider 
Returned null to createEntityManagerFactory. 

我的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="Application"> 
    <provider> 
     oracle.toplink.essentials.PersistenceProvider 
    </provider> 

    <class>com.application.entity.ProductEntity</class> 

    <properties> 
     <property name="toplink.jdbc.url"value="jdbc:sqlserver://localhost:1433;databaseName=Application"/> 
     <property name="toplink.jdbc.user" value="sa"/> 
     <property name="toplink.jdbc.password" value="Infosys1"/> 
     <property name="toplink.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/> 
    </properties> 



</persistence-unit> 

我的服務類是:

package com.application.service; 

    import javax.persistence.EntityManager; 
    import javax.persistence.EntityManagerFactory; 
    import javax.persistence.EntityTransaction; 
    import javax.persistence.Persistence; 

    import com.application.entity.ProductEntity; 
    import com.application.to.productTo; 

    public class ProductService 
    { 

public int addProduct(productTo to) 
{ 
    EntityManager em = null; 
    ProductEntity product = new ProductEntity(); 
    try { 
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("Sub"); 

     em = emf.createEntityManager(); 
     EntityTransaction et = em.getTransaction(); 
     et.begin(); 

     // Persist Product 

     product.setProductName(to.getProductName()); 
     product.setBasePrice(to.getBasePrice()); 
     product.setSellingPrice(to.getSellingPrice()); 
     product.setQuantity(to.getQuantity()); 
     product.setCompany(to.getCompany()); 

     em.persist(product); 
     et.commit(); 

    } 
    catch (Exception e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    finally 
    { 
     if (em != null) 
     { 
      em.close(); 
     } 
    } 
    return product.getProductId(); 


    } 
    } 

我已經在Java構建路徑和web內容中的庫文件夾中包括下面的jar:

  1. sqljdbc4-2.0.jar
  2. TopLink的要領,agent.jar中
  3. TopLink的essentials.jar

我的persistence.xml路徑\應用程序的\ src \ META-INF \ persistence.xml

請幫助我。提前致謝。

+0

此問題與數據庫無關,它可能與JPA無法找到persistence.xml或類路徑中的單元有關。 persistence.xml需要位於classpath中classes目錄根目錄下的meta-inf目錄中。如果您認爲它在類路徑中找到,請將TopLink日誌記錄屬性設置爲FINEST,以便記錄問題:http://www.oracle.com/technetwork/middleware/ias/configure-logging-092723.html – Chris 2014-08-30 00:41:18

回答

0

在等persistence.xml,你應該有這樣的事情:

<persistence-unit name="myPersistenceUnit"> 

當你創建EntityManagerFactory確保把同一個名字的文件中定義:

Persistence.createEntityManagerFactory("myPersistenceUnit"); 

在你的情況下,你的代碼中有"Sub",文件中有"Application";代碼想要創建一個名稱爲"Sub"但未定義,因此錯誤消息:No Persistence provider for EntityManager named Sub

+0

我更改了持久性單元名稱爲「Sub」,但得到相同的錯誤 – 2014-08-29 21:33:56

+0

嘗試在持久性單元中添加「transaction-type =」RESOURCE_LOCAL「',因此它是 m3th0dman 2014-08-29 21:44:15

+0

我按照你的建議嘗試過,但得到同樣的錯誤 – 2014-08-29 21:57:05

相關問題