Erro informado: "Unknown entity"

0 respostas
emircalife

Não estou encontrando o problema de forma alguma, embora creia que o problema seja no mapeamento das classes, não vejo como esteja acontecendo.

Log do erro no prompt:

set 21, 2017 9:32:19 AM org.hibernate.Version logVersion

INFO: HHH000412: Hibernate Core {5.2.11.Final}

set 21, 2017 9:32:19 AM org.hibernate.cfg.Environment 

INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.max_fetch_depth=5, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;LOCK_TIMEOUT=10000, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.password=<strong><strong>, hibernate.connection.pool_size=5}

set 21, 2017 9:32:19 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity

WARN: HHH90000012: Recognized obsolete hibernate namespace <a href="http://hibernate.sourceforge.net/hibernate-configuration">http://hibernate.sourceforge.net/hibernate-configuration</a>. Use namespace <a href="http://www.hibernate.org/dtd/hibernate-configuration">http://www.hibernate.org/dtd/hibernate-configuration</a> instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.

set 21, 2017 9:32:19 AM org.hibernate.spatial.integration.SpatialService 

INFO: HHH80000001: hibernate-spatial integration enabled : true

set 21, 2017 9:32:19 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager 

INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}

set 21, 2017 9:32:19 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure

WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)

set 21, 2017 9:32:19 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/locadora]

set 21, 2017 9:32:19 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH10001001: Connection properties: {user=root, password=</strong></strong>}

set 21, 2017 9:32:19 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator

INFO: HHH10001003: Autocommit mode: false

set 21, 2017 9:32:19 AM org.hibernate.engine.jdbc.connections.internal.PooledConnections 

INFO: HHH000115: Hibernate connection pool size: 5 (min=1)

Thu Sep 21 09:32:19 BRT 2017 WARN: Establishing SSL connection without servers identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isnt set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to false. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

set 21, 2017 9:32:19 AM org.hibernate.dialect.Dialect 

INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect

set 21, 2017 9:32:19 AM org.hibernate.envers.boot.internal.EnversServiceImpl configure

INFO: Envers integration enabled? : true

Conectado!

set 21, 2017 9:32:20 AM org.hibernate.engine.internal.StatisticalLoggingSessionEventListener end

INFO: Session Metrics {

1652921 nanoseconds spent acquiring 1 JDBC connections;

1771486 nanoseconds spent releasing 1 JDBC connections;

0 nanoseconds spent preparing 0 JDBC statements;

0 nanoseconds spent executing 0 JDBC statements;

0 nanoseconds spent executing 0 JDBC batches;

0 nanoseconds spent performing 0 L2C puts;

0 nanoseconds spent performing 0 L2C hits;

0 nanoseconds spent performing 0 L2C misses;

0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);

0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)

}

Script executado com sucesso.

Erro ao salvar categoria: Unknown entity: com.livro.capitulo3.categoria.Categoria

set 21, 2017 9:32:20 AM org.hibernate.engine.internal.StatisticalLoggingSessionEventListener end

INFO: Session Metrics {

1135999 nanoseconds spent acquiring 1 JDBC connections;

0 nanoseconds spent releasing 0 JDBC connections;

0 nanoseconds spent preparing 0 JDBC statements;

0 nanoseconds spent executing 0 JDBC statements;

0 nanoseconds spent executing 0 JDBC batches;

0 nanoseconds spent performing 0 L2C puts;

0 nanoseconds spent performing 0 L2C hits;

0 nanoseconds spent performing 0 L2C misses;

0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);

0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)

}

Erro ao salvar categoria: Unknown entity: com.livro.capitulo3.categoria.Categoria

set 21, 2017 9:32:20 AM org.hibernate.engine.internal.StatisticalLoggingSessionEventListener end

INFO: Session Metrics {

1195486 nanoseconds spent acquiring 1 JDBC connections;

0 nanoseconds spent releasing 0 JDBC connections;

0 nanoseconds spent preparing 0 JDBC statements;

0 nanoseconds spent executing 0 JDBC statements;

0 nanoseconds spent executing 0 JDBC batches;

0 nanoseconds spent performing 0 L2C puts;

0 nanoseconds spent performing 0 L2C hits;

0 nanoseconds spent performing 0 L2C misses;

0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);

0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)

}

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?> org.hibernate.dialect.MySQL5InnoDBDialect com.mysql.jdbc.Driver jdbc:mysql://localhost/locadora root 1234 thread 5 20 300 50 3000 true true true true
<!-- Mapeando Classes -->
	<mapping resource="com/livro/capitulo3/crudxml/Contato.hbm.xml" />
	<mapping class="com.livro.capitulo3.categoria.Categoria" />
	<mapping class="com.livro.capitulo3.cliente.Cliente" />
	<mapping class="com.livro.capitulo3.endereco.Endereco" />
	<mapping class="com.livro.capitulo3.filme.Filme" />
	<mapping class="com.livro.capitulo3.locacao.Locacao" />
	<mapping class="com.livro.capitulo3.midia.Midia" />
</session-factory>

Categoria.java

package com.livro.capitulo3.categoria;

import java.io.Serializable;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import <a href="http://javax.persistence.Id">javax.persistence.Id</a>;

import javax.persistence.Table;
@Entity

@Table(name=categoria)

public class Categoria implements Serializable {
private static final long serialVersionUID = 3891458183159263624L;

@Id
@GeneratedValue
@Column(name="idCategoria")
private Integer	categoria;

private String descricao;


public Integer getCategoria() {
	return categoria;
}

public void setCategoria(Integer categoria) {
	this.categoria = categoria;
}

public String getDescricao() {
	return descricao;
}

public void setDescricao(String descricao) {
	this.descricao = descricao;
}

@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result
			+ ((categoria == null) ? 0 : categoria.hashCode());
	result = prime * result
			+ ((descricao == null) ? 0 : descricao.hashCode());
	return result;
}


@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	Categoria other = (Categoria) obj;
	if (categoria == null) {
		if (other.categoria != null)
			return false;
	} else if (!categoria.equals(other.categoria))
		return false;
	if (descricao == null) {
		if (other.descricao != null)
			return false;
	} else if (!descricao.equals(other.descricao))
		return false;
	return true;
}

}

CategoriaDAO.java

package com.livro.capitulo3.categoria;

import java.util.List;

import org.hibernate.Criteria;

import org.hibernate.HibernateException;

import org.hibernate.Session;

import org.hibernate.Transaction;

import org.hibernate.criterion.Restrictions;

import com.livro.capitulo3.conexao.ConectaDatabase;

public class CategoriaDAO {

private Session sessao;
private Transaction transaction;

public void salvar(Categoria categoria) {
	try{
		this.sessao = ConectaDatabase.getConexao().getCurrentSession();
		this.transaction = this.sessao.beginTransaction();
		this.sessao.save(categoria);
		this.transaction.commit();			
	}catch(HibernateException e){
		System.out.println("Erro ao salvar categoria: "+e.getMessage());
	}finally{
		try {
			if(this.sessao.isOpen()){
				this.sessao.close();
			}
		} catch (HibernateException e) {
			System.out.println("Erro ao fechar sessão, salvar categoria "+e.getMessage());
		}
	}		
}


public void excluir(Categoria categoria) {
	try{
		this.sessao = ConectaDatabase.getConexao().getCurrentSession();
		this.transaction = this.sessao.beginTransaction();
		this.sessao.delete(categoria);
		this.transaction.commit();			
	}catch(HibernateException e){
		System.out.println("Erro ao excluir categoria: "+e.getMessage());
	}finally{
		try {
			if(this.sessao.isOpen()){
				this.sessao.close();
			}
		} catch (HibernateException e) {
			System.out.println("Erro ao fechar sessão, excluir categoria "+e.getMessage());
		}
	}		
}
	

public void atualizar(Categoria categoria) {
	try{
		this.sessao = ConectaDatabase.getConexao().getCurrentSession();
		this.transaction = this.sessao.beginTransaction();
		this.sessao.update(categoria);
		this.transaction.commit();			
	}catch(HibernateException e){
		System.out.println("Erro ao atualizar categoria: "+e.getMessage());
	}finally{
		try {
			if(this.sessao.isOpen()){
				this.sessao.close();
			}
		} catch (HibernateException e) {
			System.out.println("Erro ao fechar sessão, atualizar categoria "+e.getMessage());
		}
	}		
}	


public Categoria buscarCategoria(Integer id) {
	Categoria categoria = null;
	try{
		this.sessao = ConectaDatabase.getConexao().getCurrentSession();
		this.transaction = this.sessao.beginTransaction();
		Criteria filtro = this.sessao.createCriteria(Categoria.class);
		filtro.add(Restrictions.eq("categoria", id));
		categoria = (Categoria) filtro.uniqueResult();
		this.transaction.commit();			
	}catch(HibernateException e){
		System.out.println("Erro ao busca por id categoria: "+e.getMessage());
		System.out.println("Fazendo o rollback");
		if(this.transaction.isActive()){
			this.transaction.rollback();
		}
	}finally{			
		try {
			if(this.sessao.isOpen()){
				this.sessao.close();
			}
		} catch (HibernateException e) {
			System.out.println("Erro ao fechar sessão, busca por id categoria "+e.getMessage());
		}
	}		
	return categoria;
}

@SuppressWarnings("unchecked")
public List<Categoria> listar() {
	List<Categoria> categorias = null;
	try{
		this.sessao = ConectaDatabase.getConexao().getCurrentSession();
		this.transaction = this.sessao.beginTransaction();
		Criteria filtro = this.sessao.createCriteria(Categoria.class);
		categorias = filtro.list();
		this.transaction.commit();			
	}catch(HibernateException e){
		System.out.println("Erro ao listar categoria: "+e.getMessage());
		System.out.println("Fazendo o rollback");
		if(this.transaction.isActive()){
			this.transaction.rollback();
		}
	}finally{			
		try {
			if(this.sessao.isOpen()){
				this.sessao.close();
			}
		} catch (HibernateException e) {
			System.out.println("Erro ao fechar sessão, listar categoria "+e.getMessage());
		}
	}		
	return categorias;
}

}

Criado 21 de setembro de 2017
Respostas 0
Participantes 1