【问题标题】:Exceptions after updating from H2 in memory db version 1.4.X to 2.1.x从内存数据库版本 1.4.X 中的 H2 更新到 2.1.x 后的异常
【发布时间】:2022-01-22 09:32:47
【问题描述】:

我使用 H2 内存数据库进行集成测试,而不是使用 Oracle 进行实时系统。 在我更新到新的主要 H2 版本 2 后,测试会抛出如下异常:

SQL 语句中的语法错误“select test0_.ID as id1_1_3_, test0_.TEST_ID as test_2_1_3_, test0_.PU_ID as pu8_1_3_, testpar1_.TEST_ID as test_4_2_5_, testpar1_.ID as id1_2_5_, testpar1_.ID as id1_2_0_, testpar1_.TEST_ID as test_4_2_0_, testpar1_.NAME as name2_2_0_, testpar1_.[*]VALUE as value3_2_0_, from testschema.TEST test0_ left outer join testschema.PU pu2_ on test0_.PU_ID=pu2_.ID where test0_.ID=?";预期的“标识符”;

这是一个示例实体:

@Entity
@Table(name = "TEST")
@SequenceGenerator(name = "TEST_SEQUENCE_GENERATOR",
                   sequenceName = "TEST_SEQ",
                   allocationSize = 1)
public class Test  {

    @Id
    @Column(name = "ID")
    @GeneratedValue(generator = "TEST_SEQUENCE_GENERATOR", strategy = GenerationType.SEQUENCE)
    private Long id;

这些是设置属性:

 properties.put("javax.persistence.jdbc.driver", "org.h2.Driver");
        properties.put(
                "javax.persistence.jdbc.url",
                "jdbc:h2:mem:testschema;DB_CLOSE_ON_EXIT=FALSE;INIT=CREATE SCHEMA IF NOT EXISTS TESTSCHEMA");
        properties.put("javax.persistence.jdbc.user", "testschema");
        properties.put("javax.persistence.jdbc.password", "");
        properties.put("hibernate.default_schema", "testschema");
        properties.put("hibernate.show_sql", "false");
        properties.put(
                "hibernate.cache.region.factory_class",
                "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
        properties.put("hibernate.hbm2ddl.auto", "create-drop");
        properties.put("hibernate.order_by.default_null_ordering", "last");
        properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");

我查看了H2网页上的迁移指南,但找不到失败。

【问题讨论】:

  • 使用 Hibernate ORM 5.x(由于H2Dialect 的错误)通常需要将;MODE=LEGACY 添加到 H2 2.x 的 JDBC URL,但您可能还需要其他设置。请在您的问题中包含完整的例外,因为实际的错误位置隐藏在 ... 后面。
  • 感谢您的快速评论,我测试了 MODE=LEGACY 但您说得对,这还不够。

标签: hibernate h2


【解决方案1】:

VALUE 是 H2 中的关键字,是 SQL 标准中的保留字(即使在古老的 SQL-92 中也是如此)。用作标识符时,需要引用为"VALUE"。 Hibernate ORM 有一个hibernate.globally_quoted_identifiers 设置,可以设置为true 来引用所有标识符,你可以启用它。

如果出于某种原因不想全部引用,可以在 H2 的 JDBC URL 中添加 ;NON_KEYWORDS=VALUE,但此设置可能不适用于所有情况。

对于 Hibernate ORM 5.6.4 和更早的版本,您还需要 ;MODE=LEGACY,因为其中的 H2Dialect 会生成默认情况下被 H2 2.x 拒绝的无效 SQL。它已在 5.6.5 中修复。

【讨论】:

    猜你喜欢
    • 2022-07-22
    • 2021-03-03
    • 2012-04-30
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 2012-06-12
    相关资源
    最近更新 更多