org.h2.jdbc.JdbcSQLException:找不到表“ALL_SEQUENCES”

时间:2016-08-03 13:19:01

标签: java hibernate jdbc

有人可以告诉我以下错误的原因。

我在我的项目中使用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]

4 个答案:

答案 0 :(得分:7)

当您在persistence-unit内的persistence.xml中使用错误的方言,或者对错误的数据库进行验证时,会发生这种情况。例如,当您针对本地H2数据库运行应用程序时,最好的选择是删除方言,因为Hibernate可以识别没有此属性的数据库(如果Hibernate的版本足以识别新的数据库)。另一个解决方案是删除validate属性,但我不建议这样做,因为你在启动时没有数据库检查:

<properties>
    <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
    <property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>

答案 1 :(得分:1)

我使用了多个dataSources

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:

答案 2 :(得分:0)

您必须确保创建表和序列。如果它被创建,那么它将起作用。

未创建表"ALL_SEQUENCES"。请检查您的数据库是否存在?

您的问题是没有创建序列,也没有创建表。

Solution:

检查你的hibernate.cfg.xml。配置不好。 为了您的澄清,我在下面给了cfg file

<?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>

答案 3 :(得分:0)

就我而言,我在应用程序中使用多个数据源,并使用H2进行测试。 在这种情况下,您必须在数据源的JPA属性中手动设置方言,而不是通过application.properties文件。

var promises = [obj1, obj2,objN].map(function(obj){
  return database.query('id').then(function(results){
     return results;
  })
})
Promise.all(promises).then(function(results) {
    console.log("Result",results);
})

在使用的每个数据源中。

以防万一。

致谢