spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
2.98k stars 1.41k forks source link

Error accessing field ... for persistent property after migrating Spring Boot from 2.5 to 2.6 (or 2.7) #2552

Closed ftsuda-senac closed 2 years ago

ftsuda-senac commented 2 years ago

After Spring Boot migration from 2.5.14 to 2.6 (or 2.7), this error started to show when i try to save some related entities to database:

Caused by: org.springframework.orm.jpa.JpaSystemException: Error accessing field [protected java.lang.Long com.example.datajpaerror.entity.DocumentEntityId.personId] by reflection for persistent property [com.example.datajpaerror.entity.DocumentEntityId#personId] : 1; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [protected java.lang.Long com.example.datajpaerror.entity.DocumentEntityId.personId] by reflection for persistent property [com.example.datajpaerror.entity.DocumentEntityId#personId] : 1
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:331) ~[spring-orm-5.3.20.jar:5.3.20]
...
Caused by: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [protected java.lang.Long com.example.datajpaerror.entity.DocumentEntityId.personId] by reflection for persistent property [com.example.datajpaerror.entity.DocumentEntityId#personId] : 1
    at org.hibernate.property.access.spi.GetterFieldImpl.get(GetterFieldImpl.java:75) ~[hibernate-core-5.6.9.Final.jar:5.6.9.Final]
...
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field com.example.datajpaerror.entity.DocumentEntityId.personId to java.lang.Long
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) ~[na:na]

Since i'm using a quite complex Entity mapping configuration that involves inheritance and multi-column Primary Keys with EmbeddedId, i've wrote a small application in order to reproduce this error. The application can be found at https://github.com/ftsuda-senac/data-jpa-error

Steps to reproduce the error:

After downloading the application mentioned above, run as mvn test

A successful test can be achieved changing the Spring Boot version to 2.5.14 and running the same command mvn test

schauder commented 2 years ago

This doesn't seem to be a Spring Data, but a Hibernate issue.

I created a PR to your demonstrator demonstrating it. You'll need to create an issue with Hibernate.

Make sure to create a reproducer that does not use Spring, since understandably they don't appreciate to deal with other frameworks when debugging issues.

I'd appreciate it if you could leave a link here to the Hibernate issue, so others can find it more easily.

ftsuda-senac commented 2 years ago

@schauder Thanks for reply. Opened the issue https://hibernate.atlassian.net/browse/HHH-15307 and updated the demo to a pure Hibernate application,

BHRother commented 1 year ago

I stopped seeing this error after upgrading to 6.2.6.Final.

jdeflores127 commented 7 months ago

I had the same error in the version 6.4.4.Final and I notice that is a ClassLoader Issue between Hibernate and Spring Dev Tools.

First, I have this code

Captura de Pantalla 2024-02-14 a la(s) 1 44 32

And this the SessionFactory

Captura de Pantalla 2024-02-14 a la(s) 1 45 42

And this is the Entity

Captura de Pantalla 2024-02-14 a la(s) 1 46 47

This is the Properties hibernate.connection.url=jdbc:h2:mem:bookingdb hibernate.connection.username=sa hibernate.connection.password=sa hibernate.show_sql=true hibernate.format_sql=true hibernate.highlight_sql=true hibernate.hbm2ddl.auto=create

In the pom.xml I have the Spring DEV TOOLS

Captura de Pantalla 2024-02-14 a la(s) 1 49 43

When I run the App i get A similar error

Captura de Pantalla 2024-02-14 a la(s) 1 52 26

When I debug the App searching the exception I noticed that the Futbolistas object classLoader Is different that Hibernate Field classLoader. Field is generated from Hibernate with the AppClassLoader. The exception is throwed from this code fragment in the line 58.

Captura de Pantalla 2024-02-14 a la(s) 1 57 23

The Futbolistas Class was generated with the dev tool classLoader

Captura de Pantalla 2024-02-14 a la(s) 2 01 48

Reading the dev tool docs I found this

Captura de Pantalla 2024-02-14 a la(s) 2 05 15

When I drop the devtools dependency in the pom, making a mvn install, hopefully the app works fine :)

Captura de Pantalla 2024-02-14 a la(s) 2 10 00 Captura de Pantalla 2024-02-14 a la(s) 2 15 16 Captura de Pantalla 2024-02-14 a la(s) 2 16 04

So, in my case, the solution was drop the dev tools.