JPA, How to use the same class (entity) to map different tables?

后端 未结 3 550
暗喜
暗喜 2020-11-29 22:28

I have two tables: Ta and Tb. They have exactly the same table structure but different table names.

I try to create one entity class to map

3条回答
  •  时光说笑
    2020-11-29 22:58

    Create an abstract class (a template class) with annotation @MappedSuperclass then extend it. Each class that extends uses @table, @entity annotations and contains nothing but an empty constructor. All code will be in your parent class. On your methods use generics indicating your parameter entity extends from templateClass and no more code changes are needed. The proper mappings will be in each son you pass.

提交回复
热议问题