[Erro] Unknow Table

2 respostas
gfkauer

Olá,

Estou aprendendo a utilizar o Hibernate e me surgiu este erro que não consegui resolver via consultas o fórum:

log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).
log4j:WARN Please initialize the log4j system properly.
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Projetos/hibernate-annotations-3.4.0.GA/lib/test/slf4j-log4j12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Projetos/hibernate-validator-4.2.0.Beta2/lib/optional/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
Initial sessin Factory failed.org.hibernate.AnnotationException: @org.hibernate.annotations.Table references an unknown table: USUARIOS
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.gfkauer.util.HibernateUtil.(HibernateUtil.java:23)
at com.gfkauer.DAO.UsuariosDAO.salvar(UsuariosDAO.java:16)
at Principal.main(Principal.java:17)
Caused by: org.hibernate.AnnotationException: @org.hibernate.annotations.Table references an unknown table: USUARIOS
at org.hibernate.cfg.annotations.EntityBinder.processComplementaryTableDefinitions(EntityBinder.java:864)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:710)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3385)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3339)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1323)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1713)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1760)
at com.gfkauer.util.HibernateUtil.(HibernateUtil.java:19)
... 2 more

Minhas classes são as seguintes:

Usuarios
package com.gfkauer.tabelas;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import javax.persistence.Entity;
import org.hibernate.annotations.Table;

@SuppressWarnings("serial")
@Entity
@Table(appliesTo="USUARIOS")
public class Usuarios implements Serializable {

	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	private long id;
	@Column(name="login", nullable=false, length=30)
	private String login;
	@Column(name="senha", nullable=false, length=12)
	private String senha;
	
	public Usuarios(){
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getSenha() {
		return senha;
	}
	public void setSenha(String senha) {
		this.senha = senha;
	}	
}
HibernateUtil
package com.gfkauer.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 * @author advancedit.giovane
 *
 */
public class HibernateUtil {
	private static final SessionFactory sessionFactory;
	static{
		try{
			//Create the sessionFactory from hubernate.cfg.cml
			sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
		} catch(Throwable ex){
			// Make sure you log the exception, as it might be swallowed
			System.err.println("Initial sessin Factory failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}
	public static SessionFactory getSessionFactory(){
		return sessionFactory;
	}
}
UsuarioDAO
package com.gfkauer.DAO;

import org.hibernate.Session;

import com.gfkauer.tabelas.Usuarios;
import com.gfkauer.util.HibernateUtil;

public class UsuariosDAO {
	private Session session;
	public UsuariosDAO(){
	}
	public void salvar(Usuarios usr){
		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(usr);
		session.getTransaction().commit();
	}
}
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
	<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
	<hibernate-configuration>
		<session-factory>
			<!-- Database connection settings -->
			<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
			<property name="connection.url">jdbc:oracle:thin:@172.16.1.66:1521:DBDES</property>
			<property name="connection.username">scott</property>
			<property name="connection.password">tiger</property>
			<!-- JDBC connection pool (use the built-in) -->
			<property name="connection.pool_size">1</property>
			<!-- SQL dialect -->
			<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
			<!-- Enable Hibernate's automatic session context management -->
			<property name="current_session_context_class">thread</property>
			<!-- Disable the second-level cache -->
			<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
			<!-- Echo all executed SQL to stdout -->
			<property name="show_sql">true</property>
			
			<!-- Mapeamento de tabelas  -->
			<mapping class="com.gfkauer.tabelas.Usuarios"/>
		</session-factory>
	</hibernate-configuration>
Principal
import com.gfkauer.DAO.UsuariosDAO;
import com.gfkauer.tabelas.Usuarios;


public class Principal {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Usuarios usr1 = new Usuarios();
		usr1.setLogin("gfkauer");
		usr1.setSenha("padrao");
		UsuariosDAO usrDAO = new UsuariosDAO();
		usrDAO.salvar(usr1);
	}
}

Peço desculpas antecipadas caso este seja um erro banal de mais, mas não sei como solucionar.

2 Respostas

jamirdeajr

Sua base de dados é Oracle, então no hibernate.cfg.xml troque:

Por

A exceção indica que não encontrou a tabela USUARIOS, pode ser pelo dialeto acima ou porque ela não existe no banco de dados. Dá uma conferida.

Boa sorte!

gfkauer

Bom ajustei esta propriedade e não solucionou. Caso alguem tenha alguma outra sugestão.

Criado 10 de maio de 2011
Ultima resposta 11 de mai. de 2011
Respostas 2
Participantes 2