【发布时间】:2021-11-04 04:09:29
【问题描述】:
我正在尝试使用 Spring data JPA 中的 MIN 聚合函数获取最早的日期,它在我的 IDE 控制台上引发空指针异常,但是当我尝试从数据库中获取它时,我能够成功获取日期在数据库上。
我的POJO课是这样的, `@实体 @ConfigurationProperties("动作日志") @Table(name = "action_log") 公共类 ActionLog {
@Id
@NotBlank
private String id;
@NotNull
@Column(name = "job_no")
private int jobNo;
@NotNull
private Date date;
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private Staff staff;
@NotNull
@Column(name = "previous_status")
private JobStatus previousStatus;
@NotNull
@Column(name = "new_status")
private JobStatus newStatus; `
我的仓库界面是这样的,
public interface ActionLogRepository extends JpaRepository<ActionLog, String> {
@Query(value = "SELECT MIN(a.date) FROM action_log a where (a.previous_status = 4) AND (a.job_no = ?1) AND (a.user_id = ?1)", nativeQuery = true)
public Date getMinDateByJobNoAndUserId(int jobNo, String id);
}`
我的Service类是这样的,
`
@Service
@EnableConfigurationProperties(ActionLog.class)
public class ActionLogService {
@Autowired
ActionLogRepository actionLogRepository;
public void add(ActionLog actionLog) {
actionLogRepository.save(actionLog);
}
public void update(ActionLog actionLog) {
actionLogRepository.saveAndFlush(actionLog);
}
public ActionLog get(String id) throws PaperTrueActionLogNotFoundException {
Optional<ActionLog> actionLog = actionLogRepository.findById(id);
if (!actionLog.isPresent()) {
throw new PaperTrueActionLogNotFoundException("No Action Log found for this id");
}
return actionLog.get();
}
public ActionLog get(int jobNo, String userId) throws PaperTrueJobNotFoundException {
ActionLog actionLog = actionLogRepository.findByJobNoAndStaffId(jobNo, userId);
if(actionLog == null){
throw new PaperTrueJobNotFoundException("No Action log found for this jobNo");
}
return actionLog;
}
public void delete(ActionLog actionLog) {
actionLogRepository.delete(actionLog);
}
public Date getMinDateByJobNoAndUserId(int jobNo, String id) throws PaperTrueJavaException {
return actionLogRepository.getMinDateByJobNoAndUserId(jobNo, id);
}
} `
这是堆栈跟踪,
----------------------Get min date from action log by job No-----------------------
Hibernate:
SELECT
MIN(a.date)
FROM
action_log a
where
(
a.previous_status = 4
)
AND (
a.job_no = ?
)
AND (
a.user_id = ?
)
2021-09-08 11:21:48.801 TRACE 11897 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [INTEGER] - [87207]
2021-09-08 11:21:48.801 TRACE 11897 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [INTEGER] - [87207]
2021-09-08 11:21:49.082 TRACE 11897 --- [ main] o.h.type.descriptor.sql.BasicExtractor : extracted value ([MIN(a.date)] : [TIMESTAMP]) - [null]
2021-09-08 11:21:49.087 DEBUG 11897 --- [ main] o.h.stat.internal.StatisticsImpl : HHH000117: HQL: SELECT MIN(a.date) FROM action_log a where (a.previous_status = 4) AND (a.job_no = ?) AND (a.user_id = ?), time: 580ms, rows: 1
2021-09-08 11:21:49.091 INFO 11897 --- [ main] i.StatisticalLoggingSessionEventListener : Session Metrics {
259636242 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
287693891 nanoseconds spent preparing 1 JDBC statements;
268222720 nanoseconds spent executing 1 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
}
java.lang.NullPointerException
at com.papertrue.ActionLogServiceTest.contextLoads(ActionLogServiceTest.java:86)
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.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:212)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:208)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
ActionLogServiceTest 类是这样的,
@SpringBootTest
public class ActionLogServiceTest {
@Autowired
ActionLogService actionLogService;
@Autowired
StaffService staffService;
@Test
public void contextLoads() throws PaperTrueJobNotFoundException, PaperTrueJavaException, PaperTrueStaffNotFoundException {
System.out.println("----------------------Get min date from action log by job No-----------------------");
java.util.Date minDateByJobNoAndUserId = actionLogService.getMinDateByJobNoAndUserId(87207, "e734edc0-d47e-4d1a-baa0-a20f935f7d70");
System.out.println("Min date" + minDateByJobNoAndUserId.toString());
System.out.println("----------------------Get min date from action log by job No-----------------------");
}
}
【问题讨论】:
-
您能发布 StackTrace 吗?日期列中有空值吗?
-
不,我在日期列中没有任何空值,当我尝试使用 mysql 数据库中的查询获取(最小)日期时,我能够获取日期,但是当我正在使用 JPA,然后它在控制台中显示空值
-
所以请发布 NullPointerException 的完整堆栈跟踪
-
嗨西蒙,我在问题中添加了堆栈跟踪
-
这不是堆栈跟踪。堆栈跟踪是包含所有信息的异常
标签: java mysql spring-boot jpa spring-data-jpa