GUJ Discussões   :   últimos tópicos   |   categorias   |   GUJ Respostas

Erro com dialect resolver

java
hibernate
jpa
Tags: #<Tag:0x00007f75fb5e2608> #<Tag:0x00007f75fb5e24a0> #<Tag:0x00007f75fb5e2298>

#1

Fala galera, tenho uma aplicação fazendo a conexão com o banco usando JPA. O problema é que o meu persistence.xml por algum motivo não reconhece o dialeto do SQLServer mas reconhece o Postgree, então criei um resolver baseado nesse artigo:

http://blog.exxeta.com/java/2016/03/23/dynamically-resolve-hibernate-database-dialect/

que é um modo de reconhecimento do dialeto que eu to usando e ele seta pra mim, mas quando eu insiro o resolver ele não encontra nenhum e da um erro de exception.

Meu persistence.xml:

<property name="hibernate.dialect" value="com.mimimi.MyDialectResolver" />

Meu DialectResolver:

ipackage br.com.govbr.logos.resolver;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.dialect.DB2Dialect;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.H2Dialect;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.MySQL5Dialect;
import org.hibernate.dialect.Oracle10gDialect;
import org.hibernate.dialect.SQLServer2008Dialect;
import org.hibernate.dialect.SQLServer2012Dialect;
import org.hibernate.dialect.PostgreSQL9Dialect;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;

public class MyDialectResolver implements DialectResolver {

private static final long serialVersionUID = 1453501504388873542L;

private static Map<String, Class<? extends Dialect>> DIALECT_BY_NAME = new HashMap<String, Class<? extends Dialect>>();

@Override
public Dialect resolveDialect(DialectResolutionInfo info) {
	initializeDialects();
	String databaseName = info.getDatabaseName();
	Dialect dbDialect = lookupDialect(databaseName);
	
	return dbDialect;
}

private Dialect lookupDialect(String databaseName) {
	Class<? extends Dialect> dialectClass = DIALECT_BY_NAME.get(databaseName);
	if (dialectClass == null) {
		return null;
	}

	try {
		return dialectClass.newInstance();

	} catch (InstantiationException | IllegalAccessException e) {
		// log something
		return null;
	}
	
}

private void initializeDialects() {
	 registerDialect(H2Dialect.class, "H2");
	 registerDialect(MySQL5Dialect.class, "MySQL");
	 registerDialect(SQLServer2012Dialect.class, "Microsoft SQL Server 2012");
	 registerDialect(SQLServer2008Dialect.class, "Microsoft SQL Server");
	 registerDialect(HSQLDialect.class, "HSQL Database Engine");
	 registerDialect(DB2Dialect.class, "DB2", "DB2/NT64", "DB2/LINUXX8664");
	 registerDialect(Oracle10gDialect.class, "Oracle10g");
	 registerDialect(PostgreSQL9Dialect.class, "PostgreSQL9");

}

private static void registerDialect(Class<? extends Dialect> dialect, String... databaseNames) {
	for (String databaseName : databaseNames) {
		DIALECT_BY_NAME.put(databaseName, dialect);
	}
}

}

O erro:

    ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 59) MSC000001: Failed to start service jboss.persistenceunit."logos-ear.ear/logos-dom-1.0-SNAPSHOT.jar#logosPU": org.jboss.msc.service.StartException in service jboss.persistenceunit."logos-ear.ear/logos-dom-1.0-SNAPSHOT.jar#logosPU": org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
	at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:179)
	at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:121)
	at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:667)
	at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:193)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
	at org.jboss.threads.JBossThread.run(JBossThread.java:320)
    Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
    	at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:264)
    	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:228)
    	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207)
    	at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
    	at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
    	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237)
    	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207)
    	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
    	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
    	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847)
    	at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874)
    	at org.jboss.as.jpa.hibernate5.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
    	at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:161)
    	... 7 more
    Caused by: org.hibernate.HibernateException: Unable to construct requested dialect [br.com.govbr.logos.resolver.MyDialectResolver]
    	at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:84)
    	at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:51)
    	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137)
    	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
    	at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
    	at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:254)
    	... 19 more
    Caused by: java.lang.ClassCastException: br.com.govbr.logos.resolver.MyDialectResolver cannot be cast to org.hibernate.dialect.Dialect
    	at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:74)
    	... 24 more

#2

Não sei muito de Hibernate, mas de acordo com artigo, deveria ser:

<property name="hibernate.dialect_resolvers" value="br.com.govbr.logos.resolver.MyDialectResolver" />

#3

perdão, mandei errado. O meu já está assim.


#4
Caused by: java.lang.ClassCastException: br.com.govbr.logos.resolver.MyDialectResolver cannot be cast to org.hibernate.dialect.Dialect

Tem certeza que está dando o mesmo erro? Pois nessa linha está dizendo que o MyDialectResolver não é Dialect.