JPA/Hibernate无法创建名为Order的实体 [英] JPA/Hibernate can't create Entity called Order

查看:96
本文介绍了JPA/Hibernate无法创建名为Order的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Hibernate/JPA并有一个名为Order的@Entity对象,它使用Hibernate的动态表生成(即在运行时为实体生成表)指向MySQL数据库.当Hibernate创建表时,它将为除Order实体之外的所有实体创建表.如果我将Order实体重命名为其他名称,例如StoreOrder,创建store_order表没有问题.

I'm using Hibernate/JPA and have an @Entity object called Order, pointing at a MySQL database using Hibernate's dynamic table generation i.e. generate tables at runtime for entities. When Hibernate creates the tables, it creates tables for all of my entities except the Order entity. If I rename the Order entity to something else e.g. StoreOrder, the store_order table is created no problem.

此外,如果我执行此操作,然后注释StoreOrder对象以指定使用@Table(名称="order")将其应创建的表称为"order",则将不再创建该表.

Furthermore, if I do this but then annotate the StoreOrder object to specify that the table it should create is called 'order' using @Table (name="order"), the table is no longer created.

我意识到订单是保留字,但这是无法创建表的原因吗?鉴于休眠docs 使用了一个称为Order的实体的示例.

I realise that order is a reserved word but is this the reason that it can't create the table? It seems odd given that the Hibernate docs uses an example of an entity called Order.

这看起来不像是MySQL的问题,因为我可以直接在数据库上手动创建一个名为order的表.

It doesn't look like a MySQL problem as I can manually create a table called order directly on the database.

这是相关的Order实体对象定义...

Here's the relevant Order entity object definition...

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Order extends PersistentEntity {
... rest of POJO def...
}

...以及相应的Persistence.xml

...and the corresponding Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

    <persistence-unit name="store" transaction-type="RESOURCE_LOCAL">

        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <mapping-file>conf/jpa/persistence-query.xml</mapping-file>

        <!-- Entities -->
        ...other entities...
        <class>ie.wtp.store.model.Order</class>
        <class>ie.wtp.store.model.OrderItem</class>

        <!-- Hibernate properties -->
        <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/store"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value=""/>

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>

            <property name="hibernate.query.factory_class"
                      value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory"/>
            <property name="hibernate.query.substitutions" value="true 1, false 0"/>
            <property name="cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
            <property name="hibernate.max_fetch_depth" value="3"/>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>


    </persistence-unit>
</persistence>

是否有任何关于为什么未创建该Order实体但将其重命名为StoreOrder的情况?

Any thoughts as to why this Order entity is not created but is created if I rename it to StoreOrder?

干杯

J:)

推荐答案

ORDER单词是保留关键字,您必须对其进行转义.

The ORDER word is a reserved keyword, you have to escape it.

在JPA 1.0中,没有标准化的方法,而Hibernate特定的解决方案是使用反引号:

In JPA 1.0, there is no standardized way and the Hibernate specific solution is to use backticks:

@Entity
@Table(name="`Order`")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Order extends PersistentEntity {
... rest of POJO def...
}

JPA 2.0对此进行了标准化,其语法如下所示:

JPA 2.0 standardized this and the syntax looks like this:

@Entity
@Table(name="\"Order\"")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Order extends PersistentEntity {
... rest of POJO def...
}

参考文献

  • Hibernate Core文档
    • 5.4. SQL带引号的标识符
    • References

      • Hibernate Core documentation
        • 5.4. SQL quoted identifiers
          • 2.13数据库对象的命名

          这篇关于JPA/Hibernate无法创建名为Order的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆