Я новичок в java. Я пытаюсь подключить приложение к базе данных SQL-сервера на моей машине, но получаю следующее сообщение об ошибке:Не удалось сохранить базу данных sql-сервера, используя persistence.xml
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();
}
}
Я включил ниже jars в путь сборки java, а также в папку lib в webcontent:
- sqljdbc4-2.0.jar
- TOPLINK-основы-agent.jar
- TopLink-essentials.jar
Мой persistence.xml путь \ Application \ SRC \ META-INF \ persistence.xml
Просьба помочь мне. Заранее спасибо.
Эта проблема не связана с базой данных, скорее всего, это связано с тем, что JPA не находит ваш persistence.xml или блок в пути к классам. Файл persistence.xml должен находиться в каталоге meta-inf в корне каталога классов в пути к классам. Если вы считаете, что он найден на пути к классам, установите свойство регистрации TopLink в FINEST, чтобы он регистрировал проблемы: http://www.oracle.com/technetwork/middleware/ias/configure-logging-092723.html – Chris