Hello
I am creating a very simple test application, using JPA, Wildfly as an application server and Oracle Database 11g Express Edition as a database.
The intent of the Test class is to run a People query. Whenever I run the Test class, the "No Persistence provider for EntityManager named Project" error appears.
When I change the server to Tomcat 8 or Glassfish it works normally, I just can not make it work on Wildfly. I would appreciate it if someone would help me.
See below for 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
Person Class:
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;
}
}
PersonaDAO 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!