У меня проблема с методом findAll () в слое DAO. Наконец, он получает все объекты правильно, но имеет некоторую ошибку с "null" в процессе.
Например, в классе Group есть поле Faculty, Faculty - POJO:
@Entity
@Table(name = "groups")
public class Group implements ModelEntity {
@Id
@Column(name = "group_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "groups_seq")
@SequenceGenerator(name = "groups_seq", sequenceName = "groups_group_id_seq", allocationSize = 1)
private int id;
@Column(name = "group_name")
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "faculty_id")
private Faculty faculty;
вот findAll () в dao:
@SuppressWarnings("unchecked")
public List<T> findAll() {
logger.debug("findAll()");
EntityManager entityManager = entityManagerFactory.createEntityManager();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<T> criteriaQuery = (CriteriaQuery<T>) criteriaBuilder.createQuery(getEntityClass());
Root<T> rootEntry = (Root<T>) criteriaQuery.from(getEntityClass());
CriteriaQuery<T> all = criteriaQuery.select(rootEntry);
TypedQuery<T> allQuery = entityManager.createQuery(all);
List<T> result = allQuery.getResultList();
logger.trace("findAll() found {} entries.", result.size());
return result;
}
Когда I go на сайте в / groups / getAll браузер отображает все группы и факультеты в своих столбцах, все в порядке, но в консоли я вижу java.lang.NullPointerException: null
, которые начинаются с List<T> result = allQuery.getResultList();
, когда я выбираю FetchType.EAGER такая же ситуация - все записи здесь, но NPE в консоли. Как обыграть NPE в консоли?
логи выглядят так:
java.lang.NullPointerException: null
at org.hibernate.sql.ast.SqlTreePrinter.lambda$visitQuerySpec$18(SqlTreePrinter.java:262)
at org.hibernate.sql.ast.SqlTreePrinter.logNode(SqlTreePrinter.java:201)
at org.hibernate.sql.ast.SqlTreePrinter.logNode(SqlTreePrinter.java:190)
at org.hibernate.sql.ast.SqlTreePrinter.visitQuerySpec(SqlTreePrinter.java:246)
at org.hibernate.sql.ast.SqlTreePrinter.lambda$visitStatement$0(SqlTreePrinter.java:90)
at org.hibernate.sql.ast.SqlTreePrinter.logNode(SqlTreePrinter.java:201)
at org.hibernate.sql.ast.SqlTreePrinter.logNode(SqlTreePrinter.java:190)
at org.hibernate.sql.ast.SqlTreePrinter.visitStatement(SqlTreePrinter.java:88)
at org.hibernate.sql.ast.SqlTreePrinter.print(SqlTreePrinter.java:75)
at org.hibernate.sql.ast.spi.StandardSqlAstSelectTranslator.translate(StandardSqlAstSelectTranslator.java:85)
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.buildCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:232)
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.resolveCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:193)
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:153)
at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:437)
at org.hibernate.query.spi.AbstractQuery.list(AbstractQuery.java:1356)
at org.hibernate.query.Query.getResultList(Query.java:136)
at com.rumakin.universityschedule.dao.Dao.findAll(Dao.java:71)
at com.rumakin.universityschedule.service.AuditoriumService.findAll(AuditoriumService.java:35)
at com.rumakin.universityschedule.controllers.AuditoriumController.findAll(AuditoriumController.java:37)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1590)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:834)
03:21:08.012 [http-nio-8080-exec-12] DEBUG org.hibernate.orm.sql.ast.tree -
SQL AST Tree:
-> [select-statement]
-> [query-spec]
-> [select]
selection - org.hibernate.sql.results.internal.SqlSelectionImpl@d1311e6a
selection - org.hibernate.sql.results.internal.SqlSelectionImpl@429797b9
selection - org.hibernate.sql.results.internal.SqlSelectionImpl@78d44e89
selection - org.hibernate.sql.results.internal.SqlSelectionImpl@ea2bc8b8
<- [select]
-> [from]
-> [table-group]
-> [Auditorium as a1_0]
<- [table-group]
<- [from]
<- [query-spec]
<- [select-statement]