6

specification query doesn't work

When writing the background, you need to add a specification query to return only the data belonging to the community.
image.png

So I added the query in the red box in the figure and called the and function.

 specification.and(VehicleSpecs.belongToCommunity(district.getId()));

However, after execution, it was found that the query did not obtain the query data according to the idea, which is equivalent to this query has no effect .

After the breakpoint, it was found that it did enter the query function .

After testing it many times, this query does not work.

Later I checked Google and found that someone had the same problem as me.
The solution is simple:
Let specification equal to and function return value

image.png

That's it:

 specification = specification.and(VehicleSpecs.belongToCommunity(district.getId()));

Then I looked back at the declaration of the and function and found that:
The returned data is a Specification type. That is, I didn't save this data, so the query failed.
Local variables should be reassigned. Only in this way can the query actually be added.
image.png


After looking back and thinking about it, I didn't think of the reason for the reassignment. It may be that I got used to the way in the figure below, that is, when the function is called, the data is really added, and there is no need to reassign it.

image.png

Summarize

In conclusion, you still need to really look at the function declaration and its return value to determine whether you need to perform related operations. Because the usage and return value of this function are written in the declaration.

Unit test data is inconsistent

In the unit test, the error occurs on line 187.

The value returned by MockMvc is inconsistent with the expected value

image.png


But go to the console and take a look: the values are the same. The expected value is 0, and the actual value is also 0. This is very strange, the expected value and the actual value are the same, why the two fail to pass the judgment.
image.png

It's easy to link that it's the type that's the problem.


The test found that Long, Integer, String, all passed. The data that did not pass is the Short type, that is, the data in the red box.

image.png

Later, adding .toString() at the back can pass normally.
image.png

Here, the data of json type of mvc is string type, and the judgment of short type data fails. It may be that the two cannot be recognized normally.


weiweiyi
1k 声望123 粉丝