带有额外连接条件的 JPA @JoinTable [英] JPA @JoinTable with extra join conditions

查看:49
本文介绍了带有额外连接条件的 JPA @JoinTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几个小时四处寻找,但没有找到与我的情况类似的东西.

让我们假设遵循多对多数据模型:

<前>合同(任何商业实体)- 合同 ID- 其他领域当事人(另一个商业实体)-party_id- 其他领域Contract_Party(前两者之间的关系带有额外的角色指示器,例如所有者、签名者、卖方等)- 合同 ID-party_id- 角色

现在让我们假设我要映射所有与参与方相关的合同(单向).可以使用 Party 实体类中的以下注释来完成:

@OneToMany@JoinTable(name="Contract_Party",joinColumns = {@JoinColumn(name="party_id",referencedColumnName="party_id")},inverseJoinColumns = {@JoinColumn(name="contract_id",referencedColumnName="contract_id")}}私人名单<合同>合同;

没关系.

但我正在寻找的是如何映射具有特定角色的合同?

@OneToMany@???( "ROLE = 'SIGNER' ")私人名单<合同>签订的合同;

从技术上讲,我正在寻找一种将额外条件添加到 JOIN 语句中的方法.

到目前为止,在类似主题中发现了以下想法:

  • 将连接表映射为单独的实体,并使用自定义查询按角色执行过滤;
  • Hibernate 有@JoinFormula 注释,但无法在@JoinTable 中应用它;
  • Hibernate 也有@Where 注释,但它为合同表而不是连接表添加了条件;
  • 使用@MapKeyColumn 并返回 Map 而不是 List,但我可以为一个角色拥有多个合同;
  • 在数据库端创建一个视图(这个确实有效:)

谢谢!

解决方案

可以使用@WhereJoinTable 注解.适用于关联表

@OneToMany@JoinTable(name="Contract_Party",joinColumns = {@JoinColumn(name="party_id",referencedColumnName="party_id")},inverseJoinColumns = {@JoinColumn(name="contract_id",referencedColumnName="contract_id")}}@WhereJoinTable ("ROLE = 'SIGNER'")私人名单<合同>合同;

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!

解决方案

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;

这篇关于带有额外连接条件的 JPA @JoinTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆