java - Hibernate、MySQL 和名为 "Repeat"的表 - 奇怪的行为

标签 java mysql hibernate orm jpa

我有一个奇怪的问题。但首先是 orm.xml:

<entity class="de.test.businessobjects.Repeat">
    <table name="repeat"/>
    <attributes>
        <id name="id">
            <generated-value strategy="TABLE"/>
        </id>
        <many-to-one name="repeatType" fetch="LAZY">
            <join-column name="id_repeatType"/>
        </many-to-one>
        <many-to-one name="trainingSet" fetch="LAZY">
            <join-column name="id_trainingSet"/>
        </many-to-one>
    </attributes>
</entity>

我使用 Hibernate/JPA。 HSQL 和 Derby 一切正常,所以我的 BO、DAO 和单元测试一定没问题。使用 MySQL 进行测试时,出现此错误:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [select repeat0_.id as id8_, repeat0_.id_repeatType as id2_8_, repeat0_.id_trainingSet as id3_8_ from repeat repeat0_];

但是,改变

<table name="repeat"/>

<table name="repeatt"/>

解决了 MySQL 的问题。

怎么了? “repeat”是保留关键字还是 Hibernate 的 JPA 实现中的错误?

感谢和干杯 呃

最佳答案

SQL Reserved Words Checker告诉我"repeat"是 MySQL(和 DB2)的保留 SQL 关键字,因此您需要对它进行转义。

JPA 1.0 没有定义处理该问题的标准方法,因此您必须使用依赖反引号的 Hibernate 解决方案。来自 Hibernate 引用指南:

5.4. SQL quoted identifiers

You can force Hibernate to quote an identifier in the generated SQL by enclosing the table or column name in backticks in the mapping document. Hibernate will use the correct quotation style for the SQL Dialect. This is usually double quotes, but the SQL Server uses brackets and MySQL uses backticks.

<class name="LineItem" table="`Line Item`">
    <id name="id" column="`Item Id`"/><generator class="assigned"/></id>
    <property name="itemNumber" column="`Item #`"/>
    ...
</class>

假设这会在 orm.xml 中起作用也是。

JPA 2.0 更进一步,定义了一种指定分隔标识符的方法:

2.13 Naming of Database Objects

...

To specify delimited identifiers, one of the following approaches must be used:

  • It is possible to specify that all database identifiers in use for a persistence unit be treated as delimited identifiers by specifying the <delimited-identifiers/> element within the persistence-unit-defaults element of the object/relational xml mapping file. If the <delimited-identifiers/> element is specified, it cannot be overridden.
  • It is possible to specify on a per-name basis that a name for a database object is to be interpreted as a delimited identifier as follows:
    • Using annotations, a name is specified as a delimited identifier by enclosing the name within double quotes, whereby the inner quotes are escaped, e.g., @Table(name="\"customer\"").
    • When using XML, a name is specified as a delimited identifier by use of double quotes, e.g., <table name="&quot;customer&quot;"/>

如果您使用的是 JPA 2.0,我建议您使用可移植解决方案。

关于java - Hibernate、MySQL 和名为 "Repeat"的表 - 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3217479/

相关文章:

php - 具有日期范围的 MySQL 查询不返回任何结果

php - IN 查询搜索什么?

java - Spring登录表单示例

java - hibernate调用可以返回XML数据吗?

java - Intellij IDEA。隐藏 .iml 文件

java - 当尝试删除同一事务中的锁定实体时,EntityManager 抛出 OptimisticLockException

Mysql删除超过10秒的记录

java - Hibernate 中的反向继承映射

java - 如何使用 java 将参数传递给 shell 脚本?

java - 在函数中使用 FileWriter;文件应该已经存在