Changing the inheritance strategy in branches of the class hierarchy via JPA annotations

前端 未结 1 1063
孤城傲影
孤城傲影 2020-12-10 09:27

Today I faced an interesting issue. I\'ve been having an inheritance hierarchy with Hibernate JPA, with SINGLE_TABLE strategy. Later, I added a superclass to th

1条回答
  •  自闭症患者
    2020-12-10 09:35

    Well, if you read the "Inheritance" chapter of Hibernate documentation a little further :-) you'll see that the example given for mixing table-per-hierarchy and table-per-subclass strategies is in reality nothing more than table-per-hierarchy with secondary tables thrown in:

    
        
            
        
        
        
        ...
        
            
                
                ...
            
        
        
            ...
        
    
    

    You can do the same using @SecondaryTable annotation:

    @Entity
    @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(name="PAYMENT_TYPE")
    @DiscriminatorValue("PAYMENT")
    public class Payment { ... }
    
    @Entity
    @DiscriminatorValue("CREDIT")
    @SecondaryTable(name="CREDIT_PAYMENT", pkJoinColumns={
        @PrimaryKeyJoinColumn(name="payment_id", referencedColumnName="id")
    )
    public class CreditCardPayment extends Payment { ... }
    
    @Entity
    @DiscriminatorValue("CASH")
    public class CashPayment extends Payment { ... }
    

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