简体   繁体   中英

JPQL query for many-to-many join table

I have 2 Entities User and AccountBase with a many-to-many relationship. I need to select all AccountBase objects with a selected User ID from the join table. I have tried some join queries but don't work.

    @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=? 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM