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
java spring-data-jpa orm hibernate


source to share


No one has answered this question yet

See similar questions:

152
How to convert Hibernate proxy to real object
2
JPA EntityManager search method returns proxy

or similar:

1636
Java inner class and static nested class
996
What are the possible Hibernate hbm2ddl.auto config values ​​and what do they do
636
What's the difference between JPA and Hibernate?
341
Create the perfect JPA object
189
Creating a OneToOne to lazy relationship
thirty
What does the Hibernate proxy object contain?
7
Hibernation Polymorphism: Creating the Right Class
3
Hibernation lazy loading, proxy and inheritance
2
SIngle table strategy does not work. Sleep mode
1
Hibernate: return proxy object in case of sessionload



All Articles
Loading...
X
Show
Funny
Dev
Pics