243 Star 2.7K Fork 562

MyBatis-Flex/MyBatis-Flex

[Bug]: 开启sql 打印配置后 RelationManyToMany 的关联查询失效

已完成
创建于  
2023-08-29 14:22

这个 Bug 是否已经存在:

如何复现:

配置文件

#  application.yml
# DataSource Config
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test
    username: root
    password: 123456
#    type: com.alibaba.druid.pool.DruidDataSource

mybatis-flex:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

实体类

@Data
@Table("t_role")
public class Role implements Serializable {
    @Id(keyType = KeyType.Auto)
    private Integer id;

    private String name;

    @RelationManyToMany(
            joinTable = "t_user_role",
            joinSelfColumn = "role_id",
            joinTargetColumn = "user_id")
    private List<User> users;
}
@Data
@Table("t_user")
public class User implements Serializable {

    @Id(keyType = KeyType.Auto)
    private Integer id;

    private String name;

    @RelationManyToMany(
            joinTable = "t_user_role",
            joinSelfColumn = "user_id",
            joinTargetColumn = "role_id")
    private List<Role> roles;
}

数据库表结构

CREATE TABLE `t_role` (
  `id` int NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `t_user` (
  `id` int NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE `t_user_role` (
  `role_id` int DEFAULT NULL,
  `user_id` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

测试方法

@SpringBootTest
public class StudentTest {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;

 
    @Test
    public void get (){
        List<Role> roles = userMapper.selectOneWithRelationsById(2).getRoles();
        List<User> users = roleMapper.selectOneWithRelationsById(2).getUsers();
        System.out.println(roles.size());
        System.out.println(users.size());
    }
}

日志

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2cd3fc29] was not registered for synchronization because synchronization is not active
2023-08-29 13:57:47.107  INFO 16112 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-08-29 13:57:47.336  INFO 16112 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@669c2b07
2023-08-29 13:57:47.338  INFO 16112 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
==>  Preparing: SELECT * FROM `t_user` WHERE `id` = ?
==> Parameters: 2(Integer)
<==    Columns: id, name, username, password, sex, phone, code, address
<==        Row: 2, 2, 2, 2, 2, 2, 2, 2
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2cd3fc29]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6475e778] was not registered for synchronization because synchronization is not active
==>  Preparing: SELECT * FROM `t_user_role` WHERE user_id = ?
==> Parameters: 2(Integer)
<==    Columns: role_id, user_id
<==        Row: 1, 2
<==        Row: 2, 2
<==      Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6475e778]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@fd5689d] was not registered for synchronization because synchronization is not active
==>  Preparing: SELECT * FROM `t_role` WHERE `id` = ?
==> Parameters: 2(Integer)
<==    Columns: id, name
<==        Row: 2, 2
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@fd5689d]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@218f2f51] was not registered for synchronization because synchronization is not active
==>  Preparing: SELECT * FROM `t_user_role` WHERE role_id = ?
==> Parameters: 2(Integer)
<==    Columns: role_id, user_id
<==        Row: 2, 1
<==        Row: 2, 2
<==      Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@218f2f51]
0
0

预期结果:

预期结果多对多关系查询后可以获取到关联数据
正确结果
输入图片说明

实际结果:

实际上 开启 sql 打印后,多对多关联查询,无法获取到关联数据
实际结果
debug 结果显示

截图或视频:

user_role 关联表
输入图片说明
user 表
输入图片说明
role 表
输入图片说明
查询结果
debug 结果显示

关联版本:

1.6.1

评论 (3)

吃瓜的咸鱼 创建了任务 2年前
吃瓜的咸鱼 添加了
 
bug
标签
2年前
展开全部操作日志

1.5.8版本以后就有这个问题

进群远程看一下吧。

新版本已无法复现。

Michael Yang 任务状态待办的 修改为已完成 2年前

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(4)
路人甲QI-yunqi_zhao 7984572 suomm 1698661375 61279 fuhai 1716893681 吃瓜的咸鱼-sujinfengee
1
https://gitee.com/mybatis-flex/mybatis-flex.git
git@gitee.com:mybatis-flex/mybatis-flex.git
mybatis-flex
mybatis-flex
MyBatis-Flex

搜索帮助