Hibernate / JPA不起作用

时间:2016-05-09 08:42:06

标签: hibernate jpa

这是我迄今为止试图解决这个问题的第三个问题。我已经尝试了很多东西,但这仍然失败并且一直抛出完全相同的错误。让我们看看有人可以帮助我。

看,这是我的persistence.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">


    <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>

         <!-- Hibernate properties -->
        <properties>
                  <property name="javax.persistence.schema-generation.database.action" value="create"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
            <property name="hibernate.connection.charSet" value="UTF-8"/>
            <property name="hibernate.validator.apply_to_ddl" value="false"/>
            <property name="hibernate.validator.autoregister_listeners" value="false"/>
            <property name="hibernate.show_sql" value="false"/>
        </properties>        
    </persistence-unit>
</persistence>

它通过JNDI连接池“jdbc / nuevaConexion”连接到WAMP MySQL服务器。

在这里我尝试使用它(只是测试目的,作为我的实体!):

public class samplePersistence {

    public static void main(String[] args) {

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

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

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

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

我总是得到这个错误,这很奇怪,因为它告诉我更改提供程序,但是......它已经改变了。检查:

> may 09, 2016 10:36:49 AM 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 09, 2016
> 10:36:49 AM 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 09, 2016
> 10:36:49 AM 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)

2 个答案:

答案 0 :(得分:0)

似乎无法验证您的persistence.xml文件。它包含无效字符(检查编码,BOM ...)或架构无法解析(您应该删除声明xsi:schemaLocation =“http://java.sun.com/xml/ns/persistence { {3}}“)。

答案 1 :(得分:0)

升级Hibernate版本。使用版本大于或等于4.3.5。

https://hibernate.atlassian.net/browse/JPA-67