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 hispersistence.xml
, may be just above the declaration ofclass
, and see if it works. Something else is in the classpath when executing themain
. See if there is indeed a preview JPA, besides trying to change thetransaction-type
, removing.– Bruno César
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
– Reginaldo