当前位置: 首页 > 知识库问答 >
问题:

Spring Boot JPA-当“hibernate”时,对方言解析信息的访问不能为空。方言“不适用于JAVA 8u291

赫连俊悟
2023-03-14

我在我的本地笔记本电脑上运行这个,它似乎工作正常,但是每次我试图在不同的服务器上运行时,都会出现以下错误。(均使用Java8u291)

共有2个答案

于嘉许
2023-03-14

错误“访问方言解析信息不能为空”似乎非常普遍。在我的例子中,原因是试图在同一个Spring Boot API中连接到多个数据库。

在我的情况下,解决它,为Spring开机2.5。Baeldung的这个例子很有帮助,这也是中等规模的例子。

这是申请表。为MySQL db指定方言的yml文件:

spring:
  datasource:
    primaryDB:
      driverClassName: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:<port>/<primary db name>?charSet=LATIN1
      username: <username>
      password: <password>
      initialization-mode: always
    otherDB:
      driverClassName: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:<port>/<other db name>?charSet=LATIN1
      username: <username>
      password: <password>
      initialization-mode: always
  jpa:
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
  properties:
    hibernate:
      dialect: org.hibernate.dialect.MySQL5InnoDBDialect

对于主数据库的@Configuration类:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "entityManagerFactory",
        basePackages = { "com.foo.bar.repository" }
)
public class PrimaryDBConfig {

    @Value("${spring.datasource.primaryDB.driverClassName}")
    private String driver;

    @Value("${spring.datasource.primaryDB.url}")
    private String url;

    @Value("${spring.datasource.primaryDB.username}")
    private String username;

    @Value("${spring.datasource.primaryDB.password}")
    private String password;

    @Primary // this seemingly redundant datasource is apparently used behind
        // ... the scenes and throws an 'unsatisfied dependency' if removed.
    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primaryDB")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public DataSource dataSourcePrimary(){
        HikariConfig config = new HikariConfig();
        config.setDriverClassName(driver);
        config.setJdbcUrl(url);
        config.setUsername(username);
        config.setPassword(password);
        config.setAutoCommit(true);
        return new HikariDataSource(config);
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean
    entityManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier("dataSource") DataSource dataSource) {
        return builder
                .dataSource(dataSourcePrimary())
                .packages("com.foo.bar.model")
                .persistenceUnit("primaryDB")
                .build();
    }

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(
            @Qualifier("entityManagerFactory") EntityManagerFactory
                    entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}

和“其他数据库”:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "otherDBEntityManagerFactory",
        transactionManagerRef = "otherDBTransactionManager",
        basePackages = { "com.foo.bar.otherDB" }
)
public class OtherDBConfig {

    @Value("${spring.datasource.otherDB.driverClassName}")
    private String driver;

    @Value("${spring.datasource.otherDB.url}")
    private String url;

    @Value("${spring.datasource.otherDB.username}")
    private String username;

    @Value("${spring.datasource.otherDB.password}")
    private String password;

    @Bean(name = "otherDBDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.otherDB")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public DataSource dataSourceOtherDB(){
        HikariConfig config = new HikariConfig();
        config.setDriverClassName(driver);
        config.setJdbcUrl(url);
        config.setUsername(username);
        config.setPassword(password);
        config.setAutoCommit(true);
        return new HikariDataSource(config);
    }

    @Bean(name = "otherDBEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean otherDBEntityManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier("otherDBDataSource") DataSource dataSource) {
        return
                builder
                        .dataSource(dataSourceOtherDB())
                        .packages("com.foo.bar.otherDB.model")
                        .persistenceUnit("otherDB")
                        .build();
    }

    @Bean(name = "otherDBTransactionManager")
    public PlatformTransactionManager otherDBTransactionManager(
            @Qualifier("otherDBEntityManagerFactory") EntityManagerFactory
                    otherDBEntityManagerFactory) {
        return new JpaTransactionManager(otherDBEntityManagerFactory);
    }
}

包结构如下所示:

当然,拥有多个数据源是现代微服务API的反模式;因此,该解决方案基本上适用于整体(和分布式整体):-)

尤夕
2023-03-14

你错过申请了吗。不同服务器上的属性?从跟踪:

Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set at 

同时检查:关于无方言的回答可能是其他服务器无法连接到DB。

 类似资料: