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"
}
]
}
]
请教大家这是出了什么问题?谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显示一下程序运行是的sql语句啊!