How to implement a temporal table using JPA?

后端 未结 4 1711
有刺的猬
有刺的猬 2021-01-31 04:29

I would like to know how to implement temporal tables in JPA 2 with EclipseLink. By temporal I mean tables who define validity period.

One problem that I\'m facing is t

4条回答
  •  孤街浪徒
    2021-01-31 04:45

    in DAO Fusion, tracking an entity in both timelines (validity and record interval) is realized by wrapping that entity by BitemporalWrapper.

    The bitemporal reference documentation presents an example with regular Order entity being wrapped by BitemporalOrder entity. BitemporalOrder maps to a separate database table, with columns for validity and record interval, and foreign key reference to Order (via @ManyToOne), for each table row.

    The documentation also indicates that each bitemporal wrapper (e.g. BitemporalOrder) represents one item within the bitemporal record chain. Therefore, you need some higher-level entity that contains bitemporal wrapper collection, e.g. Customer entity which contains @OneToMany Collection orders.

    So, if you need a "logical child" entity (e.g. Order or Player) to be bitemporally tracked, and its "logical parent" entity (e.g. Customer or Team) to be bitemporally tracked as well, you need to provide bitemporal wrappers for both. You will have BitemporalPlayer and BitemporalTeam. BitemporalTeam can declare @OneToMany Collection players. But you need some higher-level entity for containing @OneToMany Collection teams, as mentioned above. For example, you could create a Game entity that contains BitemporalTeam collection.

    However, if you don't need record interval and you just need validity interval (e.g. not bitemporal, but uni-temporal tracking of your entities), your best bet is to roll your own custom implementation.

提交回复
热议问题