2016-05-05 79 views
-1

我是JPA的新手,我很迷茫。javax.persistence.PersistenceException:没有名为Eclipselink_JPA的EntityManager的持久性提供程序

我在netbeans中创建了一个企业项目来做一些测试。使用向导创建此持久性单元。我说这是工作的正确的一个JNDI连接,它已经进行了测试:

<?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="persistencia" transaction-type="JTA"> 
    <jta-data-source>jdbc/nuevaConexion</jta-data-source> 
    <exclude-unlisted-classes>false</exclude-unlisted-classes> 
    <properties> 
     <property name="javax.persistence.schema-generation.database.action" value="create"/> 
    </properties> 
    </persistence-unit> 
</persistence> 

后来,我创建了一些测试变量的样本单位,看看它是如何工作:

@Entity 
public class Entity1 implements Serializable { 

    private static long serialVersionUID = 1L; 

    private String nombre; 
    private int numero; 
    /** 
    * @return the serialVersionUID 
    */ 
    public static long getSerialVersionUID() { 
     return serialVersionUID; 
    } 

    /** 
    * @param aSerialVersionUID the serialVersionUID to set 
    */ 
    public static void setSerialVersionUID(long aSerialVersionUID) { 
     serialVersionUID = aSerialVersionUID; 
    } 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private int id; 

    // setters & getters 

然后,试图使服务CreateEntity1给它一个尝试,并尝试通过调用它来创建一个数据库中的新元素:

public class CreateEntity1 { 

    public static void main(String[ ] args) { 

     EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("Eclipselink_JPA");  
     EntityManager entitymanager = emfactory.createEntityManager(); 
     entitymanager.getTransaction().begin(); 

     Entity1 ent = new Entity1(); 
     ent.setId(1); 
     ent.setNombre("Mi entidad"); 
     ent.setNumero(123); 

     entitymanager.persist(ent); 
     entitymanager.getTransaction().commit(); 
     entitymanager.close(); 
     emfactory.close(); 
    } 
} 

...但它不工作。我收到以下错误:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named Eclipselink_JPA at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54) at service.CreateEntity1.main(CreateEntity1.java:22) C:\Users\hp\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

我在做什么错?

编辑:我将持久性提供者的名称更改为好的,但现在我得到了一大堆错误。

编辑2:将所有内容更改为休眠类型。仍然得到一个巨大的错误:

Exception in thread "main" Local Exception Stack: Exception [EclipseLink-30009] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException Exception Description: An exception was thrown while trying to load persistence unit at url: file:/C:/Users/hp/Documents/NetBeansProjects/PruebasJ2EE/PruebasJ2EE-ejb/build/classes/ Internal Exception: Exception [EclipseLink-30004] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException Exception Description: An exception was thrown while processing persistence.xml from URL: file:/C:/Users/hp/Documents/NetBeansProjects/PruebasJ2EE/PruebasJ2EE-ejb/build/classes/ Internal Exception: (1. El destino de la instrucción de procesamiento que coincide con "[xX][mM][lL]" no está permitido.) at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionLoadingFromUrl(PersistenceUnitLoadingException.java:100) at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processPersistenceArchive(PersistenceUnitProcessor.java:616) at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.getPersistenceUnits(PersistenceUnitProcessor.java:500) at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.findPersistenceUnitInfoInArchive(JPAInitializer.java:178) at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.findPersistenceUnitInfoInArchives(JPAInitializer.java:160) at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.findPersistenceUnitInfo(JPAInitializer.java:141) at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:188) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54) at service.CreateEntity1.main(CreateEntity1.java:22) Caused by: Exception [EclipseLink-30004] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException Exception Description: An exception was thrown while processing persistence.xml from URL: file:/C:/Users/hp/Documents/NetBeansProjects/PruebasJ2EE/PruebasJ2EE-ejb/build/classes/ Internal Exception: (1. El destino de la instrucción de procesamiento que coincide con "[xX][mM][lL]" no está permitido.) at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionProcessingPersistenceXML(PersistenceUnitLoadingException.java:118) at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processPersistenceXML(PersistenceUnitProcessor.java:665) at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processPersistenceArchive(PersistenceUnitProcessor.java:614) ... 8 more Caused by: (1. El destino de la instrucción de procesamiento que coincide con "[xX][mM][lL]" no está permitido.) at org.eclipse.persistence.internal.jpa.deployment.xml.parser.XMLExceptionHandler.error(XMLExceptionHandler.java:28) at org.eclipse.persistence.internal.jpa.deployment.xml.parser.XMLExceptionHandler.fatalError(XMLExceptionHandler.java:34) at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:180) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441) at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368) at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1436) at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanPIData(XMLScanner.java:723) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanPIData(XMLDocumentFragmentScannerImpl.java:1018) at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanPI(XMLScanner.java:691) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:912) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649) at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processPersistenceXML(PersistenceUnitProcessor.java:655) ... 9 more C:\Users\hp\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

+2

试着用'Persistence.createEntityManagerFactory( “persistencia”);',你必须指定正确的持久性单元名称。 –

回答

1

你已经错过了<provider>...尝试添加这样的:

<?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="persistencia" transaction-type="JTA"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    ... 
+0

它不断失败;检查我现在要做的编辑 – Zerok

+0

如果CLASSPATH中只有1个,则不必指定提供者 –

相关问题