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.
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.
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.
It doesn't look like a MySQL problem as I can manually create a table called order directly on the database.
Here's the relevant Order entity object definition...
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Order extends PersistentEntity {
... rest of POJO def...
}
...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>
Any thoughts as to why this Order entity is not created but is created if I rename it to StoreOrder?
Cheers,
开发者_C百科J :)
The ORDER
word is a reserved keyword, you have to escape it.
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 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...
}
References
- Hibernate Core documentation
- 5.4. SQL quoted identifiers
- JPA 2.0 specification
- 2.13 Naming of Database Objects
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 clash.
精彩评论