英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论