Bom dia!
Alguem sabe por qual motivo isso acontece?
CODIGO:
[code] EntityManagerFactory emf = Persistence.createEntityManagerFactory(“SftwPU”);
EntityManager em = emf.createEntityManager();
Query query = em.createNamedQuery("Usuario.findAll");
List<Usuario> usuarioList = query.getResultList();
ArrayList list = new ArrayList();
for(Usuario u : usuarioList){
//teste
System.out.println(u.getId());
list.add(u);
}[/code]
ERRO:
[code][EL Info]: 2012-10-15 07:36:53.62–ServerSession(2120540210)–EclipseLink, version: Eclipse Persistence Services - 2.3.2.v20111125-r10461
[EL Info]: 2012-10-15 07:36:54.078–ServerSession(2120540210)–file:/C:/Sistema Renato/SW/Sftw/build/classes/_SftwPU login successful
[EL Warning]: 2012-10-15 07:36:54.171–UnitOfWork(1976311709)–Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERRO: relação “usuario” não existe
Posição: 57
Error Code: 0
Call: SELECT id, idioma, nivel, nome, nome_pessoa, senha FROM Usuario
Query: ReadAllQuery(name=“Usuario.findAll” referenceClass=Usuario sql=“SELECT id, idioma, nivel, nome, nome_pessoa, senha FROM Usuario”)
Mon Oct 15 07:36:54 BRT 2012 [ERROR]
in org.openswing.swing.table.client.Grids.loadData():
Error while fetching data.
Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERRO: relaᅢ댃o “usuario” nᅢᆪo existe
Posiᅢ댃o: 57
Error Code: 0
Call: SELECT id, idioma, nivel, nome, nome_pessoa, senha FROM Usuario
Query: ReadAllQuery(name=“Usuario.findAll” referenceClass=Usuario sql=“SELECT id, idioma, nivel, nome, nome_pessoa, senha FROM Usuario”)
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:644)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:535)
at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1717)
at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:566)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:207)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:193)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:264)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:646)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2611)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2570)
at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:420)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1081)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:844)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1040)
at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:392)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1128)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2871)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1516)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1498)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1463)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:485)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultList(EJBQueryImpl.java:742)
at CadUsuariosSistema.CadastroUsuariosSistemaController.loadData(CadastroUsuariosSistemaController.java:71)
at org.openswing.swing.table.client.Grids.loadData(Grids.java:1794)
at org.openswing.swing.table.client.Grids.access$800(Grids.java:58)
at org.openswing.swing.table.client.Grids$LoadDataThread.run(Grids.java:4064)
Caused by: org.postgresql.util.PSQLException: ERRO: relaᅢ댃o “usuario” nᅢᆪo existe
Posiᅢ댃o: 57
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2101)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1834)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:510)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:386)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:271)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeSelect(DatabaseAccessor.java:931)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:607)
… 25 more[/code]
A entity Usuario:
[code]/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package CadUsuariosSistema;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
-
@author RR
*/
@Entity
@Table(name = “Usuario”)
@XmlRootElement
@NamedQueries({
@NamedQuery(name = “Usuario.findAll”, query = “SELECT u FROM Usuario u”),
@NamedQuery(name = “Usuario.findById”, query = “SELECT u FROM Usuario u WHERE u.id = :id”),
@NamedQuery(name = “Usuario.findByNome”, query = “SELECT u FROM Usuario u WHERE u.nome = :nome”),
@NamedQuery(name = “Usuario.findBySenha”, query = “SELECT u FROM Usuario u WHERE u.senha = :senha”),
@NamedQuery(name = “Usuario.findByNomePessoa”, query = “SELECT u FROM Usuario u WHERE u.nomePessoa = :nomePessoa”),
@NamedQuery(name = “Usuario.findByIdioma”, query = “SELECT u FROM Usuario u WHERE u.idioma = :idioma”),
@NamedQuery(name = “Usuario.findByNivel”, query = “SELECT u FROM Usuario u WHERE u.nivel = :nivel”)})
public class Usuario implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = “id”)
private Integer id;
@Column(name = “nome”)
private String nome;
@Column(name = “senha”)
private String senha;
@Column(name = “nome_pessoa”)
private String nomePessoa;
@Column(name = “idioma”)
private Character idioma;
@Column(name = “nivel”)
private Character nivel;
public Usuario() {
}
public Usuario(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public String getNomePessoa() {
return nomePessoa;
}
public void setNomePessoa(String nomePessoa) {
this.nomePessoa = nomePessoa;
}
public Character getIdioma() {
return idioma;
}
public void setIdioma(Character idioma) {
this.idioma = idioma;
}
public Character getNivel() {
return nivel;
}
public void setNivel(Character nivel) {
this.nivel = nivel;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won’t work in the case the id fields are not set
if (!(object instanceof Usuario)) {
return false;
}
Usuario other = (Usuario) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return “CadUsuariosSistema.Usuario[ id=” + id + " ]";
}
}
[/code]
Tente colocar FROM Usuario U
Internal Exception: org.postgresql.util.PSQLException: ERRO: relação "usuario" não existe
Tenta fazer sua consulta dentro de um gerenciador, como PGAdmin, EMS PostgreSQL, e etc.
Veja se a consulta retorna registros, porque o erro da Exception informada diz que é algo mais interno, como por exemplo, pode ser uma chave estrangeira, ou primária.
Abraços.
Quando faço, assim não roda:
select u from Usuario u
Qd eu faço o SELECT assim vai no proprio banco:
SELECT
"Usuario".id,
"Usuario".nome,
"Usuario".senha,
"Usuario".nome_pessoa,
"Usuario".idioma,
"Usuario".nivel
FROM
public."Usuario";
E assim tb funciona, qd eu passo para o proprio BD:
SELECT id, nome, nome_pessoa, idioma, nivel
FROM "Usuario";
faça SELECT u FROM Usuario U
Qd eu faço:
Retorna isso:
[WARNING ] SELECT u FROM Usuario U
ERRO: relação "usuario" não existe
LINE 1: SELECT u FROM Usuario U
^
BANCO POSTGRESQL
Agora se eu coloco : Query query = em.createNamedQuery(“SELECT u FROM Usuario U”);
Fala isso:
Mon Oct 15 10:45:36 BRT 2012 [ERROR]
in org.openswing.swing.table.client.Grids.loadData():
Error while fetching data.
java.lang.IllegalArgumentException: NamedQuery of name: SELECT u FROM Usuario U not found.
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getDatabaseQueryInternal(EJBQueryImpl.java:577)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1043)
at CadUsuariosSistema.CadastroUsuariosSistemaController.loadData(CadastroUsuariosSistemaController.java:69)
at org.openswing.swing.table.client.Grids.loadData(Grids.java:1794)
at org.openswing.swing.table.client.Grids.access$800(Grids.java:58)
at org.openswing.swing.table.client.Grids$LoadDataThread.run(Grids.java:4064)
O que pode ser?
você não pode escrever uma query dentro do createNamedQuery.
Se quiser definir a query aí use createQuery
Se eu deixar ssim, funciona?
[quote=renatofox89]Se eu deixar ssim, funciona?
Query query = em.createNamedQuery("Usuario.findAll");
[/quote]Você testou? O que aconteceu?
Retorna isso:
Aponta erro nessa linha : List usuarioList = query.getResultList();
Mon Oct 15 11:14:16 BRT 2012 [ERROR]
in org.openswing.swing.table.client.Grids.loadData():
Error while fetching data.
Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERRO: relaᅢ댃o "usuario" nᅢᆪo existe
Posiᅢ댃o: 57
Error Code: 0
Call: SELECT id, idioma, nivel, nome, nome_pessoa, senha FROM Usuario
Query: ReadAllQuery(name="Usuario.findAll" referenceClass=Usuario sql="SELECT id, idioma, nivel, nome, nome_pessoa, senha FROM Usuario")
[EL Info]: 2012-10-15 11:14:15.719--ServerSession(853986125)--EclipseLink, version: Eclipse Persistence Services - 2.3.2.v20111125-r10461
[EL Info]: 2012-10-15 11:14:16.164--ServerSession(853986125)--file:/C:/Sistema Renato/SW/Sftw/build/classes/_SftwPU login successful
-----------------------Chamou --->EJBQueryImpl(ReadAllQuery(name="Usuario.findAll" referenceClass=Usuario sql="SELECT id, idioma, nivel, nome, nome_pessoa, senha FROM Usuario"))
[EL Warning]: 2012-10-15 11:14:16.257--UnitOfWork(1113532981)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERRO: relação "usuario" não existe
Posição: 57
Error Code: 0
Call: SELECT id, idioma, nivel, nome, nome_pessoa, senha FROM Usuario
Query: ReadAllQuery(name="Usuario.findAll" referenceClass=Usuario sql="SELECT id, idioma, nivel, nome, nome_pessoa, senha FROM Usuario")
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:644)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:535)
at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:1717)
at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:566)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:207)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:193)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:264)
at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:646)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2611)
at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRows(ExpressionQueryMechanism.java:2570)
at org.eclipse.persistence.queries.ReadAllQuery.executeObjectLevelReadQuery(ReadAllQuery.java:420)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeDatabaseQuery(ObjectLevelReadQuery.java:1081)
at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:844)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1040)
at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:392)
at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1128)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2871)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1516)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1498)
at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1463)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:485)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultList(EJBQueryImpl.java:742)
at CadUsuariosSistema.CadastroUsuariosSistemaController.loadData(CadastroUsuariosSistemaController.java:73)
at org.openswing.swing.table.client.Grids.loadData(Grids.java:1794)
at org.openswing.swing.table.client.Grids.access$800(Grids.java:58)
at org.openswing.swing.table.client.Grids$LoadDataThread.run(Grids.java:4064)
Caused by: org.postgresql.util.PSQLException: ERRO: relaᅢ댃o "usuario" nᅢᆪo existe
Posiᅢ댃o: 57
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2101)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1834)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:510)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:386)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:271)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeSelect(DatabaseAccessor.java:931)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:607)
... 25 more
------------------------
The resource 'Erro Enquando Carrega os dados' is not defined.
The resource 'Erro ao carregar os dados' is not defined.
The resource 'ok' is not defined.
Qd eu faço:
SELECT u FROM Usuario U;
ai em cima ta errado, SELECT u FROM Usuario UUUUUU ??? o u tem que ser minusculo.
Retorna isso:
[WARNING ] SELECT u FROM Usuario U
ERRO: relação "usuario" não existe
LINE 1: SELECT u FROM Usuario U
^
Se mesmo colocando o U minusculo der errado experimenta colocar “.*”, como abaixo.
SELECT u.* FROM Usuario u
e outra, ja tentou rodar isso direto no PGAdmin ?
Karaka,
usar:
nunca vi essa! mas tentei n dá certo
[quote=renatofox89]Karaka,
usar:
nunca vi essa! mas tentei n dá certo[/quote]Isso não funciona com JPQL.
Você precisa entender a diferença entre NamedQuery, NativeQuery e como funciona.
Esse post mostra sobre consultas: JPA Consultas e Dicas.
Veja c no seu banco de dados se sua tabela está com o nome usuario (tudo minusculo). SE não estiver, tente fazer isso.
acredite, isso existe ^^
experimenta usar no pgadmin pro c ver =D
uma vez tive um problema num sistema, fiquei batendo cabeça, resolvi por dessa forma e funcionou, nao perdi meu tempo tentando entender aquilo devido ao tempo, mas funcionou, isso que importa.
Bom, em relação ao seu problema, ainda não consegui identificar o erro, mas de repente voce poderia verificar suas configuracoes do hibernate como estao, dialect,e etc…
Boa sorte, qualquer ideia eu posto aqui. Flw.
Meus conhecimentos de JPQL ainda são bem excassos, vlw pelo link, vo dar uma olhada ^^