Pessoal boa noite, estou iniciando os estudos no hibernate e estou tendo um pequeno problema…
segue minha hibernateutil
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
AnnotationConfiguration cfg = new AnnotationConfiguration();
cfg.configure("hibernate.cfg.xml");
return cfg.buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
minha classe…
[code]
import java.sql.Date;
import javax.persistence.*;
@Entity
@Table(name = “contato”)
public class ContatoAnnotations{
@Id
@GeneratedValue
@Column (name = "codigo")
private Integer codigo;
@Column (name = "nome", length = 50, nullable = true)
private String nome;
@Column (name = "telefone", length = 50, nullable = true)
private String telefone;
@Column (name = "email", length = 50, nullable = true)
private String email;
@Column (name = "dt_cad", nullable = true)
private Date dataCadastro;
@Column (name = "obs", length = 50, nullable = true)
private String observacao;
public Integer getCodigo() {
return codigo;
}
public void setCodigo(Integer codigo) {
this.codigo = codigo;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getDataCadastro() {
return dataCadastro;
}
public void setDataCadastro(Date dataCadastro) {
this.dataCadastro = dataCadastro;
}
public String getObservacao() {
return observacao;
}
public void setObservacao(String observacao) {
this.observacao = observacao;
}
}[/code]
Quando eu tento executar uma consulta me retorna o erro:
“Não foi possível buscar o contato. Erro: contato is not mapped [from contato]”
Exception in thread “main” org.hibernate.HibernateException: org.hibernate.hql.ast.QuerySyntaxException: contato is not mapped [from contato]
at hibernatecomannotations.ContatoCrudAnnotations.listar(ContatoCrudAnnotations.java:104)
at hibernatecomannotations.ContatoCrudAnnotations.main(ContatoCrudAnnotations.java:181)
Caused by: org.hibernate.hql.ast.QuerySyntaxException: contato is not mapped [from contato]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:314)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3355)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3239)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:726)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:577)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:294)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:237)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:98)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1760)
at hibernatecomannotations.ContatoCrudAnnotations.listar(ContatoCrudAnnotations.java:97)
… 1 more
Java Result: 1
Alguém pode me ajudar?
Grato.