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

实体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"
            }
        ]
    }
]

请教大家这是出了什么问题?谢谢
阅读 8.1k
1 个回答

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题