While learning how to use Hybernate I'm working on a small order system. One of the classes within this system is the credit of a customer and I implemented this as follows:
<class name="com.infor.trial.Credit"
table="CREDIT" discriminator-value="DESC" >
<!-- primary key -->
<id name="id" column="id" type="java.lang.Long">
<generator class="native"/>
</id>
<!-- attributes-->
<property name="limit" column="limit" />
<property name="orderBalance" column="orderbalance" />
</class>
This caused the following exception when the tables are created as limit is a reserved word in MySQL:
ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - Unsuccessful: create table CREDIT (c_id bigint not null auto_increment, c_customer_id bigint not null unique, limit double precision, c_order_balance double precision, c_shipped_balance double precision, primary key (c_id))
ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limit double precision, c_order_balance double precision, c_shipped_balance doub' at line 1
ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - Unsuccessful: alter table CREDIT add index FK76F89EF919E1330D (c_customer_id), add constraint FK76F89EF919E1330D foreign key (c_customer_id) references CUSTOMER (c_id)
ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - Table 'sls_order1_db.credit' doesn't exist
2008-01-16 09:24:12,302 ERROR org.hibernate.util.JDBCExceptionReporter - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limit, c_order_balance, c_shipped_balance) values (1, 0.0, 0.0, 0.0)' at line 1
ERROR com.infor.trial.Main - org.springframework.dao.InvalidDataAccessResourceUsageException: could not insert: [com.infor.trial.Credit]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [com.infor.trial.Credit]
However, if I add "opposite single quotes" (on my international keyboard above the <tab> on the same key as the ~) then this problem does not occur and the table is created. This solution came when trying to create the table manually in MySQL.
<class name="com.infor.trial.Credit"
table="CREDIT" discriminator-value="DESC" >
<!-- primary key -->
<id name="id" column="id" type="java.lang.Long">
<generator class="native"/>
</id>
<!-- attributes-->
<property name="limit" column="`limit`" />
<property name="orderBalance" column="order_balance" />
</class>
Suggestion: It is hard to know all reserved words for all databases and you won't always know beforehand on which database your code will be running. Can hibernate be augmented that column-names will always be "quoted" as applicable for the database you are running on
_________________ With Kind Regards,
Andre Kamsteeg
|