Java Spring数据jdbc微秒精度在插入LocalDateTime类型变量期间丢失
在我看来,当我们运行Java Spring数据jdbc微秒精度在插入LocalDateTime类型变量期间丢失,java,spring,spring-data,spring-data-jdbc,localdatetime,Java,Spring,Spring Data,Spring Data Jdbc,Localdatetime,在我看来,当我们运行repository.save(entity)并且该实体有一个LocaldateTime变量映射到timestamp(6)类型的列时,spring数据jdbc(或某个底层库)将插入该列的数据四舍五入到3个小数点并加上零 下面是我计划插入的简单的客户订单实体 public class CustomerOrder { @Id String id; LocalDateTime createdAt; } 这是模式 CREATE TABLE IF NOT EXISTS custo
repository.save(entity)
并且该实体有一个LocaldateTime
变量映射到timestamp(6)
类型的列时,spring数据jdbc(或某个底层库)将插入该列的数据四舍五入到3个小数点并加上零
下面是我计划插入的简单的客户订单
实体
public class CustomerOrder {
@Id
String id;
LocalDateTime createdAt;
}
这是模式
CREATE TABLE IF NOT EXISTS customer_order (
id char(36) NOT NULL PRIMARY KEY ,
created_at timestamp(6)
);
CRUD repo接口扩展
@Repository
public interface OrderRepository extends CrudRepository<CustomerOrder, String> {
}
测试用例
@RunWith(SpringRunner.class)
@SpringBootTest
class CustomerOrderRepositoryTest {
@Autowired
OrderRepository repository;
@Test
public void testFind() {
// create entity to insert
CustomerOrder expectedCustomerOrder = new CustomerOrder();
expectedCustomerOrder
.setCreatedAt(LocalDateTime.now().truncatedTo(ChronoUnit.MICROS));
// run the insert
repository.save(expectedCustomerOrder);
// run findall to get everything
List<CustomerOrder> allActualOrders = StreamSupport.stream(repository.findAll().spliterator(), false).collect(Collectors.toList());
//validate data
assertEquals(1, allActualOrders.size());
assertEquals(expectedCustomerOrder, allActualOrders.get(0));
}
}
@RunWith(SpringRunner.class)
@春靴测试
类CustomerOrderRepositoryTest{
@自动连线
订单存储库;
@试验
公共void testFind(){
//创建要插入的实体
CustomerOrder expectedCustomerOrder=新CustomerOrder();
预期客户订单
.setCreatedAt(LocalDateTime.now().truncatedTo(ChronoUnit.MICROS));
//运行插入
repository.save(expectedCustomerOrder);
//运行findall以获取所有信息
List allActualOrders=StreamSupport.stream(repository.findAll().spliterator(),false).collect(collector.toList());
//验证数据
assertEquals(1,allActualOrders.size());
assertEquals(expectedCustomerOrder,allActualOrders.get(0));
}
}
测试用例输出
org.opentest4j.AssertionFailedError:
预期:客户订单(id=c8a9fc0e-ee9a-4798-b273-323b3a922a43,创建日期=2020-11-18T10:44:03.390097)
实际:客户订单(id=c8a9fc0e-ee9a-4798-b273-323b3a922a43,创建日期=2020-11-18T10:44:03.390)
位于org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
位于org.junit.jupiter.api.AssertionUtils.failNotEqual(AssertionUtils.java:62)
位于org.junit.jupiter.api.AssertEquals.AssertEquals(AssertEquals.java:182)
位于org.junit.jupiter.api.AssertEquals.AssertEquals(AssertEquals.java:177)
位于org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1124)
在com.test.sample.sdj.CustomerOrderRepositoryTest.testFind上(CustomerOrderRepositoryTest.java:36)
位于java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
位于java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
位于java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
位于java.base/java.lang.reflect.Method.invoke(Method.java:566)
位于org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:686)
位于org.junit.jupiter.engine.execution.MethodInvocation.procedure(MethodInvocation.java:60)
位于org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.Procedue(InvocationInterceptorChain.java:131)
位于org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
位于org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
位于org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
位于org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
位于org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
位于org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.Procedue(InvocationInterceptorChain.java:106)
在org.junit.jupiter.engine.execution.InvocationInterceptorChain.procedure(InvocationInterceptorChain.java:64)上
位于org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
位于org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
位于org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
位于org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:212)
位于org.junit.platform.engine.support.hierarchy.ThrowableCollector.execute(ThrowableCollector.java:73)
位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:208)
位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)
位于org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)
位于org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$ExecuteCursive$5(NodeTestTask.java:135)
位于org.junit.platform.engine.support.hierarchy.ThrowableCollector.execute(ThrowableCollector.java:73)
位于org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$executecurively$7(NodeTestTask.java:125)
位于org.junit.platform.engine.support.hierarchy.Node.around(Node.java:135)
位于org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$ExecuteCursive$8(NodeTestTask.java:123)
位于org.junit.platform.engine.support.hierarchy.ThrowableCollector.execute(ThrowableCollector.java:73)
在org.junit.platform.engine.support.hierarchy.NodeTestTask.ExecuteCursive上(NodeTestTask.java:122)
位于org.junit.platform.engine.support.hierarchy.NodeTestTask.execute(NodeTestTask.java:80)
位于java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
位于org.junit.platform.engine.support.hierarchy.samethreadherarchycalTestExecutorService.invokeAll(samethreadherarchycalTestExecutorService.java:38)
位于org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$ExecuteCursive$5(NodeTestTask.java:139)
位于org.junit.platform.engine.support.hierarchy.ThrowableCollector.execute(ThrowableCollector.java:73)
位于org.junit.platform.engine.support.hierarch
@SpringBootApplication
public class SampleApp {
public static void main(String[] args) {
SpringApplication.run(SampleApp.class, args);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
class CustomerOrderRepositoryTest {
@Autowired
OrderRepository repository;
@Test
public void testFind() {
// create entity to insert
CustomerOrder expectedCustomerOrder = new CustomerOrder();
expectedCustomerOrder
.setCreatedAt(LocalDateTime.now().truncatedTo(ChronoUnit.MICROS));
// run the insert
repository.save(expectedCustomerOrder);
// run findall to get everything
List<CustomerOrder> allActualOrders = StreamSupport.stream(repository.findAll().spliterator(), false).collect(Collectors.toList());
//validate data
assertEquals(1, allActualOrders.size());
assertEquals(expectedCustomerOrder, allActualOrders.get(0));
}
}
org.opentest4j.AssertionFailedError:
Expected :CustomerOrder(id=c8a9fc0e-ee9a-4798-b273-323b3a922a43, createdAt=2020-11-18T10:44:03.390097)
Actual :CustomerOrder(id=c8a9fc0e-ee9a-4798-b273-323b3a922a43, createdAt=2020-11-18T10:44:03.390)
<Click to see difference>
at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
at org.junit.jupiter.api.AssertionUtils.failNotEqual(AssertionUtils.java:62)
at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177)
at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1124)
at com.test.sample.sdj.CustomerOrderRepositoryTest.testFind(CustomerOrderRepositoryTest.java:36)
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:1540)
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:1540)
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:69)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)