JPA查询 - OnetoMany对多方

时间:2015-12-18 10:31:02

标签: hibernate hql jpql

我想执行将Partnership(1)链接到合作伙伴的JPA查询 (许多)。我希望指定的合作伙伴关系和指定年份的所有合作伙伴。

查询1 - 不起作用,将所有合作伙伴带回所有年份

query = "SELECT p FROM Partnership p"
  + " JOIN p.partners prt"
  + " WHERE prt.id.prtSchemeYear=:sYear"
  + " AND p.pspHerd=:herd")     

查询2 - 也不起作用 - 同样所有合作伙伴都返回,而只有2015年要求

query = "select distinct p from Partnership p"
      + " JOIN p.partners prt with (prt.id.prtSchemeYear=:sYear)"
      + " WHERE p.pspHerd=:herd")

{result - Partners for all years returned even though 2015 specified in :sYear, yes, restricted by herd using pk psp_cust_id

ONE - 合作伙伴     private long pspCustId; {这是PK(即)@Id)     private String pspHerd;     private Set partners = new HashSet(0)

@OneToMany(fetch = FetchType.EAGER, mappedBy = "partnership")
public Set<Partner> getPartners() {
    return this.partners;
}

MANY - 合作伙伴     私人PartnerId id; {复合PK类}     私人业主ownerPyPrtPreviousOwnId;     私营伙伴关系;     私人业主ownerPyOwnId;     私人长期prtHolding;

@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name = "prtSchemeYear", column = @Column(name =
  "PRT_SCHEME_YEAR", nullable = false, precision = 4, scale = 0)),
@AttributeOverride(name = "prtHerd", column = @Column(name = "PRT_HERD",
    nullable = false, length = 8)),
@AttributeOverride(name = "prtOwnId", column = @Column(name = 
    "PRT_OWN_ID", nullable = false, precision = 10, scale = 0)) })
    public PartnerId getId() {
        return this.id;
} 

PK等级 合作伙伴ID     @Embeddable     public class PartnerId实现java.io.Serializable {         私人短期prtSchemeYear;         private String prtHerd;         private long prtOwnId;

我试过的命名查询 - 与指定牛群和年份的合作伙伴建立合作伙伴关系(2015)

T_PARTNERS的结果SQL不包括仅限年份psp_cust_id(这是一个外键)的任何限制

谢谢你能提供帮助

0 个答案:

没有答案