Hibernate Join two unrelated table when both has Composite Primary Key

前端 未结 5 1442
自闭症患者
自闭症患者 2020-12-31 09:21

I\'m writing java application using hibernate 5.2 but without HQL

there is two table, Transactions and ResponseCode

5条回答
  •  -上瘾入骨i
    2020-12-31 10:16

    You have mapped a single ResponseCode entity in Transaction, which is wrong. The response code is not a PK, it does not uniquely identifies a ResponseCode entity for a given Transaction entity. E.g. for a transaction with response code 000 there are 2 ResponseCode entities (with 'en' and 'ge' langs).

    I recommend you to try to map a collection instead.

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name="rc")
    private List rcList;
    

    Since your query WHERE conditions apply to the Transaction only, you can simply query the Transaction entities. Hibernate cache will optimize the eventual subqueries you need for each response code (one query for '000', one for '116', etc).

提交回复
热议问题