JPA:需要多对多查询帮助

发布于 2024-09-07 07:27:58 字数 1706 浏览 5 评论 0原文

我有四个实体参与查询,但我遇到了一些问题。关系如下:Exchange----*Contract*----*Combo----*Trade 和(简化的)实体如下:

@Entity
public class Exchange implements Serializable {
    @Id(name="EXCHANGE_ID")
    private long exchangeId;

    @Column
    private String exchangeShortName;
}


@Entity
public class Contract implements Serializable { 
        @Id
        private long contractId;

        @Column
        private String contractName;

        @ManyToOne
        @JoinColumn(name="EXCHANGE_ID")
        private Exchange exchange;

        @ManyToMany
        @JoinTable(name="CONTRACT_COMBO",
                        joinColumns = { @JoinColumn(name="CONTRACT_ID") },
                        inverseJoinColumns = {@JoinColumn(name="COMBO_ID")})
        private Set<Combo> combos;

        @Column(name = "ACTIVE_FLAG")
        private String activeFlag;
}

@Entity
public class Combo implements Serializable {

        @Id
        @Column(name="COMBO_ID")
        private Integer id;

        @ManyToMany
        @JoinTable(name="CONTRACT_COMBO",
                        joinColumns = { @JoinColumn(name="COMBO_ID") },
                        inverseJoinColumns = {@JoinColumn(name="CONTRACT_ID")})
        private Set<Contract> legs;

        @OneToMany(mappedBy = "combo")
        private Set<Trade> trades;    
}

@Entity
public class Trade implements Serializable {
        @Id
        @Column(name="TRADE_ID")
        private long tradeId;

        @Column(name="REFERENCE")
        private String reference;

        @ManyToOne
        @JoinColumn(name="COMBO_ID")
        private Combo combo;
}

我想获取以下列表特定交易所的所有交易,我无法完全与 MEMBER OF 合作。任何帮助将不胜感激。

I have four entities that are involved in a query that I'm having a little trouble with. The relationship is as follows : Exchange----*Contract*----*Combo----*Trade and the (simplified) entities are as follows:

@Entity
public class Exchange implements Serializable {
    @Id(name="EXCHANGE_ID")
    private long exchangeId;

    @Column
    private String exchangeShortName;
}


@Entity
public class Contract implements Serializable { 
        @Id
        private long contractId;

        @Column
        private String contractName;

        @ManyToOne
        @JoinColumn(name="EXCHANGE_ID")
        private Exchange exchange;

        @ManyToMany
        @JoinTable(name="CONTRACT_COMBO",
                        joinColumns = { @JoinColumn(name="CONTRACT_ID") },
                        inverseJoinColumns = {@JoinColumn(name="COMBO_ID")})
        private Set<Combo> combos;

        @Column(name = "ACTIVE_FLAG")
        private String activeFlag;
}

@Entity
public class Combo implements Serializable {

        @Id
        @Column(name="COMBO_ID")
        private Integer id;

        @ManyToMany
        @JoinTable(name="CONTRACT_COMBO",
                        joinColumns = { @JoinColumn(name="COMBO_ID") },
                        inverseJoinColumns = {@JoinColumn(name="CONTRACT_ID")})
        private Set<Contract> legs;

        @OneToMany(mappedBy = "combo")
        private Set<Trade> trades;    
}

@Entity
public class Trade implements Serializable {
        @Id
        @Column(name="TRADE_ID")
        private long tradeId;

        @Column(name="REFERENCE")
        private String reference;

        @ManyToOne
        @JoinColumn(name="COMBO_ID")
        private Combo combo;
}

I want to get a list of all trades for a particular exchange which I can't quite get to work with MEMBER OF. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假装不在乎 2024-09-14 07:27:58

试试这个

select distinct t 
  from Trade t
  join t.combo c
  join c.legs l
  join l.exchange e
 where e.exchangeShortName = 'whatever'

Try this

select distinct t 
  from Trade t
  join t.combo c
  join c.legs l
  join l.exchange e
 where e.exchangeShortName = 'whatever'
人间不值得 2024-09-14 07:27:58

没有真正优化,但我认为这应该可以解决问题:

Long exchangeId = Long.valueOf(5324623L);
List<Trade> trades = em.createQuery("select T from Trade T where T in " +
    "(select distinct C from Combo c where c member of " +
        "(select e.combos from Exchange e where e.id = :id) " +
    ")").setParameter("id", exchangeId).getResultList();

Not really optimized, but I think this should do the trick:

Long exchangeId = Long.valueOf(5324623L);
List<Trade> trades = em.createQuery("select T from Trade T where T in " +
    "(select distinct C from Combo c where c member of " +
        "(select e.combos from Exchange e where e.id = :id) " +
    ")").setParameter("id", exchangeId).getResultList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文