JPA / Hibernate cannot create Entity called Order

I am using Hibernate / JPA and have an @Entity object called Order pointing to a MySQL database using a dynamic Hibernate table, i.e. generates runtime tables for objects. When Hibernate creates tables, it creates tables for all my objects except the Order entity. If I rename the Order object to something else, for example. StoreOrder, the store_order table is not created.

Also, if I do this, but then annotate the StoreOrder object to indicate that the table that it should create is called β€œorder” using @Table (name = "order"), the table is no longer created.

I understand that an order is a reserved word, but is this the reason that it cannot create a table? It seems strange given that Hibernate docs uses an example object called Order.

This doesn't seem to be a MySQL problem, since I can manually create a table called order directly in the database.

Here is the corresponding definition of the order entity object ...

@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 on why this order object is not created, but created if I rename it to StoreOrder?

Greetings

J :)

+7
java mysql orm hibernate jpa
source share
2 answers

The word ORDER is a reserved keyword, you need to avoid it.

There is no standardized method in JPA 1.0, and Hibernate's special solution is to use backlinks:

 @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 is as follows:

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

References

+13
source share

Probably because Order is a keyword in SQL. When I come across this, I usually just add β€œTbl” to the end of my entity / table names to avoid a potential collision.

+1
source share

All Articles