background

The project verifies all the entities inputting data with @Validated of Spring. However, in the integration test, there are always problems of verification pass and warehousing exception.

Remember the development of the time tested input verification, how suddenly failed? At one point I suspected I had lost my memory and wondered why the @Validated check failed.

Oracle Varchar2 stores the length of Chinese

There is a problem: The @ validation of Spring passes, but the database insert time is too long. Suddenly realized that the problem was Chinese, Oracle stored Chinese, a Chinese length is 2.

Solution: The @ verification length cannot be the same as the database length. Set the verification length to half of the database length.

In addition, the EditPlus text editor displays columns with one Chinese character occupying two columns. Test through EditPlus input Chinese, look at the number of columns, thought the input Chinese character number is consistent with the number of columns, did not expect it is the same as Oracle.

I rely too much on the tool. I didn’t expect that there is a problem with the data length. The tracking direction has been placed on the failure of the @Validated check.

Review the process of solving the problem

To solve the Chinese length storage problem, try the following process:

  1. Suspect a package reference problem, go back to the project commit log to see if there is any change, no change.
  2. @ValidatedPossible causes of failure and solutions, failed. SpringBoot uses the verification package provided in 2.1.6, which is not a common problem on the Internet.
  3. Write aHellWorldTest class,ValidatedLabels can be checked correctly. Then adjust the real entity by adding one for a field@NotEmptyIf the corresponding field in the request parameter is empty, a verification failure message is returned indicating that the verification is effective.
  4. Yes, why can’t the data go into the library? Copy the test data to EditPlus and try to count it carefully. When the cursor moves a character, the number of columns increases by 2:

Finally realized it was the data,Chinese characters are not super long, but database storage is.

The revelation of

For all business entity classes, the @size data length is specified to ensure that invalid data is filtered before SQL execution. The length is initially consistent with the length of the database table field.

However, Oracle records the length of a Chinese character as 2. Therefore, when combining the @Validated data, it is necessary to pay attention to the Length of the Chinese character. I reduce the @SIZE range of @Validated to half of the length of the database field, so as to ensure smooth entry into the database when the Chinese character is the longest.