我正在使用一个使用许多数据库的Spring Boot应用程序。我定义数据源,定义实体管理器,并且不断收到此错误
org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100)
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54)
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:94)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
这是一个数据库的@Configuration
文件:
@Configuration
public class CustomerDbConfig {
@Bean
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "customerEntityManager")
@PersistenceContext(unitName = "customerEntityManager")
public LocalContainerEntityManagerFactoryBean customerEntityManagerFactory(
EntityManagerFactoryBuilder builder, DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.example.report_service").build();
}
@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(emf);
return jpaTransactionManager;
}
@Bean
public EntityManagerFactoryBuilder entityManagerFactoryBuilder() {
return new EntityManagerFactoryBuilder(new HibernateJpaVendorAdapter(), new HashMap<>(), null);
}
}
这些是application.properties
spring.datasource.url=jdbc:postgresql://dv1.example.com:5432/customer
spring.datasource.username=${DATABASECUSTOMERUSERNAME:customer}
spring.datasource.password=${DATABASECUSTOMERPASSWORD:customer}
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.hikari.minimum-idle=2
spring.datasource.hikari.maximum-pool-size=${MAX_POOL_SIZE:20}
spring.datasource.tomcat.validation-query=SELECT 1
spring.datasource.tomcat.validation-query-timeout=5000
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.test-on-connect=true
spring.transaction.default-timeout=${TRANSACTION_TIMEOUT:3600}
spring.jpa.properties.hibernate.proc.param_null_passing=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jpa.open-in-view=false
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.type=trace
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
当我在entitymanager中显式设置persistenceUnit名称时,错误发生了变化,它抱怨找不到用我指定的名称的PU。
我将我的应用程序限制为只有一个数据库,试图找出正在发生的事情。当我删除配置文件时,没有任何问题。该应用程序之所以与众不同,是因为它仅使用本机查询,没有@Entity类,因为数据库是由其他应用程序更新的。当我从代码中删除.packages(..)部分时,它会抱怨
No persistence units parsed from {classpath*:META-INF/persistence.xml}
我怀疑该问题与以下事实有关:我们没有需要扫描的类。
编辑: 按照M.Deinum的回答,我将配置替换为
@Configuration
@EnableTransactionManagement
public class CustomerDbConfig {
@ConfigurationProperties(prefix = "customer.datasource")
@Bean(name = "customer-datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "customer-jdbcTemplate")
public JdbcTemplate primaryJdbcTemplate(@Qualifier("customer-datasource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
,我在dao中使用了四个模板。奇迹般有效。
答案 0 :(得分:1)
使用JPA时,您需要实体。使用JPA类仅执行SQL查询是多余的。只需使用普通的Jdbctemplate
或NamedParameterJdbcTemplate
即可。