![](/img/trans.png)
[英]H2 - Oracle - liquibase - org.h2.jdbc.JdbcSQLException: Table "all_sequences" not found;
[英]org.h2.jdbc.JdbcSQLException: Table “ALL_SEQUENCES” not found
有人可以告訴我以下錯誤的原因。
我在我的項目中使用 Hibernate 並在服務器啟動期間遇到以下錯誤
15:04:27.909 [localhost-startStop-1] ERROR o.h.tool.hbm2ddl.SchemaValidator - HHH000319: Could not get database metadata
org.h2.jdbc.JdbcSQLException: Table "ALL_SEQUENCES" not found; SQL statement:
select sequence_name from all_sequences union select synonym_name from all_synonyms us, all_sequences asq where asq.sequence_name = us.table_name and asq.sequence_owner = us.table_owner [42102-168]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329) ~[h2-1.3.168.jar:1.3.168]
at org.h2.message.DbException.get(DbException.java:169) ~[h2-1.3.168.jar:1.3.168]
at org.h2.message.DbException.get(DbException.java:146) ~[h2-1.3.168.jar:1.3.168]
at org.h2.command.Parser.readTableOrView(Parser.java:4770) ~[h2-1.3.168.jar:1.3.168]
at org.h2.command.Parser.readTableFilter(Parser.java:1084) ~[h2-1.3.168.jar:1.3.168]
at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1690) ~[h2-1.3.168.jar:1.3.168]
這發生在您使用在一個錯誤的話persistence-unit
的內部persistence.xml
,或您驗證對錯誤的數據庫。 例如,當您針對本地 H2 數據庫運行應用程序時,最好的選擇是刪除方言,因為 Hibernate 可以識別沒有此屬性的數據庫(如果 Hibernate 的版本足夠新以識別較新的數據庫)。 另一種解決方案是刪除驗證屬性,但我不建議這樣做,因為您在啟動時沒有數據庫檢查:
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
我使用了多個數據源
OracleDb1Configuration
@Primary
@Bean(name = "oracleDb1EntityManager")
public LocalContainerEntityManagerFactoryBean oracleDb1EntityManagerFactory(EntityManagerFactoryBuilder builder) {
return builder
.dataSource(oracleDb1DataSource)
.properties(hibernateProperties())
.packages("com.fengxin58.user.ddd.domain.model.oracle.db1")//設置實體類所在位置
.persistenceUnit("oracleDb1PersistenceUnit")
.build();
}
private Map<String, Object> hibernateProperties() {
String env = monitorService.env();
if(log.isDebugEnabled()) {
log.debug("current profile: {}", env);
}
Resource resource = null;
if(EnvEnum.TEST.key().equals(env)) {
resource = new ClassPathResource("hibernate-oracle-db1-test.properties");
}else {
resource = new ClassPathResource("hibernate-oracle-db1.properties");
}
try {
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
return properties.entrySet().stream()
.collect(Collectors.toMap(
e -> e.getKey().toString(),
e -> e.getValue())
);
} catch (IOException e) {
return new HashMap<String, Object>();
}
}
hibernate-oracle-db1-test.properties
hibernate.hbm2ddl.auto=更新
application-test.yml
oracle: db1: datasource: url: jdbc:h2:mem:test driver-class-name: org.h2.Driver username: root password: db2: datasource: url: jdbc:h2:mem:test driver-class-name: org.h2.Driver username: root password:
您必須確保已創建表和序列。 如果它被創建,那么它將起作用。
未創建表"ALL_SEQUENCES"
。 請檢查您的數據庫是否存在?
您的問題是未創建序列以及未創建表。
檢查您的 hibernate.cfg.xml。 它沒有配置好。 為了您的澄清,我在下面給出了一個cfg 文件:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:database/test</property>
<property name="connection.username">sa</property>
<property name="connection.password"/>
<property name="hibernate.default_schema">PUBLIC</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="au.com.ozblog.hibernate.h2.example.User"/>
</session-factory>
</hibernate-configuration>
就我而言,我在我的應用程序中使用了多個數據源,並使用 H2 進行測試。 在這種情況下,您必須在數據源的 JPA 屬性中手動設置方言,而不是通過 application.properties 文件。
Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
return builder
.dataSource(ds)
.packages("com.application.demo")
.properties(properties)
.persistenceUnit("persitenceUnit1")
.build();
在使用的每個數據源中。
萬一它可以幫助某人。
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.