question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid hibernate proxy entity id

See original GitHub issue

When trying to delete an entity, nothing happens because spring is accessing the id directly, and because it is a hibernate proxy object, the member has default 0 value, thus it thinks it is a new entity and skips altogether the delete.

From debugging, this is the relevant part of the callstack:

getField:674, ReflectionUtils (org.springframework.util)
getPropertyValue:63, DirectFieldAccessFallbackBeanWrapper (org.springframework.data.util)
getId:154, JpaMetamodelEntityInformation (org.springframework.data.jpa.repository.support)
isNew:42, AbstractEntityInformation (org.springframework.data.repository.core.support)
isNew:233, JpaMetamodelEntityInformation (org.springframework.data.jpa.repository.support)
delete:170, SimpleJpaRepository (org.springframework.data.jpa.repository.support)

At the top, in ReflectionUtils::getField(Field,Object) method, the target variable is of type class package.Class$HibernateProxy$HWnaDQWN, and field is private int package.Class.id.

Note that accessing the id by method target.getId() returns non-zero id, as expected.

The id is defined inside the entity as follows:

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

Spring Boot 2.1.6.RELEASE

  • Spring-core 5.1.8.RELEASE
  • Hibernate-core 5.3.10.Final
  • Spring-data-commons 2.1.9.RELEASE
  • Spring-data-jpa 2.1.9.RELEASE

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cdalexndrcommented, Jul 19, 2019
1reaction
mp911decommented, Jul 19, 2019

We should move that ticket to JIRA, here’s probably not the right place. I think we can improve on that end to align with JPA access rules and fetch the Id value in a better way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jpa - How to avoid initializing a Hibernate proxy when only the ...
I've tried adding @Access(AccessType.PROPERTY) like: @Entity class Continent( @Id @Access(AccessType.
Read more >
How to Convert a Hibernate Proxy to a Real Entity Object
In this tutorial, we'll learn how to convert a Hibernate proxy to a real entity object.
Read more >
Session (Hibernate JavaDocs) - Red Hat on GitHub
Return the persistent instance of the given entity class with the given identifier, or null if there is no such persistent instance.
Read more >
How does a JPA Proxy work and how to unproxy it with ...
Learn how JPA and Hibernate Proxy objects work, and how you can unproxy an entity Proxy to get access to the underlying POJO...
Read more >
hibernate-orm/LazyInitializer.java at main - proxy - GitHub
void initialize() throws HibernateException;. /**. * Retrieve the identifier value for the entity our owning proxy represents.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found