英文:
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="reconciliationId")
@Where(clause = "debit_type = 'TAX_RETURN'")
private List<BtaReconciliation> taxPaidReconciliation;
The BtaReconciliation class is like this:
@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;
The thing is that the relationship should be a OneToOne rather than a ManyToOne.
The issue is when I change to this:
@OneToOne(mappedBy="reconciliationId")
@Where(clause = "debit_type = 'TAX_RETURN'")
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
专注分享java语言的经验与见解,让所有开发者获益!
评论