使用Hibernate作为我的JPA提供程序时出现各种错误

时间:2016-05-06 10:57:04

标签: hibernate jpa persistence.xml

我尝试了JPA,所以我已经按照指南进行了尝试,并尝试自己使用Hibernate。我创建了一个实体" testEntity"它只有两个变量,并用一个主方法来测试它。

我的连接池有一个已经过测试和工作的JDBC连接。我配置了持久性单元来使用它。检查我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
             version="2.1">

<persistence-unit name="PruebaswebPU" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider    </provider>
    <jta-data-source>java:app/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>

我在我的测试类中使用它:

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

        testEntity ent = new testEntity();
        ent.setId(5l);
        ent.setNombre("Nombraco");

        entitymanager.persist(ent);
        entitymanager.getTransaction().commit();

        entitymanager.close();
        emfactory.close();

...但我收到了一堆错误:

may 06, 2016 12:52:26 PM org.hibernate.ejb.HibernatePersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
may 06, 2016 12:52:26 PM org.hibernate.ejb.HibernatePersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead. 
may 06, 2016 12:52:26 PM org.hibernate.ejb.HibernatePersistence logDeprecation WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead. 

Exception in thread "main" javax.persistence.PersistenceException: Unable to locate persistence units   
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:99) 
    at org.hibernate.ejb.HibernatePersistence.getEntityManagerFactoryBuilderOrNull(HibernatePersistence.java:93)
    at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:86)
    at org.hibernate.ejb.HibernatePersistence.getEntityManagerFactoryBuilderOrNull(HibernatePersistence.java:101)
    at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:67)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)     
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at testing.samplePersistence.main(samplePersistence.java:20)

Caused by: javax.persistence.PersistenceException: Invalid persistence.xml. Error parsing XML [line : -1, column : -1] : cvc-complex-type.2.4.b: El contenido del elemento 'persistence' no está completo. Se esperaba uno de '{"http://xmlns.jcp.org/xml/ns/persistence":persistence-unit}'.
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.validate(PersistenceXmlParser.java:377)     
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.loadUrl(PersistenceXmlParser.java:310)  
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.parsePersistenceXml(PersistenceXmlParser.java:114) 
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.doResolve(PersistenceXmlParser.java:104)    
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.locatePersistenceUnits(PersistenceXmlParser.java:86) 
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:95)    ... 8 more 
C:\Users\hp\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

我错过了什么吗?提前谢谢!

编辑:

Netbeans用于此项目的自动添加库的一个小屏幕截图:

Libraries

1 个答案:

答案 0 :(得分:0)

您的问题是persistence.xml未完成。您可能需要一些额外的部分。有关更多信息,请参阅此tutorial

persistence.xml还有额外的空间。

首次更改persistence.xml

<provider>org.hibernate.jpa.HibernatePersistenceProvider    </provider>

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

然后跑。如果出现任何问题,请通知我。

UPDATE:

请您更改以下版本:

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">