Org.hibernate.exception.SQLGrammarException: could not load an entity

7 respostas
E

Bom dia caros amigos, gostaria de saber o porquê que está dando esse problema. Eu andei assistindo o vídeo do Dyego, e então fui tentar fazer, porém da erro.

Estou com o eclipse galileo, utilizando JBoss Tool.

aqui está meu persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="conexao" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <mapping-file>META-INF/consultas.xml</mapping-file>
        <class>br.com.entidades.People</class>
        <properties>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.username" value="postgres" />
            <property name="hibernate.connection.password" value="1234" />
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postgres" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        </properties>
    </persistence-unit>
   /code]

[code]
    <persistence-unit name="xxx" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <mapping-file>META-INF/consultas.xml</mapping-file>
        <properties>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.username" value="root" />
            <property name="hibernate.connection.password" value="1234" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/SISCONTV" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        </properties>
    </persistence-unit>
   
</persistence>
aqui está minha classe People
package br.com.entidades;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;



@Entity(name="People")

public class People implements java.io.Serializable{

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(nullable= false , name="id")
    private Integer id;
    @Column(name="name")
    private String name;
    @Column(name="age")
    private Integer age;
    /**
     *
     */
    public People() {
       
    }
   
    public People(Integer id){
        this.id = id;
    }
   
   
    /**
     * @param id
     * @param name
     * @param age
     */
    public People(Integer id, String name, Integer age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

   
   
    /**
     * @return the id
     */
    public Integer getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the age
     */
    public Integer getAge() {
        return age;
    }
    /**
     * @param age the age to set
     */
    public void setAge(Integer age) {
        this.age = age;
    }
    /**
     * @return the serialversionuid
     */
    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        People other = (People) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
   
   
   
}
e aqui está minha appClass
package br.com;

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

import br.com.entidades.People;

public class TheAppClass {


    public static void main(String[] args) {

        EntityManagerFactory emf = Persistence
                .createEntityManagerFactory("conexao");

        EntityManager em = emf.createEntityManager();

        People p = em.find(People.class, 1);

        System.out.println("Nome da pessoa é: " + p.getName());

        em.close();

        emf.close();
    }

}
... Dae, da esse erro:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select people0_.id as id0_0_, people0_.age as age0_0_, people0_.name as name0_0_ from People people0_ where people0_.id=?
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not load an entity: [br.com.entidades.People#1]
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
    at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:202)
    at br.com.TheAppClass.main(TheAppClass.java:19)
Caused by: org.hibernate.exception.SQLGrammarException: could not load an entity: [br.com.entidades.People#1]
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.loadEntity(Loader.java:1895)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
    at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
    at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
    at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
    at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
    at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223)
    at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
    at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
    at org.hibernate.impl.SessionImpl.get(SessionImpl.java:842)
    at org.hibernate.impl.SessionImpl.get(SessionImpl.java:835)
    at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:182)
    ... 1 more
Caused by: org.postgresql.util.PSQLException: ERRO: relação "people" não existe
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1547)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1315)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:190)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:451)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:350)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:254)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1808)
    at org.hibernate.loader.Loader.doQuery(Loader.java:697)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
    ... 13 more

alguem ae pra me ajudae ??? Agradeço aneee

7 Respostas

B
Caused by: org.postgresql.util.PSQLException: ERRO: relação "people" não existe

Poste o código onde é declarada esta relação.

E

Eu mexi no persistence, deixei assim:

<?xml version="1.0" encoding="UTF-8"?>  
    <persistence version="1.0"  
        xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">  
        <persistence-unit name="conexao" transaction-type="RESOURCE_LOCAL">  
            <provider>org.hibernate.ejb.HibernatePersistence</provider>  
            <mapping-file>META-INF/consultas.xml</mapping-file>  
            <class>br.com.entidades.People</class>  
            <properties>  
               <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />  
               <property name="hibernate.connection.username" value="postgres" />  
               <property name="hibernate.connection.password" value="1234" />  
               <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postgres" />  
               <property name="hibernate.show_sql" value="true" />  
               <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />  
           </properties>  
       </persistence-unit>  
    
        
   </persistence>

E o erro que está dando agora é:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select people0_.id as id0_0_, people0_.age as age0_0_, people0_.name as name0_0_ from People people0_ where people0_.id=?
Exception in thread "main" java.lang.NullPointerException
	at br.com.TheAppClass.main(TheAppClass.java:21)

eu queria saber pq ele ta procurando em “people0_.id” , se o nome da tabela é só People ? …

I

O hibernate da apelido para as tabelas e colunas.

select people0_.id as id0_0_, people0_.age as age0_0_, people0_.name as name0_0_ from People people0_ where people0_.id=?

E

Valeu Inacio, eu fui perceber isso depois ! Obrigado.

Sim, continuando, meu erro ainda continua, alguém pra da uma força ae ?

I

Executando essa query direto no banco de dados retorna o q?

select people0_.id as id0_0_, people0_.age as age0_0_, people0_.name as name0_0_ from People people0_ where people0_.id=?
E

Não retorna nada

¬¬ …

E

Galera, resolvi meu problema ! O problema estava no postgres ! Quando eu criei a tabela People, eu não sei porque, a tabela People foi criada entre aspas ou seja, então minha classe People ficou assim:

package br.com.entidades;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity(name="People")
@Table(name = "\"People\"")
public class People implements java.io.Serializable {


............................

E foi resolvido ! =D

Agradeoç a atenção de todos !!!

Criado 7 de agosto de 2009
Ultima resposta 7 de ago. de 2009
Respostas 7
Participantes 3