无法使用persistence.xml持久保存到sql server数据库

时间:2014-08-29 20:58:54

标签: java jpa

我是java的新手。我正在尝试将我的应用程序连接到我的计算机上的SQL服务器数据库,但是出现以下错误:

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构建路径中包含了以下jars,在webcontent中包含了lib文件夹:

  1. sqljdbc4-2.0.jar
  2. 排名靠前的必需品-agent.jar中
  3. 排名靠前-essentials.jar
  4. 我的persistence.xml路径是\ Application \ src \ META-INF \ persistence.xml

    请帮助我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

persistence.xml等中,你应该有这样的东西:

<persistence-unit name="myPersistenceUnit">

当您创建EntityManagerFactory时,请确保提供与文件中定义的名称相同的名称:

Persistence.createEntityManagerFactory("myPersistenceUnit");

在您的情况下,代码中有"Sub",文件中有"Application";代码想要创建一个名为"Sub"但未定义的代码,因此出现错误消息:No Persistence provider for EntityManager named Sub