2 JPA entities on the same table

后端 未结 1 693
面向向阳花
面向向阳花 2020-12-09 15:56

Let\'s say I\'ve a table with 200 columns and most of them are never used.

I map SmallEntity to the 10 columns that are used often. I use it in the associations with

相关标签:
1条回答
  • 2020-12-09 16:16

    The most straightforward way to do this is to map properties you don't use often as lazy:

    <property name="extendedProperty" lazy="true" />
    
    ... or using Annotations ...
    
    @Basic(fetch = FetchType.LAZY)
    String getExtendedProperty() { ... }
    

    Hibernate would not load such properties initially; instead they'll be loaded on demand (when first accessed). You can force Hibernate to load all properties by using fetch all properties clause in your HQL query.

    Another possible scenario is to actually map two completely separate entities to the same table but make one of them immutable. Keep in mind that they will be treated as different entities by Hibernate, with first / second level cache being completely separate for both (which is why immutability is important).

    You will NOT be able to achieve this functionality via inheritance mapping because Hibernate always returns an actual concrete entity type. Take a look at my answer to Hibernate Inheritance Strategy question for a detailed explanation.

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