Hibernate Inheritance and Level 2 Layer Proxies

A simple application does not work correctly with the following entity structure

@Entity(name="some_table")
@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn( name="TYPE", discriminatorType=DiscriminatorType.STRING )
abstract class EntityBase {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column
    private int id;
}

@Entity
@DiscriminatorValue("EntityA")
@Cacheable
class EntityA extends EntityBase {
    @Column
    private int aColumn;
...
}

@Entity
@DiscriminatorValue("EntityB")
@Cacheable
class EntityB extends EntityBase {
    @Column
    private int bColumn;
...
}

@Entity(name="holder_table")
@Cacheable
class HolderEntity {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column
    private int id;

    @ManyToOne(fetch=FetchType.LAZY)
    EntityBase holdedEntity;

    ...
}

For first boot or without cache, everything works well

After loading the HolderEntity instance from the cache, holdedEntity initialized with an entity of type EntityBase (abstract class).

pseudo code:

def a = HolderEntity.get(1)
assert a.holdedEntity.class!=EntityBase //ok
a = HolderEntity.get(1) // load from cache
assert a.holdedEntity.class!=EntityBase //fails (actually EntityBase_$$_jvstbbe_0)

hibernate : ( EntityBase) (final Type [] types = subclassPersister.getPropertyTypes(), DefaultLoadEventListener)
SessionImpl.internalLoad(String entityName, Serializable id, boolean eager, boolean nullable) witch "" init

holdedEntity , AbstractEntityPersister.EntityMetamodel. , ,

L2 ?

4.3.8 (4.3.6 4.3.11 )

:

+4
1

, Hibernate . -, @Proxy :

@Proxy(lazy=false)

GitHub, , @Proxy(lazy=false) .

+3

Source: https://habr.com/ru/post/1609355/


All Articles