JPA @JoinTable with extra join conditions

懵懂的女人 提交于 2019-12-18 02:48:29

问题


I have spent couple of hours searching around and did not found anything similar to my case.

Let's assume following many-to-many data model:

Contract (any business entity)
- contract_id
- other fields

Party (another business entity)
- party_id
- other fields

Contract_Party (relations between first two 
with additional role indicator, e.g. owner, signer, seller, etc)
- contract_id
- party_id
- role

Now let's assume I want to map all contracts related to party (uni-directional). It can be done using following annotations in Party entity class:

@OneToMany
@JoinTable(
  name="Contract_Party", 
  joinColumns = {@JoinColumn(name="party_id", referencedColumnName="party_id")},
  inverseJoinColumns = {@JoinColumn(name="contract_id", referencedColumnName="contract_id")}
}
private List<Contract> contracts;

That is fine.

But what I'm looking for is how to map contracts with particular role?

@OneToMany
@??? ( "ROLE = 'SIGNER' ")
private List<Contract> signedContracts;

Technically I'm looking for a way to add extra condition into JOIN statement.

So far found following ideas in similar topics:

  • map join table as separate entity, and perform filtering by role using custom queries;
  • Hibernate has @JoinFormula annotation, but no way to apply it inside @JoinTable;
  • Hibernate also has @Where annotation, but it adds condition for Contract table not for join table;
  • use @MapKeyColumn and return Map instead of List, but I can have multiple contracts per one role;
  • create a view on DB side (this one works indeed :)

Thanks!


回答1:


You can use @WhereJoinTable annotation. It applies to the association table

@OneToMany
@JoinTable(
  name="Contract_Party", 
  joinColumns = {@JoinColumn(name="party_id",referencedColumnName="party_id")},
  inverseJoinColumns = {@JoinColumn(name="contract_id", referencedColumnName="contract_id")}
}
@WhereJoinTable  ( "ROLE = 'SIGNER' ")
private List<Contract> contracts;



回答2:


You must use:

@WhereJoinTable(clause = "ROLE ='SIGNER'")


来源:https://stackoverflow.com/questions/27945738/jpa-jointable-with-extra-join-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!