一、No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor
详细报错
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.example.sbjdemo.pojo.User["car"]->com.example.sbjdemo.pojo.Car$HibernateProxy$CCqaurKR["hibernateLazyInitializer"])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.13.5.jar:2.13.5]
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1300) ~[jackson-databind-2.13.5.jar:2.13.5]
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400) ~[jackson-databind-2.13.5.jar:2.13.5]
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:46) ~[jackson-databind-2.13.5.jar:2.13.5]
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:29) ~[jackson-databind-2.13.5.jar:2.13.5]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728) ~[jackson-databind-2.13.5.jar:2.13.5]
...........
触发场景
当fetch设置为懒加载的时候(fetch = FetchType.LAZY
),进行查询操作
错误原因
car在转化为json的时候,序列化失败了(大概就是这个意思吧)
解决办法
在序列化失败的实体类上添加 @JsonIgnoreProperties(value = {"hibernateLazyInitializer"})
二、java.lang.StackOverflowError: null
详细报错
java.lang.StackOverflowError: null
at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_212]
at java.lang.ClassLoader.defineClass(ClassLoader.java:763) ~[na:1.8.0_212]
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[na:1.8.0_212]
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468) ~[na:1.8.0_212]
at java.net.URLClassLoader.access$100(URLClassLoader.java:74) ~[na:1.8.0_212]
at java.net.URLClassLoader$1.run(URLClassLoader.java:369) ~[na:1.8.0_212]
at java.net.URLClassLoader$1.run(URLClassLoader.java:363) ~[na:1.8.0_212]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_212]
触发场景
两个关联的实体类都有外键字段,且都有关联注解,如:@OneToOne、@OneToManay、@ManyToOne、@ManyToMany
错误原因
两个相关联的对象互相调用并打印,形成一个递归调用,最后堆栈溢出
解决办法
两个关联的实体中的关联对象上,都加上一个 @JsonIgnore 注解,表示在序列化的时候,忽略这个属性(网上有说@ToString(exclude={"userList"})
可以规避,但是我没有测试成功,大家可以自己试一下) 如下图所示
@OneToOne
@JoinColumn(name = "car_id")
@JsonIgnore
private Car car;
三、No class com.example.sbjdemo.pojo.User entity with id 18 exists!
详细报错
org.springframework.dao.EmptyResultDataAccessException: No class com.example.sbjdemo.pojo.User entity with id 18 exists!
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.lambda$deleteById$0(SimpleJpaRepository.java:173) ~[spring-data-jpa-2.7.10.jar:2.7.10]
at java.util.Optional.orElseThrow(Optional.java:290) ~[na:1.8.0_212]
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.deleteById(SimpleJpaRepository.java:172) ~[spring-data-jpa-2.7.10.jar:2.7.10]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_212]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_212]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:289) ~[spring-data-commons-2.7.10.jar:2.7.10]
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137) ~[spring-data-commons-2.7.10.jar:2.7.10]
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121) ~[spring-data-commons-2.7.10.jar:2.7.10]
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:530) ~[spring-data-commons-2.7.10.jar:2.7.10]
触发场景
执行数据删除操作
错误原因
删除的数据在数据库中不存在
以上就是Spring JPA 错题集解决案例的详细内容,更多关于Spring JPA 错题集的资料请关注编程网其它相关文章!