JPA:在搜索ID之前,JPA是否修剪字符串?

huangapple go评论143阅读模式
英文:

JPA : Do JPA trims the string before it search for id

问题

在这里,我将user_id传递为"abc "" abc "。我正在使用DB2数据库。现在数据库中有abc,但即使添加了空格,JPA仍然能够成功找到ID并返回对象。

在JPA中是否有关于处理空格的规则?

Test test = testRepository.findByUserId(user_id).orElseThrow(
    () -> new UsernameNotFoundException("User Not Found with -> username " + user_id));
英文:

here I am pass user_id as "abc " or " abc ". I am using DB2 . now I have abc in the database, but even on adding the space JPA is finding the id successfully and return the object.

do we have any rule on treating the space in JPA.

Test test = testRepository.findByUserId(user_id).orElseThrow(
				() -> new UsernameNotFoundException("User Not Found with -> username " + user_id));

答案1

得分: 1

这是由于DB2在字符串比较方面的默认行为所致。根据字符串比较中的相关文档:

当比较长度不等的字符字符串时,将使用短字符串的逻辑副本进行比较,该副本在右侧填充了足够的空格,以将其长度扩展到较长字符串的长度。这种逻辑扩展适用于所有字符字符串,包括标记为FOR BIT DATA的字符串。

如果您希望不忽略空格,请改用LIKE功能。

英文:

This is due to the default behaviour in DB2 when it comes to String comparison. According to the respective documentation in the Section String comparisons:

> When comparing character strings of unequal lengths, the comparison is made using a logical copy of the shorter string, which is padded on the right with blanks sufficient to extend its length to that of the longer string. This logical extension is done for all character strings, including those tagged as FOR BIT DATA.

If you would like for the whitespaces to not be ignored, you would need to use the LIKE functionality instead.

huangapple
  • 本文由 发表于 2020年8月3日 21:56:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63230802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定