SpringBoot中@ManyToMany的坑

我在User表中添加了manytomany的外键映射

@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(name="user_role",
joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
inverseJoinColumns={@JoinColumn(name="role_id", referencedColumnName="id")})
private Set<Role> roles;

这导致了我的获取用户产品接口,返回记录时出现了重复,即相同的记录出现两次
这让我十分困惑

问题原因:外键out join时,user表被展成了多行
http://stackoverflow.com/questions/1995080/hibernate-criteria-returns-children-multiple-times-with-fetchtype-eager

去掉FetchType.EAGER就好了
这就意味着如果fetchtype未eager,获取到的用户数据其实是多行的

原文地址:https://www.cnblogs.com/plwang1990/p/6371049.html