gpt4 book ai didi

java - 为什么每次调用findById()时Calendar字段都会自动增加

转载 作者:行者123 更新时间:2023-12-02 01:33:49 25 4
gpt4 key购买 nike

当我使用spring-boot 2.1.7.release和mysql 5.6编码时,出现一些错误。过程如下:

首先,我将一个实体保存到数据库中。该实体有一个名为 applyTime 的字段,类型为 Calendar然后,调用findById()。最后,applyTime 会自动增加一些数字。

示例:保存值的timeInMillis为2000000L,findById()返回值的timeInMillis为70400000。

当更新实体的其他字段时,日历字段将保留新值。

我创建了一个带有示例两个类的圆顶,即Student和StudentRepository。并且错误也出现了。

实体

@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;

private Calendar applyTime;

// setter and getter ...
}

DAO

public interface StudentRepository extends CrudRepository<Student, Long> {
}

测试

    @Test
public void test() {
Student student = new Student();
Calendar calendar = Calendar.getInstance();
long time = 20000000L;
/** init with 20,000,000 */
calendar.setTimeInMillis(time);
student.setApplyTime(calendar);

/** save entity with: 20,000,000 (1970/1/1 13:33:20 +8:00). database: 1970-01-01 13:33:20 +8:00 */
studentRepository.save(student);

Student student1 = studentRepository.findById(student.getId()).get();
Assertions.assertThat(student1.getApplyTime().getTimeInMillis()).isGreaterThan(time);

/** get 70400000 (1970/1/2 3:33:20 +8:00) , database: 1970-01-01 13:33:20 (+8:00)*/
time = student1.getApplyTime().getTimeInMillis();
logger.warn(String.valueOf(time));

student1.setName("hi");
studentRepository.save(student1);

student1 = studentRepository.findById(student.getId()).get();
Assertions.assertThat(student1.getApplyTime().getTimeInMillis()).isGreaterThan(time);
time = student1.getApplyTime().getTimeInMillis();

/** get 120800000(1970/1/2 17:33:20 +8:00), database: 1970-01-02 03:33:20 you may get other auto increase value*/
logger.warn(String.valueOf(time));

student1.setName("hello");
studentRepository.save(student1);

student1 = studentRepository.findById(student.getId()).get();
Assertions.assertThat(student1.getApplyTime().getTimeInMillis()).isGreaterThan(time);
time = student1.getApplyTime().getTimeInMillis();

/** get 171200000(1970/1/3 7:33:20 +8:00) database: 1970-01-02 17:33:20 +8:00*/
logger.warn(String.valueOf(time));
}

mysql

version: '3.1'

services:

db:
image: mysql:5.6
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: example
volumes:
- ./mysql:/var/lib/mysql
ports:
- 3316:3306

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

应用程序.yml

# 测试环境
spring:
jpa:
hibernate:
ddl-auto: create-drop
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
show-sql: true
datasource:
url: jdbc:mysql://127.0.0.1:3316/example?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: example
separator: //

logging:
level:
org:
hibernate:
type:
descriptor:
sql:
BasicBinder: TRACE
debug: false

github:https://github.com/mengyunzhi/jpa-calendar-error

如何禁用自动增加日历字段?谢谢!

最佳答案

当您将其中一个字段定义为日历类型时,您需要对其进行相应的注释,以防止出现任何不一致:

@Temporal(TemporalType.DATE)
private Calendar applyTime;

@Temporal(TemporalType.TIMESTAMP)
private Calendar applyTime;

关于java - 为什么每次调用findById()时Calendar字段都会自动增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57538016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com