JPA / Hibernate无法创建名为Order的Entity

时间:2022-09-11 11:49:40

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.

我正在使用Hibernate / JPA并且有一个名为Order的@Entity对象,使用Hibernate的动态表生成指向MySQL数据库,即在运行时为实体生成表。当Hibernate创建表时,它会为除Order实体之外的所有实体创建表。如果我将Order实体重命名为其他内容,例如StoreOrder,store_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.

此外,如果我这样做但随后注释StoreOrder对象以指定它应创建的表使用@Table(name =“order”)称为“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.

我意识到订单是一个保留字,但这是它无法创建表的原因吗?考虑到Hibernate docs使用一个名为Order的实体的例子,这似乎很奇怪。

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

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

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

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

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

...and the corresponding Persistence.xml

...以及相应的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>

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

有关为什么不创建此Order实体但是如果我将其重命名为StoreOrder而创建的任何想法?

Cheers,

J :)

2 个解决方案

#1


14  

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

ORDER字是保留关键字,你必须逃避它。

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

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

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

JPA 2.0 standardized this and the syntax looks like this:

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

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

References

  • Hibernate Core documentation
  • Hibernate Core文档5.4。 SQL引用的标识符

  • JPA 2.0 specification
    • 2.13 Naming of Database Objects
    • 2.13数据库对象的命名

  • JPA 2.0规范2.13数据库对象的命名

#2


1  

Probably because 'Order' is a keyword in SQL. When I encounter this I normally just put 'Tbl' on the end of my entity/table names to avoid the potential *.

可能是因为'Order'是SQL中的关键字。当我遇到这个时,我通常只在实体/表名的末尾加上'Tbl'以避免潜在的冲突。

#1


14  

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

ORDER字是保留关键字,你必须逃避它。

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

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

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

JPA 2.0 standardized this and the syntax looks like this:

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

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

References

  • Hibernate Core documentation
  • Hibernate Core文档5.4。 SQL引用的标识符

  • JPA 2.0 specification
    • 2.13 Naming of Database Objects
    • 2.13数据库对象的命名

  • JPA 2.0规范2.13数据库对象的命名

#2


1  

Probably because 'Order' is a keyword in SQL. When I encounter this I normally just put 'Tbl' on the end of my entity/table names to avoid the potential *.

可能是因为'Order'是SQL中的关键字。当我遇到这个时,我通常只在实体/表名的末尾加上'Tbl'以避免潜在的冲突。