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

[BUG] #242

Open
whatmiss opened this issue Nov 28, 2022 · 3 comments
Open

[BUG] #242

whatmiss opened this issue Nov 28, 2022 · 3 comments
Assignees
Labels
bug 错误

Comments

@whatmiss
Copy link

测试模块:demo-orm-jpa
数据库的表有父子关系:
CREATE TABLE orm_department (
id int NOT NULL AUTO_INCREMENT COMMENT '主键',
name varchar(32) NOT NULL COMMENT '部门名称',
superior int DEFAULT NULL COMMENT '上级id',
levels int NOT NULL COMMENT '层级',
order_no int NOT NULL DEFAULT '0' COMMENT '排序',
create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
last_update_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '上次更新时间',
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='Spring Boot Demo Orm 系列示例表

然后查询处理的结果会这样无限循环,这是为什么呢?
查询方式(两种):

// List departmentList = departmentDao.findDepartmentsByLevels(1);
List departmentList = departmentDao.findAll();
ret.put("data", departmentList);
return ret;

image

@whatmiss whatmiss added the bug 错误 label Nov 28, 2022
@guxiren
Copy link

guxiren commented Nov 28, 2022 via email

@hugqq
Copy link

hugqq commented Feb 21, 2023

User类需要加个注解

/**
* 关联部门表
* 1、关系维护端,负责多对多关系的绑定和解除
* 2、@jointable注解的name属性指定关联表的名字,joinColumns指定外键的名字,关联到关系维护端(User)
* 3、inverseJoinColumns指定外键的名字,要关联的关系被维护端(Department)
* 4、其实可以不使用@jointable注解,默认生成的关联表名称为主表表名+下划线+从表表名,
* 即表名为user_department
* 关联到主表的外键名:主表名+下划线+主表中的主键列名,即user_id,这里使用referencedColumnName指定
* 关联到从表的外键名:主表中用于关联的属性名+下划线+从表的主键列名,department_id
* 主表就是关系维护端对应的表,从表就是关系被维护端对应的表
*
* JPA多对多关系导致的栈溢出问题
* 使用 @JsonIgnore注解 或者使用@JsonManagedReference和@JsonBackReference注解(使用这组注解可以序列化另一个关系的类)。具体详见代码
*/
@manytomany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@jointable(name = "orm_user_dept",
joinColumns = @joincolumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @joincolumn(name = "dept_id", referencedColumnName = "id"))
@JsonIgnore
private Collection departmentList;

@guxiren
Copy link

guxiren commented Feb 21, 2023 via email

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

No branches or pull requests

4 participants