简体   繁体   中英

How can the criteria api add quote to the column?

In my aplication I use eclipselink and the criteria api.

My database have a table with a column called "order".

The problem is when I use the criteria api to create a select it made this sql:

SELECT id, order, name, phone, uri FROM campus

It throw a exception because "order" is a restrict keyword in sql

How can I force the criteria api to put quotes in the columns names?

The easiest (and IMHO the best) way is to change order to campus_order or something like that and avoid using SQL keywords as a field identifier. This practice typically causes problems.

I will be glad to know that criteria API has some kind of work around for this problem but I'd recommend you to rename the column. Today you are using criteria API, tomorrow you will use something else... But at the end of the day the good old SQL is generated and the last think you want is to find that one of your queries does not work because the column name equals to keyword of one of SQL versions.

Renaming the field is an easier option, but JPA will quote delimited fields. To mark it as delimited, just add quotes when defining the column: "\\"order\\"".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM