我是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:
- sqljdbc4-2.0.jar
- TopLink的要領,agent.jar中
- TopLink的essentials.jar
我的persistence.xml路徑\應用程序的\ src \ META-INF \ persistence.xml
請幫助我。提前致謝。
此問題與數據庫無關,它可能與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