HibernateException: hibernate.dialect 未设置,但我确实在 application.properties 中设置了它
HibernateException: hibernate.dialect not set, but I did set it in application.properties
我正在开发一个使用多个数据库的 Spring 启动应用程序。我定义了数据源,我定义了实体管理器,但我一直收到这个错误
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);
}
}
并且我使用了daos中的四个模板。奇迹般有效。
使用 JPA 时需要实体。使用 JPA 类 来执行 SQL 查询是多余的。只需使用普通的 Jdbctemplate
或 NamedParameterJdbcTemplate
即可。
我正在开发一个使用多个数据库的 Spring 启动应用程序。我定义了数据源,我定义了实体管理器,但我一直收到这个错误
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);
}
}
并且我使用了daos中的四个模板。奇迹般有效。
使用 JPA 时需要实体。使用 JPA 类 来执行 SQL 查询是多余的。只需使用普通的 Jdbctemplate
或 NamedParameterJdbcTemplate
即可。