How to map one class to different tables using hibernate/jpa annotations

前端 未结 3 1973
你的背包
你的背包 2020-12-03 03:36

I\'m currently stuck with what seems to be a very simple problem, but I just can\'t seem to find a way around:

I have 2 identical tables:

  1. tb
相关标签:
3条回答
  • 2020-12-03 04:21

    Using @MappedSuperclass, you would proceed as follows:

    @MappedSuperclass
    public class Transaction ...
    
    @Entity
    @Table(name="tbl_creditcard_approved_txns")
    public class DeclinedTransaction extends Transaction ...
    
    @Entity
    @Table(name="tbl_creditcard_declined_txns")
    public class ApprovedTransaction extends Transaction ...
    

    Use @AttributeOverride to override column names between the two types of Transaction objects, if needed.

    Update: I see that you want to map one @Entity to two tables in the same EntityManagerFactory ... I don't think you can do that.

    0 讨论(0)
  • 2020-12-03 04:22

    You have to use two different persistence units or two separate entities. This was already answered here.

    0 讨论(0)
  • 2020-12-03 04:39

    The other way to do it would be to use a partioned table on the database layer that would then make visible one single table to your java code.

    This is probably the way to go as a general rule, the more smart partitioning you do, the faster your queries can be.

    • Mysql
    • Oracle
    0 讨论(0)
提交回复
热议问题