无法找到持久性单位

时间:2017-06-11 13:08:27

标签: java web-services rest netbeans-8 persistence.xml

我有一个使用maven标准的网络项目 每次我执行Web服务请求时它都会正常执行,但是当它进入错误的插入部分时

javax.persistence.PersistenceException: Unable to locate persistence units

我已经尝试过所有唱歌但我找不到解决问题的方法。

我要把我的代码给你看。

HibernateUtil.java

package hibernate;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class HibernateUtil {

    private final EntityManagerFactory factory;
    private static HibernateUtil hibernateUtil;

    //Estabelece a conexão com o BD!
    private HibernateUtil() {
        factory = Persistence.createEntityManagerFactory("Persistencia");//Persistencia: nome referenciado no persistence.xml
    }

    //Retorna a instância da classe EntityManagerFactory!
    public EntityManagerFactory getFactory() {
        return factory;
    }

    //Verifica se a instância já foi criada, se sim, retorna a instância, caso contrário cria uma!
    public static HibernateUtil getInstance() {
        if (hibernateUtil == null) {
            hibernateUtil = new HibernateUtil();
        }
        return hibernateUtil;
    }
}

persistencia.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="Persistencia" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <!--<provider>org.hibernate.ejb.HibernatePersistence</provider>-->
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <class>entidade.Pessoa</class>
    <class>entidade.Usuario</class>
    <class>entidade.Publicacao</class>
    <class>entidade.Orgao</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/divulgueWeb?zeroDateTimeBehavior=convertToNull"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.password" value="1996"/>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="true"/>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
  </persistence-unit>
</persistence>

如何解决这种责骂?

Ps:我正在使用tomcat

0 个答案:

没有答案