我在春季启动时还很陌生,我正在尝试通过JPA验证来设置多个数据源,但出现以下错误:
Unable to build DatabaseInformation
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.tool.schema.internal.Helper.buildDatabaseInformation(Helper.java:140) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.tool.schema.internal.AbstractSchemaValidator.doValidation(AbstractSchemaValidator.java:61) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:191) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:310) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:467) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:939) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) ~[spring-orm-5.1.5.RELEASE.jar:5.1.5.RELEASE]
... 20 common frames omitted
Caused by: java.sql.SQLSyntaxErrorException: Table 'concentrador.pg_class' doesn't exist
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1218) ~[mysql-connector-java-8.0.15.jar:8.0.15]
at com.zaxxer.hikari.pool.ProxyStatement.executeQuery(ProxyStatement.java:111) ~[HikariCP-3.2.0.jar:na]
at com.zaxxer.hikari.pool.HikariProxyStatement.executeQuery(HikariProxyStatement.java) ~[HikariCP-3.2.0.jar:na]
at org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorLegacyImpl.extractMetadata(SequenceInformationExtractorLegacyImpl.java:42) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl.initializeSequences(DatabaseInformationImpl.java:65) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl.<init>(DatabaseInformationImpl.java:59) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.tool.schema.internal.Helper.buildDatabaseInformation(Helper.java:132) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
... 29 common frames omitted
Process finished with exit code 1
这是我的application.properties
文件
spring.datasource.url = jdbc:postgresql://<ip>:<port>/<database>
spring.datasource.driver-class-name = org.postgresql.Driver
spring.datasource.username = <username>
spring.datasource.password = <password>
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = validate
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
flyway.local.locations.classpath = resources/db/migration/
concentrador.datasource.jdbc-url = jdbc:mysql://<ip>:<port>/<database>?useSSL=false
concentrador.datasource.driver-class-name=com.mysql.jdbc.Driver
concentrador.datasource.username = <user>
concentrador.datasource.password = <password>
concentrador.jpa.properties.hibernate.temp.use-jdbc-metadata-defaults = false
concentrador.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
concentrador.datasource.testWhileIdle = true
concentrador.datasource.validation-query = SELECT 1
concentrador.jpa.show-sql = true
concentrador.jpa.hibernate.ddl-auto = validate
concentrador.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
concentrador.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
#OTHER SETTINGS
这是我的第一个数据源配置:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
basePackages = {"com.my.package.datasource"}
)
public class DataSourceLocalConfig {
@Value("${spring.datasource.validation-query}")
private String validationQuery;
@Primary
@Bean(name = "dataSource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource(DataSourceProperties dataSourceProperties) {
return DataSourceBuilder.create(dataSourceProperties.getClassLoader())
.type(HikariDataSource.class)
.driverClassName(dataSourceProperties.getDriverClassName())
.url(dataSourceProperties.getUrl())
.username(dataSourceProperties.getUsername())
.password(dataSourceProperties.getPassword())
.build();
}
@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSource") DataSource dataSource
) {
return builder
.dataSource(dataSource)
.packages("com.my.package.datasource.domain")
.persistenceUnit("datasource")
.properties(singletonMap("hibernate.hbm2ddl.auto", "validate"))
.build();
}
@Primary
@Bean(name = "flyway")
@ConfigurationProperties(prefix = "flyway.local")
public Flyway flyway(@Qualifier("dataSource") DataSource dataSource) {
Flyway flyway = Flyway.configure().dataSource(dataSource).load();
flyway.migrate();
return flyway;
}
@Primary
@Bean(name = "transactionManager")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactory") EntityManagerFactory
entityManagerFactory
) {
return new JpaTransactionManager(entityManagerFactory);
}
}
这是我的第二个数据源配置:
@Configuration
@EnableTransactionManagement
@EnableConfigurationProperties(value = DataSourceProperties.class)
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryConcentrador",
transactionManagerRef = "transactionManagerConcentrador",
basePackages = {"com.my.package.concentrador"})
public class DataSourceConcentradorConfig {
@Value("${concentrador.jpa.properties.hibernate.dialect}")
private String dialect;
@Value("${concentrador.jpa.hibernate.naming-strategy}")
private String naming;
@Bean(name = "entityManagerFactoryConcentrador")
@ConfigurationProperties(prefix = "concentrador.jpa")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("dataSourceConcentrador") DataSource dataSource) {
return builder.dataSource(dataSource).packages("com.my.package.concentrador.domain")
.persistenceUnit("concentrador")
.properties(singletonMap("hibernate.hbm2ddl.auto", "validate"))
.properties(singletonMap("concentrador.jpa.properties.hibernate.dialect", dialect))
.properties(singletonMap("concentrador.jpa.hibernate.naming-strategy", naming))
.build();
}
@Bean(name = "dataSourceConcentrador")
@ConfigurationProperties(prefix = "concentrador.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
private Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
properties.setProperty("hibernate.ddl-auto","none");
return properties;
}
@Bean(name = "transactionManagerConcentrador")
public PlatformTransactionManager transactionManager(
@Qualifier("entityManagerFactoryConcentrador") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
这是我的主要应用程序
@SpringBootApplication()
@EntityScan(basePackageClasses = {PdvMovelApplication.class, Jsr310JpaConverters.class})
@ComponentScan({"com.my.package", "com.my.package.config", "com.my.package"})
public class MyApplicationApplication {
public static void main(String[] args) {
SpringApplication.run(PdvMovelApplication.class, args);
}
}
我还将flyway用作主要数据源。 谢谢!