Hibernate returns proxy for base class

I have a hierarchy in my domain model that 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 get objects like this:

Specification<BaseEntity> specification = createSpecification();
BaseEntity entity = baseRepository.findOne(specification);

      

(I am using spring-data)

The problem is that Hibernate is returning a proxy object (which I understand), but the proxy has BaseEntity

, and not the corresponding subclass (its class BaseEntity_$$_jvsted9_26

, therefore entity instanceof IndividualEntity

false).

Interestingly, not all objects are returned as proxies. I get objects in a loop (general transaction), some are returned in normal form (i.e. IndividualEntity

/ BranchEntity

), some are returned as proxies. If I change the mechanism so that each fetch is performed in a separate transaction, no proxy objects are returned at all.

I know I can deploy this proxy (for example here ), but what is the reason for this behavior (something strange to me) and I can avoid it

+3


source to share





All Articles