简体   繁体   English

Spring 启动 H2 数据库模式 oracle

[英]Spring boot H2 db mode oracle

Hi I have a spring boot app (2.3.1.RELEASE, ojdbc8) and basically it is connected to an oracle database.嗨,我有一个 spring 引导应用程序(2.3.1.RELEASE,ojdbc8),基本上它连接到 oracle 数据库。

The spring boot app starts find when it is connected to the oracle db.However fails to start when the integration test is connected to a H2 embedded db and some of my queries are failing with the following error message: spring 启动应用程序在连接到 oracle 数据库时开始查找。但是当集成测试连接到 H2 嵌入式数据库时无法启动,并且我的一些查询失败并显示以下错误消息:

Caused by: java.lang.NullPointerException: null
    at org.hibernate.hql.internal.NameGenerator.generateColumnNames(NameGenerator.java:27)
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.generateColumnNames(SessionFactoryHelper.java:434)

I manage to make it work by adding adding the following line of code in my application-test.yml file:我设法通过在我的 application-test.yml 文件中添加以下代码行来使其工作:

spring:
  jpa:
    database-platform: org.hibernate.dialect.Oracle10gDialect

Please fine below application-test.yml config:请在 application-test.yml 配置下罚款:

spring.datasource:
  url: jdbc:h2:mem:db;Mode=Oracle;DB_CLOSE_DELAY=-1
  username: sa
  password: sa
  driverClassName: org.h2.Driver
# added empty context path to override application.yml context path
server:
  servlet:
    contextPath:
spring:
  jpa:
    database-platform: org.hibernate.dialect.Oracle10gDialect

The application starts well and integration completed successfully however the following exception can be shown in the log:应用程序启动良好,集成成功完成,但日志中会显示以下异常:

org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "ALL_SEQUENCES" not found; SQL statement:
select * from all_sequences [42102-200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:453) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:429) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.get(DbException.java:205) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.get(DbException.java:181) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.command.Parser.readTableOrView(Parser.java:7628) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.command.Parser.readTableFilter(Parser.java:1970) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.command.Parser.parseSelectFromPart(Parser.java:2827) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.command.Parser.parseSelect(Parser.java:2959) ~[h2-1.4.200.jar:1.4.200]
    
    
    ....
    
    org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "drop table dash_processed cascade constraints" via JDBC Statement
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:375) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlStrings(SchemaDropperImpl.java:359) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.dropFromMetadata(SchemaDropperImpl.java:241) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.performDrop(SchemaDropperImpl.java:154) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:126) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:112) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:145) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
    
    ...
    
    drop table dash_processed cascade constraints [42102-200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:453) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:429) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.get(DbException.java:205) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.message.DbException.get(DbException.java:181) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.command.ddl.DropTable.prepareDrop(DropTable.java:65) ~[h2-1.4.200.jar:1.4.200]
    at org.h2.command.ddl.DropTable.update(DropTable.java:124) ~[h2-1.4.200.jar:1.4.200]

Any idea how i can stop those errors?知道如何阻止这些错误吗?

Thanks in advance提前致谢

To simulate the Oracle and sequence objects with h2 database add this line to your spring boot application.properties:要使用 h2 数据库模拟 Oracle 和序列对象,请将此行添加到您的 spring 引导 application.properties:

spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle

h2 doc Oracle Compatibility Mode section h2 doc Oracle 兼容模式部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM