我有 2 个具有多对多关系的实体 User 和 AccountBase。 我需要从连接表中选择具有选定用户 ID 的所有 AccountBase 对象。 我尝试了一些联接查询,但不起作用。
@Table(name = "ACCOUNT")
@DiscriminatorColumn(name = "ACCOUNT_TYPE", length = 1)
public abstract class AccountBase extends ModelBase {
protected double balance;
protected List<User> users = new ArrayList<>();
@Table(name = "USER_ACCOUNT")
public class User extends ModelBase implements Serializable {
private static final long serialVersionUID = 1L;
protected String name;
protected List<AccountBase> bankAccounts = new ArrayList<>();
// bi-directional many-to-many association to AccountBase
@ManyToMany
@JoinTable(name = "USER_ACCOUNT_ACCOUNT", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = {
@JoinColumn(name = "ACCOUNT_ID") })
public List<AccountBase> getBankAccounts() {
return this.bankAccounts;
}
最佳答案
从帐户库加入用户实体
select account from AccountBase account join account.users user where user.id=?
关于java - 多对多连接表的 JPQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59121958/