Tuesday 15 June 2010

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


I had to face an interesting issue today. I have a heritage hierarchy with Hibernate JPA, in which with the SINGLE_TABLE strategy later, I added a superclass to the hierarchy, which defines the TABLE_PER_CLASS strategy. It happened that the entire hierarchy was trying to behave as TABLE_PER_CLASS . This is definitely fair, if we read @Inheriatance javadoc:

, the unit defines the heritage strategy used for the category hierarchy. This unit is specified on the square which is the root of the unit category hierarchy.

, however, it has to be said that:

It is possible to use different mapping strategies for different branches of different branches, the same heritage hierarchy

and This statement will continue on exemption, it is done through XML configuration.

So, I have a question - to enable XML XML behavior through annotation, and EntityManager .

OK, if you read the "inheritance" chapter of the hibernate documentation :-) - The example given for mixing hierarchy and table-per-sub-class strategy, in reality, is thrown in the secondary table more than the table-per-hierarchy:

  & lt ; Class name = "payment" table = "payment" & gt; & Lt; Id name = "id" type = "tall" column = "PAYMENT_ID" & gt; & Lt; Generator class = "parent" /> & Lt; / Id & gt; & Lt; Discriminator column = "PAYMENT_TYPE" type = "string" / & gt; & Lt; Asset name = "zodiac" column = "AMOUNT" /> ... & lt; Subclass name = "credit card payment" discriminator-value = "CREDIT" & gt; & Lt; Include table = "CREDIT_PAYMENT" & gt; & Lt; Property Name = "Credit Card Type" column = "CCTVPE" /> ... & lt; / Join> & Lt; / Subclass & gt; & Lt; Subclass name = "cash payment" discriminator-value = "cash" & gt; ... & lt; / Subclass & gt; & Lt; / Square & gt;  

You can do this by using:

  @ Entity @ Inheritance (strategy = inheritance type SINGLE_TABLE) @DiscriminatorColumn (name = "PAYMENT_TYPE "@DiscriminatorValue (" Payment ") Public Segment Payment {...} @ Entity @ Dispinner Value (" Credit ") @ Secondary Table (name =" CREDIT_PAYMENT ", pkJoinColumns = {@PrimaryKeyJoinColumn (name =" Payment_ID ", Referenced column name = "id")) Expanding the public category credit card payment [...} @ Entity @ Dispriniter Value ("Cash") increases public category cash payment payments {...}  

No comments:

Post a Comment