I have a hierarchy in my domain model, which is described by classes:
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class BaseEntity {
@Id
private Long id;
// other fields
}
@DiscriminatorValue(value = "Individual")
public class IndividualEntity extends BaseEntity {
// fields
}
@DiscriminatorValue(value = "Branch")
public class BranchEntity extends BaseEntity {
// fields
}
I'm fetching objects like this:
Specification<BaseEntity> specification = createSpecification();
BaseEntity entity = baseRepository.findOne(specification);
(I'm using spring-data)
The problem is that Hibernate returns proxy object (what I understand), but the proxy is of BaseEntity
, not the proper subclass (its' class is BaseEntity_$$_jvsted9_26
, therefore entity instanceof IndividualEntity
is false).
What is interesting, not all objects are returned as proxy.
I'm fetching entities in loop (common transaction), some of them are returned in normal form (i.e. IndividualEntity
/BranchEntity
), some as proxies.
If I change mechanism, so that every fetch is done in separate transaction - no proxy objects are returned at all.
I know that I can unwrap that proxy (e.g. like here), but what is the reason for such behaviour (kinda strange for me) and can I avoid it?