Configurar Hibernate de forma programática

1 resposta
M

Fala Galera blz!!!

Bom deixa eu tentar explicar, estou trabalhando com Flex + Java + Hibernate + Firebird.
Do Flex dependendo do Usuário selecionado no Login eu preciso conectar a um determinado Banco de Dados.
Acredito que isso eu já consegui pois estou passando o caminho do Banco para este Metdodo:

import hibernateUtil.HibernateUtil;

public class BancoDadosDAO {
	
	public void geraSessionFactory(String caminhoBanco) throws Exception
	{
		HibernateUtil.criaSessionFactory(caminhoBanco); 	
	}
	
	public void fechaSessao()
	{
		HibernateUtil.closeSession();
	}
}

Meu HibernateUtil

package hibernateUtil;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

	private static HibernateUtil hibernateUtil;

	private static SessionFactory sessionFactory;
	private static Session session;
	
	//constructor privado
	private HibernateUtil(String caminhoBanco) throws Exception {
		startSession(caminhoBanco);
	}

	public static HibernateUtil criaSessionFactory(String caminhoBanco) throws Exception {
    	    if (hibernateUtil == null) {
    		hibernateUtil = new HibernateUtil(caminhoBanco);
	    }
    	
	    return hibernateUtil;
	}
	
        private static void startSession(String caminhoBanco) throws Exception {
		
		if (sessionFactory == null) {
			try {

				Configuration cfg = new Configuration()						
						.setProperty("hibernate.dialect",
								"org.hibernate.dialect.FirebirdDialect")
						.setProperty("hibernate.connection.driver_class",
								"org.firebirdsql.jdbc.FBDriver")
						.setProperty(
								"hibernate.connection.url",
								"jdbc:firebirdsql:localhost/3050:E:/.../"+caminhoBanco)
						.setProperty("hibernate.connection.username", "...")
						.setProperty("hibernate.connection.password", "...")
						.addResource("com/ehweb/dadosEH/maps/Usuario.hbm.xml");

				sessionFactory = cfg.buildSessionFactory();

			} catch (Exception e) {
				sessionFactory = null;
				throw e;
			}
		}
	}

	public static Session getCurrentSession() {
		if (session == null || !session.isOpen() || !session.isConnected()) {
			session = sessionFactory.openSession();
		}	
		
		return session;
	}
			
	public static void closeSession() {
		session.close();
	}
}

Segue o que foi gerado após conectar com o banco passando o Caminho

9528 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : com/ehweb/dadosEH/maps/Usuario.hbm.xml
9543 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.HbmBinder - Mapping class: com.ehweb.dadosEH.entitys.Usuario -> USUARIO
9554 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.Configuration - Hibernate Validator not found: ignoring
9558 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.search.HibernateSearchEventListenerRegister - Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
9558 ["http-bio-8080"-exec-10] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
9558 ["http-bio-8080"-exec-10] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
9558 ["http-bio-8080"-exec-10] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
9558 ["http-bio-8080"-exec-10] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: org.firebirdsql.jdbc.FBDriver at URL: jdbc:firebirdsql:localhost/3050:E:/.../bds/Bdeh3/DadosEH3.FDB
9558 ["http-bio-8080"-exec-10] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=SYSDBA, password=****}
9569 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Database ->
       name : Firebird 2.0/WI-V2.0.3.12981 Firebird 2.0/tcp (USUARIO-PC)/P10
    version : WI-V2.0.3.12981 Firebird 2.0/WI-V2.0.3.12981 Firebird 2.0/tcp (USUARIO-PC)/P10
      major : 2
      minor : 0
9569 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Driver ->
       name : Jaybird JCA/JDBC driver
    version : 2.1
      major : 2
      minor : 1
9569 ["http-bio-8080"-exec-10] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.FirebirdDialect
9569 ["http-bio-8080"-exec-10] INFO org.hibernate.engine.jdbc.JdbcSupportLoader - Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
9569 ["http-bio-8080"-exec-10] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
9569 ["http-bio-8080"-exec-10] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
9569 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
9569 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
9569 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): disabled
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
9570 ["http-bio-8080"-exec-10] INFO org.hibernate.cfg.SettingsFactory - Check Nullability in Core (should be disabled when Bean Validation is on): enabled
9573 ["http-bio-8080"-exec-10] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
9573 ["http-bio-8080"-exec-10] INFO org.hibernate.type.BasicTypeRegistry - Type registration [materialized_clob] overrides previous : org.hibernate.type.MaterializedClobType@c550
9573 ["http-bio-8080"-exec-10] INFO org.hibernate.type.BasicTypeRegistry - Type registration [blob] overrides previous : org.hibernate.type.BlobType@9a18a0
9573 ["http-bio-8080"-exec-10] INFO org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Blob] overrides previous : org.hibernate.type.BlobType@9a18a0
9573 ["http-bio-8080"-exec-10] INFO org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_characters_clob] overrides previous : org.hibernate.type.CharacterArrayClobType@c6e1ec
9573 ["http-bio-8080"-exec-10] INFO org.hibernate.type.BasicTypeRegistry - Type registration [clob] overrides previous : org.hibernate.type.ClobType@11a5fd0
9573 ["http-bio-8080"-exec-10] INFO org.hibernate.type.BasicTypeRegistry - Type registration [java.sql.Clob] overrides previous : org.hibernate.type.ClobType@11a5fd0
9573 ["http-bio-8080"-exec-10] INFO org.hibernate.type.BasicTypeRegistry - Type registration [characters_clob] overrides previous : org.hibernate.type.PrimitiveCharacterArrayClobType@8890da
9574 ["http-bio-8080"-exec-10] INFO org.hibernate.type.BasicTypeRegistry - Type registration [materialized_blob] overrides previous : org.hibernate.type.MaterializedBlobType@1de007d
9574 ["http-bio-8080"-exec-10] INFO org.hibernate.type.BasicTypeRegistry - Type registration [wrapper_materialized_blob] overrides previous : org.hibernate.type.WrappedMaterializedBlobType@1f243d1
9579 ["http-bio-8080"-exec-10] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured

O meu problema começa agora.
Após passar o caminho do Banco e criar a sessionFactory, preciso chamar um outro método para validar o Usuario.
Segue o UsuarioDAO

import hibernateUtil.HibernateUtil;

import org.hibernate.Query;
import org.hibernate.Session;

import com.ehweb.dadosEH.entitys.Usuario;
import com.ehweb.dadosEH.interfaces.IUsuario;


public class UsuarioDAO implements IUsuario {
	private Session sessaoGeral = HibernateUtil.getCurrentSession();
	
	@Override
	public Usuario verificaUsuario(Usuario usuario) throws Exception {
		Query select = sessaoGeral.createQuery("FROM Usuario WHERE nome =:nome AND senha =:senha");
		
		select.setString("nome", usuario.getNome());
		select.setString("senha", usuario.getSenha());

		HibernateUtil.closeSession();
		return (Usuario) select.uniqueResult();
	}
}

Após isso o Flex não consegue invocar esse método verificaUsuario acredito que o problema é em algo que eu fiz no Hibernate.
Alguém poderia me ajudar?

Desde já agradeço muito

1 Resposta

M

ninguém!!!

Criado 23 de março de 2011
Ultima resposta 24 de mar. de 2011
Respostas 1
Participantes 1