0
votes

Spring data is failing when attempting to access an associated HibernateProxy instance (lazy fetched association).

Error with these latest versions:

  • spring-data-2.0.6.RELEASE
  • org.hibernate:hibernate-core:5.3.0.CR2

Entity Attribute with:

@Entity @Table(name = "WORKSPACE", uniqueConstraints = @UniqueConstraint(columnNames = { "EXT_KEY", "SERVICE_ID" })) public class Workspace {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "SERVICE_ID", nullable = false, insertable = false, updatable = false)
public Service getService()

Lazy fetched associated entity Results in a HibernateProxy instance which fails with the following error:

java.lang.IllegalArgumentException: Couldn't find PersistentEntity for type class xxx.xxx.xxx.xxx.xxx.Service$HibernateProxy$zKdvHMKB!
    at org.springframework.data.mapping.context.PersistentEntities.lambda$getRequiredPersistentEntity$2(PersistentEntities.java:78) ~[spring-data-commons-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at java.util.Optional.orElseThrow(Optional.java:290) ~[na:1.8.0_111]
    at org.springframework.data.mapping.context.PersistentEntities.getRequiredPersistentEntity(PersistentEntities.java:77) ~[spring-data-commons-2.0.6.RELEASE.jar:2.0.6.RELEASE]
    at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.wrap(PersistentEntityResourceAssembler.java:72) ~[spring-data-rest-webmvc-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toResource(PersistentEntityResourceAssembler.java:55) ~[spring-data-rest-webmvc-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.lambda$null$0(RepositoryPropertyReferenceController.java:136) ~[spring-data-rest-webmvc-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at java.util.Optional.map(Optional.java:215) ~[na:1.8.0_111]
    at org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController$ReferencedProperty.mapValue(RepositoryPropertyReferenceController.java:450) ~[spring-data-rest-webmvc-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.lambda$followPropertyReference$2(RepositoryPropertyReferenceController.java:118) ~[spring-data-rest-webmvc-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.lambda$doWithReferencedProperty$16(RepositoryPropertyReferenceController.java:423) ~[spring-data-rest-webmvc-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at java.util.Optional.map(Optional.java:215) ~[na:1.8.0_111]
    at org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.doWithReferencedProperty(RepositoryPropertyReferenceController.java:420) ~[spring-data-rest-webmvc-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController.followPropertyReference(RepositoryPropertyReferenceController.java:144) ~[spring-data-rest-webmvc-3.0.6.RELEASE.jar:3.0.6.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]

Works with earlier versions

  • spring-data-1.5.12.RELEASE
  • org.hibernate:hibernate-core:5.0.4.Final

Temporary workaround - set the associated entity fetch type to EAGER

@ManyToOne(fetch = FetchType.EAGER)
1
Just wondering hw this class structure and annotated - Couldn't find PersistentEntity for type class xxx.xxx.xxx.xxx.xxx.Service ? It says cant find persistent entity. Can you post some snippet?Karthik R
I suspect this is related to jira.spring.io/projects/SPR/issues/SPR-16569 can you downgrade to an older Hibernate version?Jens Schauder
@KarthikR - added annotations to question - however I think Jens Schauder has solved it.Nathan Coast
@JensSchauder - yes - you've identified it thanks! it's the switch from javassist to ByteBuddy in Hibernate 5.3 that is incompatible with spring. I'll follow up with the details / workaround.Nathan Coast

1 Answers

0
votes

Known incompatibility between spring-data and hibernate 5.3 https://jira.spring.io/projects/SPR/issues/SPR-16569

Solution 1) downgrade hibernate version to 5.2 or earlier.

Solution 2) set hibernate to use javassist instead of ByteBuddy

hibernate.bytecode.provider=javassist

Thanks Jens Schauder