Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JOIN 查询同一张表多次,副表数据错乱bug #781

Open
gxmanito opened this issue Nov 27, 2024 · 12 comments
Open

JOIN 查询同一张表多次,副表数据错乱bug #781

gxmanito opened this issue Nov 27, 2024 · 12 comments

Comments

@gxmanito
Copy link

Description

APIJSON Version/APIJSON 版本号
6.3.0

Database Type & Version/数据库类型及版本号
mysql8

如图,JOIN 查询同一张表多次,副表数据错乱bug,explain执行计划打印的SQL是对的,但是副表的数据不对

image

@gxmanito gxmanito changed the title [xxx] JOIN 查询同一张表多次,副表数据错乱bug JOIN 查询同一张表多次,副表数据错乱bug Nov 27, 2024
@TommyLemon
Copy link
Collaborator

断点调试下 AbstractSQLExecutor.execute 中对 JOIN 查到 ResultSet 后分离 主、副 表数据的处理
https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java#L390-L584

如果这里没问题,也可能是后面对 User:a 生成的缓存 SQL 与对 User:b 生成的缓存 SQL 一致,导致 AbstractSQLExecuto.getCacheItem 取错了

@TommyLemon
Copy link
Collaborator

也可以先升级到 7.0.3/6.4.3 等更高版本后试试

@TommyLemon
Copy link
Collaborator

也可以先把驼峰转换去掉后试试还有没有同样问题,看看是不是自己的改动导致

@gxmanito
Copy link
Author

gxmanito commented Dec 2, 2024

也可以先升级到 7.0.3/6.4.3 等更高版本后试试

升级版本暂时不能升级的

@gxmanito
Copy link
Author

gxmanito commented Dec 2, 2024

也可以先把驼峰转换去掉后试试还有没有同样问题,看看是不是自己的改动导致

驼峰转换去除后还是有问题的

@gxmanito
Copy link
Author

gxmanito commented Dec 2, 2024

断点调试下 AbstractSQLExecutor.execute 中对 JOIN 查到 ResultSet 后分离 主、副 表数据的处理 https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java#L390-L584

如果这里没问题,也可能是后面对 User:a 生成的缓存 SQL 与对 User:b 生成的缓存 SQL 一致,导致 AbstractSQLExecuto.getCacheItem 取错了

是缓存取错了,但是暂时没有太好的方案处理,如果去掉缓存会导致n+1查询

@TommyLemon
Copy link
Collaborator

把具体的相关代码调试截屏发出来,这样纯文本的一问一答很低效,我也没时间去猜各种可能

@gxmanito
Copy link
Author

gxmanito commented Feb 5, 2025

#781 (comment)
master分支是否兼容jdk1.8呀

@TommyLemon
Copy link
Collaborator

TommyLemon commented Feb 6, 2025

#781 (comment) master分支是否兼容jdk1.8呀

@gxmanito

代码兼容 JDK 1.8,但目前配置打包 JDK 版本是 17,可以改为 JDK 1.8 打包,这样业务项目就能用 JDK 1.8 了:
1.删除 jitpack.yml 文件
https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/jitpack.yml

2.java.version、maven.compiler.source、maven.compiler.target 和 maven-compiler-plugin 中 configuration 的 source、target 都改成 1.8
https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/pom.xml

apijson-framework 已经兼容 JDK 1.816 及 SpringBoot 1.42.7 了,也可以改为 JDK 1.8 打包。
https://github.com/APIJSON/apijson-framework/releases/tag/7.1.5

@gxmanito
Copy link
Author

gxmanito commented Feb 8, 2025

#781 (comment)
已成功升级到最新版本,感谢!

不过还是遇到了一个问题,我们项目组要求传参所有必须是驼峰格式,目前@column字段、where条件、出参驼峰均没问题,但是join关联字段时有很大问题,过程也改了很多源码,但是还是各种bug(比如n+1查询等),您有没有一键配置支持所有驼峰的规划呢

Image

Image

Image

Image

Image

@TommyLemon
Copy link
Collaborator

TommyLemon commented Feb 8, 2025

@gxmanito
JSONResponse.IS_FORMAT_UNDERLINE = true
https://github.com/Tencent/APIJSON/blob/master/APIJSONORM/src/main/java/apijson/JSONResponse.java#L25-L34

不过这个只对最终返回前端的 JSON key 生效,把数据库的 蛇形命名 字段名 转成 驼峰命名。

你的需求还需要把 on.getKey() 和 on.getOriginKey() 的 驼峰命名 转成数据库的 蛇形命名,
可以在 AbstractSQLConfig 中加个 formatKey 方法,另外 getSQLKey 也调用下 formatKey。

AbstractSQLExecutor 也可以加个 formatKey 方法,把数据库的 蛇形命名 字段名 转成 驼峰命名。

所有涉及 on.getKey() 和 on.getOriginKey() 的代码都可以通过 IDE 对 Join.On 中这两个方法 Find Usages 来查找,避免遗漏。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants