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

后端 未结 3 544
暗喜
暗喜 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:43

    You can also do this without using subclasses if you use two different persistence units.

    Each persistence unit can specify a unique set of mappings (including table name). One way to achieve this is to create two orm.xml files. In persistence.xml you'll need something like this :

    
    
         
            . . .
            orm-1.xml
            . . .
        
    
         
            . . .
            orm-2.xml
            . . .
        
    
    

    Then within orm-1.xml :

    
    
        mypackage
        
            

    And within orm-2.xml :

    
    
        mypackage
        
            

    You'll need to create a separate EntityManagerFactory for each PersistenceUnit (probably not what you want), but if you wanted to use the same class on different databases (with different table names) this would be a way to go.

提交回复
热议问题