spring data jpa onetomany关系,查询记录重复

发布于 2022-09-05 04:01:56 字数 5808 浏览 18 评论 0

实体OneToMany关系 查询记录重复

Group Entity:

@Entity
@Table(name="t_group")
public class Group implements Serializable{
    private static final long serialVersionUID = -2287958893052502698L;
    private int id;
    private String name;
    private Set<User> users= new HashSet<>();

    @Id
    @GeneratedValue(generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }


    @OneToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,targetEntity = User.class)
    @JoinColumn(name = "group_id")
    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
            this.users = users;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Group)) return false;

        Group group = (Group) o;

        if (id != group.id) return false;
        if (!name.equals(group.name)) return false;
        if (!users.equals(group.users)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + name.hashCode();
        result = 31 * result + users.hashCode();
        return result;
    }
}  


User Entity:

@Entity
@Table(name="t_user")
public class User implements Serializable {
    private static final long serialVersionUID = -3887964705538451980L;
    private int id;
    private String name;

    private Integer age;

    private String province;

    public User() {
    }

    @Id
    @GeneratedValue(generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }


    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }
}


SQL语句:

/**group_sql**/

INSERT INTO `springjpa2`.`t_group` (`id`, `name`) VALUES ('1', 'SLAVE');
INSERT INTO `springjpa2`.`t_group` (`id`, `name`) VALUES ('2', 'MASTER');


/**user_sql**/

INSERT INTO `t_user` (`id`, `age`, `name`, `province`, `group_id`) VALUES ('3', '20', 'AA', 'NewYork', '1');
INSERT INTO `t_user` (`id`, `age`, `name`, `province`, `group_id`) VALUES ('4', '21', 'BB', 'NewYork', '1');
INSERT INTO `t_user` (`id`, `age`, `name`, `province`, `group_id`) VALUES ('6', '23', 'CC', 'NewYork', '2');
INSERT INTO `t_user` (`id`, `age`, `name`, `province`, `group_id`) VALUES ('7', '22', 'DD', 'NewYork', '1');


GroupRepository:

public interface GroupRepository extends JpaRepository<Group,Integer>,JpaSpecificationExecutor<Group>{

}


Controller:

@RequestMapping("/ds")
@RestController
public class DataSourceController  {

    @Autowired
    GroupRepository groupRepository;

    @RequestMapping("")
    public  Object test(){

        List<Group> thisGroup = groupRepository.findAll((root, query, cb) -> {
            Predicate p1 = cb.equal(root.get("id"), 1);
            root.fetch("users");
            query.where(p1);
            return query.getRestriction();
        });

        return  thisGroup;
    }

}



返回结果:

[
    {
        "id":1,
        "name":"SLAVE",
        "users":[
            {
                "id":4,
                "name":"BB",
                "age":21,
                "province":"NewYork"
            },
            {
                "id":7,
                "name":"DD",
                "age":22,
                "province":"NewYork"
            },
            {
                "id":3,
                "name":"AA",
                "age":20,
                "province":"NewYork"
            }
        ]
    },
    {
        "id":1,
        "name":"SLAVE",
        "users":[
            {
                "id":4,
                "name":"BB",
                "age":21,
                "province":"NewYork"
            },
            {
                "id":7,
                "name":"DD",
                "age":22,
                "province":"NewYork"
            },
            {
                "id":3,
                "name":"AA",
                "age":20,
                "province":"NewYork"
            }
        ]
    },
    {
        "id":1,
        "name":"SLAVE",
        "users":[
            {
                "id":4,
                "name":"BB",
                "age":21,
                "province":"NewYork"
            },
            {
                "id":7,
                "name":"DD",
                "age":22,
                "province":"NewYork"
            },
            {
                "id":3,
                "name":"AA",
                "age":20,
                "province":"NewYork"
            }
        ]
    }
]


这样的结果并不是我想要的结果  明显数据重复

正确的结果应该是这样的:

[
    {
        "id":1,
        "name":"SLAVE",
        "users":[
            {
                "id":4,
                "name":"BB",
                "age":21,
                "province":"NewYork"
            },
            {
                "id":7,
                "name":"DD",
                "age":22,
                "province":"NewYork"
            },
            {
                "id":3,
                "name":"AA",
                "age":20,
                "province":"NewYork"
            }
        ]
    }
]

请教大家这是出了什么问题?谢谢

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖个坑埋了你 2022-09-12 04:01:56

显示一下程序运行是的sql语句啊!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文