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

Jay*_*ark 10 java mysql orm hibernate jpa

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

此外,如果我这样做但是然后注释StoreOrder对象以指定它应该创建的表使用@Table(name ="order")称为"order",则不再创建表.

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

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

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

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

...和相应的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>
Run Code Online (Sandbox Code Playgroud)

有关为什么没有创建这个Order实体但是如果我将它重命名为StoreOrder而创建的想法?

干杯,

J :)

Pas*_*ent 17

这个ORDER词是保留的关键字,你必须逃避它.

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

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

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

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

参考