Caused by: org.hibernate.exception.SQLGrammarException: could not load an entity
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>
<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
...
e aqui como está no banco.
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>
<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
...
e aqui como está no banco.
Euclides Filizola
Curtidas 0
Respostas
Dyego Carmo
07/08/2009
Bom dia !
Logo abaixo
@Entity(name="People")
coloque
@Entity(name="People")
@Table(name="People")
e teste denovo.
Logo abaixo
@Entity(name="People")
coloque
@Entity(name="People")
@Table(name="People")
e teste denovo.
GOSTEI 0
Euclides Filizola
07/08/2009
Bom dia Dr. !
agora está aparecendo 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" java.lang.NullPointerException
at br.com.TheAppClass.main(TheAppClass.java:23)
agora está aparecendo 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" java.lang.NullPointerException
at br.com.TheAppClass.main(TheAppClass.java:23)
GOSTEI 0
Dyego Carmo
07/08/2009
Oi
Preciso do Stack Trace completo.
Preciso do Stack Trace completo.
GOSTEI 0
Euclides Filizola
07/08/2009
Dyego, só aparece isso mano !
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:23)
dae quando eu clico em br.com.TheAppClass.main(TheAppClass.java:23),
ele mostra a linha :
System.out.println("Nome da pessoa é: " + p.getName());
É como se não tivesse o Usuario número 1, não é ?
mas aqui está meu select:
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:23)
dae quando eu clico em br.com.TheAppClass.main(TheAppClass.java:23),
ele mostra a linha :
System.out.println("Nome da pessoa é: " + p.getName());
É como se não tivesse o Usuario número 1, não é ?
mas aqui está meu select:
GOSTEI 0
Dyego Carmo
07/08/2009
Exatamente.
Provavelmente nao existe este usuario cadastrado.
Provavelmente nao existe este usuario cadastrado.
GOSTEI 0
Euclides Filizola
07/08/2009
Opa Dyego, errei aqui.
o erro que da no console é esse:
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:23)
.. Dae, é como se não tivesse o Usuario número 1.
mas ta aqui minha img.
o erro que da no console é esse:
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:23)
.. Dae, é como se não tivesse o Usuario número 1.
mas ta aqui minha img.
GOSTEI 0
Dyego Carmo
07/08/2009
na imagem que esta me mostrando nao consigo ver o registro de ID 1 cadastrado...
GOSTEI 0
Euclides Filizola
07/08/2009
Coloquei outra ae embaixo, Da uma olhada ae =D
GOSTEI 0
Dyego Carmo
07/08/2009
Tente salvar um usuario novo para vermos se da algum erro.
GOSTEI 0
Euclides Filizola
07/08/2009
Deixa eu te explicar, eu tentei inserir um usuario com o msm ID, da erro. Até ae beleza, dae eu deletei os 2 usuários que eu criei,
Dae eu criei novamente o usuário ID=1,
Pronto, dae eu executei novamente a appClass
Porém, continua o mesmo erro:
Dae eu criei novamente o usuário ID=1,
Pronto, dae eu executei novamente a appClass
Porém, continua o mesmo erro:
GOSTEI 0
Dyego Carmo
07/08/2009
Troque o p.getName() para apenas p dentro do metodo System.out.println e cole o resultado aqui...
GOSTEI 0
Euclides Filizola
07/08/2009
O resultado é:
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=?
Nome da pessoa é: null
...
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=?
Nome da pessoa é: null
...
GOSTEI 0
Dyego Carmo
07/08/2009
Acredito que voce esta inserindo a pessoa mas nao esta dando o COMMIT nas alteracoes do POSTGRES... crie a pessoa... FECHE o administrador do postgres , volte e de o select... veja se apessoa esta lá...
GOSTEI 0
Euclides Filizola
07/08/2009
Dyego, os dados se encontram lá sim ! =D, quando eu dou um execute no botãozinho verde ,ele comita tbm !
deixa eu te mostrar como está tudo atualmente aqui:
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")
@Table(name="People")
public class People implements java.io.Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Column(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;
}
}
Class TheApp
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);
em.close();
emf.close();
}
}
meu persistence:
<?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 aqui como está meu banco:
deixa eu te mostrar como está tudo atualmente aqui:
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")
@Table(name="People")
public class People implements java.io.Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Column(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;
}
}
Class TheApp
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);
em.close();
emf.close();
}
}
meu persistence:
<?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 aqui como está meu banco:
GOSTEI 0
Dyego Carmo
07/08/2009
Que curioso... prq nao tem nada de errado, o hibernate simplesmente nao tah achando nada quando dah o select...
Pegue esse select que o hibernate tah dando e coloque o id = 1 e veja se retorna...
que estranho...
Pegue esse select que o hibernate tah dando e coloque o id = 1 e veja se retorna...
que estranho...
GOSTEI 0
Euclides Filizola
07/08/2009
Usei esse select:
select people0_.id as id0_0_, people0_.age as age0_0_, people0_.name as name0_0_ from People people0_ where people0_.id=1 ;
Mas não aparece nada, conforme a figura abaixo:
Porém, eu peguei o select correto que é para ser, conforme imagem abaixo,e tem uma coisa interessante: em name, o atributo está entre aspas ! Então eu fiz o mesmo select do hibernate, porém coloquei aspas, e veja oq apareceu:
Ou seja, Deu certo ! Agora eu pergunto: Como eu colocaria as aspas no nome da coluna ?
select people0_.id as id0_0_, people0_.age as age0_0_, people0_.name as name0_0_ from People people0_ where people0_.id=1 ;
Mas não aparece nada, conforme a figura abaixo:
Porém, eu peguei o select correto que é para ser, conforme imagem abaixo,e tem uma coisa interessante: em name, o atributo está entre aspas ! Então eu fiz o mesmo select do hibernate, porém coloquei aspas, e veja oq apareceu:
Ou seja, Deu certo ! Agora eu pergunto: Como eu colocaria as aspas no nome da coluna ?
GOSTEI 0
Dyego Carmo
07/08/2009
nao tem como tirar estas aspas da criacao da coluna "name" da tabela ?
cria ela normalmente... e tira estas ASPAS... e tenta executar aquele select denovo...
cria ela normalmente... e tira estas ASPAS... e tenta executar aquele select denovo...
GOSTEI 0
Euclides Filizola
07/08/2009
Tem como criar sim, é só alterar uma configuração no postgres, mas eu resolvi meu problema ! Não sei se é a melhor maneira, mas quando isso ocorrer, principalmente no postgre, quando a gente cria uma table, de vez enquando ele gera com aspas, é chato isso, dae eu fiz o seguinte :
peguei na minha classe People e fiz isso :
@Entity(name = "People")
@Table(name = "\"People\"")
public class People implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
Onde tem Table, eu usei "People" como tabela, e advinha ?
Funcionou mano ! eheheh. valeu pela sua ajuda, vou continuar lendo e vendo aqui seus videos e tudo, qualquer coisa venho te pedir ajuda ! Agradeço muito ae sua força !
Abraços !
peguei na minha classe People e fiz isso :
@Entity(name = "People")
@Table(name = "\"People\"")
public class People implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
Onde tem Table, eu usei "People" como tabela, e advinha ?
Funcionou mano ! eheheh. valeu pela sua ajuda, vou continuar lendo e vendo aqui seus videos e tudo, qualquer coisa venho te pedir ajuda ! Agradeço muito ae sua força !
Abraços !
GOSTEI 0
Dyego Carmo
07/08/2009
Vixe maria... que coisa doida do Postgres...
Espero ter ajuda :)
Espero ter ajuda :)
GOSTEI 0