JPA / Hibernate – @OneToOne 和 @Where 不起作用 – 那么现在怎么办?

huangapple 未分类评论44阅读模式
英文:

JPA / Hibernate - @OneToOne and @Where not working - so what now?

问题

我已成功在我的@OneToMany关系中使用了@Where子句,如下所示:

@OneToMany(mappedBy="reconciliationId")
@Where(clause = "debit_type = 'TAX_RETURN'")
private List<BtaReconciliation> taxPaidReconciliation;

BtaReconciliation类如下所示:

@Table(name = "bta_reconciliation")
public class BtaReconciliation implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "reconciliation_id")
private Long reconciliationId;

@Column(name = "debit_type")
private String debitType;

@Column(name = "credit_type")
private String creditType;

问题在于该关系应该是OneToOne而不是ManyToOne。当我更改为以下内容时出现问题:

@OneToOne(mappedBy="reconciliationId")
@Where(clause = "debit_type = 'TAX_RETURN'")
private BtaReconciliation taxPaidReconciliation;

它不起作用。taxPaidReconciliation字段始终为null。

我在网上搜索了一些内容,我看到@Where不能与@OneToOne一起使用。

它就像上面那样无法工作。

我尝试了许多其他选项,但我无法弄清楚如何使其工作。

注意:BtaReconciliation类不应进行更新,因为有许多debit_types,我不想在那里引入更改以支持每个debit_type。

我期待着您的回复,以解决这个问题。

顺祝商祺,
Fergal

英文:

I have successfully used a @Where clause in my @OneToMany relationship as follows:

 @OneToMany(mappedBy=&quot;reconciliationId&quot;)
 @Where(clause = &quot;debit_type = &#39;TAX_RETURN&#39;&quot;)
 private List&lt;BtaReconciliation&gt; taxPaidReconciliation;

The BtaReconciliation class is like this:

@Table(name = &quot;bta_reconciliation&quot;)
public class BtaReconciliation implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = &quot;reconciliation_id&quot;)
private Long reconciliationId;

@Column(name = &quot;debit_type&quot;)
private String debitType;

@Column(name = &quot;credit_type&quot;)
private String creditType;

The thing is that the relationship should be a OneToOne rather than a ManyToOne.
The issue is when I change to this:

 @OneToOne(mappedBy=&quot;reconciliationId&quot;)
 @Where(clause = &quot;debit_type = &#39;TAX_RETURN&#39;&quot;)
 private BtaReconciliation taxPaidReconciliation;

it doesn't work. The taxPaidReconciliation field is always null.

I did some searching online and I can see that @Where cannot be used with @OneToOne.

It simply won't work as above.

I have tried many other options but I simply can't figure out a way to get this to work.

Note: The BtaReconciliation class should not be updated as there are many debit_types and I don't want to introduce changes there to support each debit_type.

I'm looking forward to hearing back on how to best resolve this issue.

Kind regards,
Fergal

huangapple
  • 本文由 发表于 2020年4月8日 21:34:39
  • 转载请务必保留本文链接:https://java.coder-hub.com/61101978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定