Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

java - JPQL查询多对多联接表(JPQL query for many-to-many join table)

I have 2 Entities User and AccountBase with a many-to-many relationship.

(我有2个实体,用户和AccountBase具有多对多关系。)

I need to select all AccountBase objects with a selected User ID from the join table.

(我需要从联接表中选择具有选定用户ID的所有AccountBase对象。)

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;
    }

在此处输入图片说明

  ask by Phuc Nguyen translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

从帐户库加入用户实体

select account from AccountBase account join account.users user where user.id=? 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...