About the conflict between Hibernate entity class field and database keyword under MySQL

Source: Internet
Author: User

Long time no write, have forgotten the blog, while now still in the company, write something is verified, not at home by memory write, the correct rate has security, say said recently encountered one thing.

Previously used Oracle database, this project I am responsible for the module is the system is used MySQL database, the results of the original table, the field what all rely on Baidu, is not English, and then there is a field called usage, is the MySQL database key word, when their own testing time found, But think I just SQL statement, add ", as can be used, so there is no attention.

The results of the first issue has been on-line, and now for the table to do other maintenance, only to find that the use of hibernate directly with the deletion of the problem, because it generated the statement is definitely not with ' this escaped, and this time if you do not want to change the entity class do not want to change the table field, Then you only need to add "to the entity class configuration file."

The specific demo code is as follows:

1. Initial configuration file:

1 2 <class name= "Com.entity.test" table= "Test" >3 <id column= "id" name= "id" type= "java.lang.Long" >4class= "Native"/>5 </ Id>6 <property column= "Usage" length= "1" name= "usage" type= "java.lang.String"/>7 </ class>8 

In this configuration, directly for the entire entity class operation will be error, that is what xxx ' near the usage xxx Those things, is the usage around the error, the specific English can not I also can not understand, only know it caused.

2. Post-Change configuration file

class= "native"/></id><property column= "' Usage '" Length= "1" name= "usage" type= "java.lang.String"/></class>

After this change, do not need to change the other things, as usual, the two ' position is usually under the keyboard, the number keys 1 the left of the one, and ~ a key, rather than single quotes, this is not mistaken.

  

Next is the digression, although with this solution, but finally still helpless to change the field, although it will be very troublesome, after all, a period has been on-line, and quite a lot of local code used in this field, do not know whether there is no change. The reason why this solution is not adopted is that the subsequent iteration of the project will inevitably result in the need for the tool class to regenerate the entity, and the generic tool-generating entity will not have the ' this ', resulting in an error. Then Yimeimei maintenance, not my own maintenance, this kind of error is more difficult to find.

So in general, the program ape in the definition of the table structure as far as possible not to use the database keyword, define the field, it is likely that the keyword is the first to try to avoid the back more white so much kung fu.

About the conflict between Hibernate entity class field and database keyword under MySQL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.