JPA error with Wildfly - No Persistence Provider for Entitymanager named Project

Asked

Viewed 717 times

1

Hello!

I am creating a very simple test application using JPA, Wildfly as application server and Oracle Database 11g Express Edition as database.

The purpose of the Test class is to execute a People query. Whenever I run the Test class, the error appears "No Persistence Provider for Entitymanager named Project".

When I change the server to Tomcat 8 or Glassfish works normally, I just can’t get it to work on Wildfly. I’d really appreciate it if someone would help me.

Below is more information.

Stacke Trace:

Exception in thread "main" java.lang.ExceptionInInitializerError
at br.com.pessoa.dao.PessoaDAO.getPessoas(PessoaDAO.java:13)
at br.com.pessoa.util.Teste.main(Teste.java:13)
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named Projeto
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at br.com.pessoa.util.JPAUtil.<clinit>(JPAUtil.java:9)
... 2 more

Classe Pessoa:

package br.com.pessoa.model;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@NamedQuery(name="Pessoa.findAll", query="SELECT p FROM Pessoa p")
public class Pessoa implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="PESSOA_PESSOAID_GENERATOR", sequenceName="SEQ_PESSOA")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="PESSOA_PESSOAID_GENERATOR")
    @Column(name="PESSOA_ID")
    private long pessoaId;

    @Column(name="CPF_CNPJ")
    private String cpfCnpj;

    @Temporal(TemporalType.DATE)
    @Column(name="DATA_CADASTRO")
    private Date dataCadastro;

    @Temporal(TemporalType.DATE)
    @Column(name="DATA_NASCIMENTO")
    private Date dataNascimento;

    private String email;

    private String endereco;

    private String nome;

    private String sexo;

    private String telefone;

    public Pessoa() {
    }

    public long getPessoaId() {
        return pessoaId;
    }

    public void setPessoaId(long pessoaId) {
        this.pessoaId = pessoaId;
    }

    public String getCpfCnpj() {
        return cpfCnpj;
    }

    public void setCpfCnpj(String cpfCnpj) {
        this.cpfCnpj = cpfCnpj;
    }

    public Date getDataCadastro() {
        return dataCadastro;
    }

    public void setDataCadastro(Date dataCadastro) {
        this.dataCadastro = dataCadastro;
    }

    public Date getDataNascimento() {
        return dataNascimento;
    }

    public void setDataNascimento(Date dataNascimento) {
        this.dataNascimento = dataNascimento;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getEndereco() {
        return endereco;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getSexo() {
        return sexo;
    }

    public void setSexo(String sexo) {
        this.sexo = sexo;
    }

    public String getTelefone() {
        return telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }        

}

Personal Class:

package br.com.pessoa.dao;

import java.util.List;
import javax.persistence.EntityManager;    
import br.com.pessoa.model.Pessoa;
import br.com.pessoa.util.JPAUtil;

public class PessoaDAO {

    public List<Pessoa> getPessoas(){
        EntityManager em = JPAUtil.getEntityManager();
        return em.createNamedQuery("Pessoa.findAll",Pessoa.class).getResultList();
    }    

}

Jpautil class:

package br.com.pessoa.util;

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

public final class JPAUtil {

    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("Projeto");

    public static EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

}

Test class (Main method):

package br.com.pessoa.util;

import java.util.List;    
import br.com.pessoa.dao.PessoaDAO;
import br.com.pessoa.model.Pessoa;

public class Teste {    
    public static void main(String[] args) {    
        PessoaDAO pessoaDAO = new PessoaDAO();        
        List<Pessoa> pessoas = pessoaDAO.getPessoas();    
    }    
}

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="Projeto" transaction-type="RESOURCE_LOCAL">

        <class>br.com.pessoa.model.Pessoa</class>

        <properties>

            <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
            <property name="javax.persistence.jdbc.user" value="xxx"/>
            <property name="javax.persistence.jdbc.password" value="xxx"/>

            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="true"/>  
            <property name="hibernate.format_sql" value="true"/>            

        </properties>
    </persistence-unit>
</persistence>

Thank you!

  • You are in a JEE container, can use JTA and retrieve an EM from the CDI context. Still, if you want to use how you put it, add <provider>org.hibernate.ejb.HibernatePersistence</provider> in his persistence.xml, may be just above the declaration of class, and see if it works. Something else is in the classpath when executing the main. See if there is indeed a preview JPA, besides trying to change the transaction-type, removing.

  • I made the changes you suggested and it still doesn’t work. By changing the project to use a Datasource JTA and injecting Entitymanager, the Test class keeps giving error, this time returning me a java.lang.Nullpointerexception. Entitymanager is null, not being injected. The strange thing is that in Glassfish this same code works normally, only in Wildfly 8.2 that does not

2 answers

1

To create the datasource manually, change to transaction-type="JTA", deletes the persistence.xml lines:

    <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
    <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
    <property name="javax.persistence.jdbc.user" value="xxx"/>
    <property name="javax.persistence.jdbc.password" value="xxx"/>

Add these settings within the wildfly standalone.xml, within the datasources tag':

<datasource jta="true" jndi-name="java:jboss/datasources/nomeProjetoDS" pool-name="nomeProjetoDS" enabled="true" use-java-context="true">
                    <connection-url>jdbc:oracle:thin:@localhost:1521:xe</connection-url>
                    <driver>oracle</driver>
                    <pool>
                        <min-pool-size>1</min-pool-size>
                        <max-pool-size>5</max-pool-size>
                    </pool>
                    <security>
                        <user-name>usuario</user-name>
                        <password>senha</password>
                    </security>
                </datasource>

Inside the 'drivers' tag, include:

<driver name="oracle" module="com.oracle">
                        <xa-datasource-class> oracle.jdbc.xa.client.OracleXADataSource </xa-datasource-class>
                    </driver>

Now you have to include your driver inside a module in the wildfly. In your case it is in a directory such as "wildfly-10.0.0.Final/modules/system/layers/base/com/oracle/main".

This link can help you set up the driver: example-datasource-oracle-wildfly

0

To use JTA you must create a Data Source.

Go to localhost:9990 and create a.

you can also use the sample data source that is created automatically.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.