spring-data-2.0.6 java.lang.IllegalArgumentException:无法找到类型为HibernateProxy

时间:2018-05-02 21:55:24

标签: java hibernate spring-boot spring-data spring-data-jpa

尝试访问关联的HibernateProxy实例(延迟提取的关联)时,Spring数据失败。

这些最新版本出错:

  • 弹簧数据2.0.6.RELEASE
  • org.hibernate作为:休眠核:5.3.0.CR2

实体属性:

@Entity @Table(name =" WORKSPACE",uniqueConstraints = @UniqueConstraint(columnNames = {" EXT_KEY"," SERVICE_ID"})) 公共类工作区{

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

Lazy fetched HibernateProxy实例中的关联实体结果失败并出现以下错误:

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]

适用于早期版本

  • 弹簧数据1.5.12.RELEASE
  • org.hibernate作为:休眠核:5.0.4.Final

临时解决方法 - 将关联的实体提取类型设置为EAGER

@ManyToOne(fetch = FetchType.EAGER)

1 个答案:

答案 0 :(得分:0)

spring-data和hibernate 5.3之间已知的不兼容性 https://jira.spring.io/projects/SPR/issues/SPR-16569

解决方案1)将hibernate版本降级到5.2或更早版本。

解决方案2)将hibernate设置为使用javassist而不是ByteBuddy

hibernate.bytecode.provider =了Javassist

谢谢Jens Schauder