大家好,一切都好吗?我想使用findall方法和类示例获取通过过滤器的所有用户,但我总是得到一个lazyloadexception。
我已经尝试了几种方法,但如果没有这个问题,我无法使使用过滤器的分页工作。
@AllArgsConstructor
@Service("PersonService")
public class PersonService {
private PersonRepository personRepository;
private final PersonMapper personMapper ;
public Page<PersonDTO> findByExample(PersonDTO personExample, PageRequest pageOptions) {
return personRepository.findAll(Example.of(personMapper.toModel(personExample)), pageOptions).map(personMapper::toDTO);
}
}
public class Person implements Serializable{
private static final long serialVersionUID = 7358912459265900699L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name ="first_name", nullable = false)
private String firstName;
@Column(name ="last_name", nullable = false)
private String lastName;
@Column(name ="cpf",nullable = false, unique = true)
private String cpf;
@Column(name ="data_nascimento")
private LocalDate birthDate;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Phone> phones = new ArrayList<>();
@CreatedDate
@Column(name = "created_at")
private LocalDate createdAt;
}
org.hibernate.lazyinitializationexception:未能延迟初始化角色集合:one.digitalinovation.bootcampgft.personapi.model.person.phones,无法初始化代理-在org.hibernate.collection.internal.abstractpersistentcollection.ThrowlazyiInitializationException(abstractpersistentcollection.java:606)~[hibernate-core-5.4.32.final.jar:5.4.32.final]处没有会话org.hibernate.collection.internal.abstractpersistentcollection.WithTemporarySessionIneed(abstractpersistentcollection.java:218)~[hibernate-core-5.4.32.final.jar:5.4.32.final]位于org.hibernate.collection.internal.abstractpersistentcollection.readsize(abstractpersistentcollection.java:162)~[hibernate-core-5.4.32.final.jar:5.4.32.final]org.hibernate.collection.internal.persistentbag.size(persistentbag.java:371)~[hibernate-core-5.4.32.final.jar:5.4.32.final]at one.digitalinovation.bootcampgft.personapi.mapper.personmapperimpl.phonelisttophonedtolist(personmapperimpl.java:108)~[classes/:na]at在java.base/java.util.stream.referencepipeline$3$1.accept(referencepipeline.java:195)~[na:na]在java.base/java.util.arraylist$itr.ForEachRestain(arraylist.java:999)~[na:na]在java.base/java.util.spliterators$iteratorspliterator.foreachremaining(spliterators.java:1801)~[na:na]在java.base/java.util.stream.abstractpipeline.copyinto(abstractpipeline.java:484)~[na:na]在java.base/java.util.stream.abstractpipeline.wrapandcopyinto(abstractpipeline.java:474)~[na:na]在java.base/java.util.stream.reduceops$reduceops.evaluatesequential(reduceops.java:913)~[na:na]在java.base/java.util.stream.abstractpipeline.evaluate(abstractpipeline.java:234)~[na:na]在java.base/java.util.stream.referencepipeline.collect(referencepipeline.java:578)~[na:na]在org.springframework.data.domain.chunk.getconvertedcontent(chunk.java:173)~[spring-data-commons-2.5.2.jar:2.5.2]位于org.springframework.data.domain.pageimpl.map(pageimpl.java:106)~[spring-data-commons-2.5.2.jar:2.5.2]位于one.digitalinovation.bootcampgft.Persoppi.service.personservice.findbyexample(personservice.java:60)~[classes/:na]在java.base/jdk.internal.reflect.nativemethodaccessorimpl.invoke0(本机方法)~[na:na]在java.base/jdk.internal.reflect.nativemethodaccessorimpl.invoke0(本机方法)~[na:na]在java.base/jdk.internal.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62)~[na:na]java.base/jdk.internal.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)~[na:na]在java.base/java.lang.reflect.method.invoke(method.java:567)~[na:na]在org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod.java:197)~[spring-web-5.3.8]org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:141)~[spring-web-5.3.8.jar:5.3.8]位于org.springframework.web.servlet.mvc.method.annotation.ServletinInvocableHandlerMethod.invokeandhandle(ServletinInvocableHandlerMethod.java:106)~[spring-webmvc-5.3.8]org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlermethod(requestmappinghandleradapter.java:894)~[spring-webmvc-5.3.8.jar:5.3.8]位于org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:808)~[spring-webmvc-5.3.8]在org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:87)~[spring-webmvc-5.3.8.jar:5.3.8]在org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:1063)~[spring-webmvc-5.3.8.jar:5.3.8]org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:963)~[spring-webmvc-5.3.8.jar:5.3.8]在org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:1006)~[spring-webmvc-5.3.8.jar:5.3.8]在org.springframeworkframeworkervlet.web.servlet.frameworkservlet.doget(frameworkservlet.java:898)~[spring-webmvc-5.3.8.jar:5.3.8]在javax.servlet.http.httpservlet.service(httpservlet.java:655)~[tomcat-embed-core-9.0.48.jar:4.0.fr]在org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:883)~[spring-webmvc-5.3.8.jar:5.3.8]在javax.servlet.http.httpservlet.service.java:764)~[tomcat-embed-core-9.0.48.jar:4.0.fr]位于org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:228)~[tomcat-embed-core-9.0.48.jar:9.0.48]位于org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:163)~[tomcat-embed-core-9.0.48.jar:9.0.48]org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:53)~[tomcat-embed-websocket-9.0.48.jar:9.0.48]org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:190)~[tomcat-embed-core-9.0.48.jar:9.0.48]org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:163)~[tomcat-embed-core-9.0.48.jar:9.0.48]位于org.springframework.web.filter.characterencodingfilter.dofilter内部(characterencodingfilter.java:201)~[spring-web-5.3.8.jar:5.3.8]位于org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:119)~[spring-web-5.3.8.jar:5.3.8]org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:190)~[tomcat-embed-core-9.0.48.jar:9.0.48]位于org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:163)~[tomcat-embed-core-9.0.48.jar:9.0.48]org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:202)~[tomcat-embed-core-9.0.48.jar:9.0.48]在org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:97)~[tomcat-embed-core-9.0.48]在org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:542)上~[tomcat-embed-core-9.0.48.jar:9.0.48]在org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:143)~[tomcat-embed-core-9.0.48.jar:9.0.48]在org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:92)~[tomcat-embed-core-9.0.48.jar:9.0.48]在org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:78)~[tomcat-embed-core-9.0.48.jar:9.0.48]位于org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:357)~[tomcat-embed-core-9.0.48.jar:9.0.48]位于org.apache.coyote.http11.http11processor.service(http11processor.java:382)~[tomcat-embed-core-9.0.48.jar:9.0.48]在org.apache.coyote.abstractprocessorlight.process(abstractprocessorlight.java:65)~[tomcat-embed-core-9.0.48.jar:9.0.48]在org.apache.coyote.abstractprotocol$connectionhandler.process(abstractprotocol.java:893)~[tomcat-embed-core-9.0.48.jar:9.0.48]在org.apache.tomcat.util.net.niodemprocessorbase.run(socketprocessorbase.java:49)~[tomcat-embed-core-9.0.48.jar:9.0.48]在org.apache.tomcat.util.net.socketprocessorbase.run(socketprocessorbase.java:49)~[tomcat-embed-core-9.0.48.jar:9.0.48]在java.base/java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1128)~[na:na]java.base/java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:628)~[na:na]位于org.apache.tomcat.util.threads.taskthread$wrappingranable.run(taskthread.java:61)~[tomcat-embed-core-9.0.48.jar:9.0.48]位于java.base/java.lang.thread.run(thread.java:830)~[na:na]
1条答案
按热度按时间bihw5rsg1#
关于您的问题没有足够的信息,请添加更多详细信息。根据给出的信息,我建议您检查是否在一个事务有界的方法中,在另一种情况下,它应该有这个错误。
请查看此链接:https://thorben-janssen.com/lazyinitializationexception/