使用@ManyToMany时,JPA有很多选择查询
关系是
Person 1 : Bank N, Bank 1 : Account N
这是我的代码:
@Entity
public class Person {
@Id
@Column(unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "person_id")
private List<Bank> banks;
}
@Entity
public class Bank {
@Id
@Column(unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "bank_id")
private List<Account> accounts;
}
@Entity
public class Account {
@Id
@Column(unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
}
,查询日志为:
select ....
select ....
select ....
select ....
select ....
...
如果包含关系的表的行数增加,则查询数将增加。
并且性能更低...我想解决这个日益增长的查询问题。
如何在JAP或更少的查询上创建一个选择查询?