每次启动Sping Boot 应用程序时,Hibernate都会更改表

9njqaruj  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(195)

每次我启动我的spring Boot 应用程序时,hibernate都会改变我所有表中的一些字段。这种交替只发生在Timestamp和Date类型(如LocalDateLocalDateTime)上,而不会发生在其他字段(如int、String或其他对象)上。Hibernate将数据类型设置为Timestamp,尽管它已经定义为Timestamp。

  • 使用:Java 17、PostgreSQL 15.3、Sping Boot 3.1.1*
    控制台
Hibernate: alter table if exists public.users alter column deleted_at set data type timestamp(6)
Hibernate: alter table if exists public.users alter column updated_at set data type timestamp(6)

字符串

Application.yml

datasource:
    url: ${DATASOURCE_URL:jdbc:postgresql://localhost:5432/ddltest}
    username: ${DB_USERNAME:postgres}
    password: ${DB_PASSWORD:root}
  jpa:
    show-sql: true
    properties:
      hibernate:
        default_schema: public
        enable_lazy_load_no_trans: true
        jdbc:
          time_zone: UTC
    open-in-view: false
    generate-ddl: true
server:
  port: ${API_PORT:8080}
  servlet:
    context-path: /api/v1

实体类

@Table(name = "users")
@Getter
@Setter
public class User{

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "email")
    private String email;

    @Column(name = "password")
    private String password;
    
    @Column(name = "deleted_at")
    private Timestamp deletedAt;

    @Column(name = "updated_at")
    private Timestamp updatedAt;
    
}

POM.XML

<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 https://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>3.1.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.vojvoda</groupId>
    <artifactId>ecommerce-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ecommerce-api</name>
    <description>ecommerce-api</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-api</artifactId>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-impl</artifactId>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-jackson</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt-api</artifactId>
                <version>0.11.5</version>
            </dependency>
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt-impl</artifactId>
                <version>0.11.5</version>
            </dependency>
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt-jackson</artifactId>
                <version>0.11.5</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

t30tvxxf

t30tvxxf1#

首先,在配置中定义了generate-ddl: true。这允许JPA根据您的实体定义更改数据库模式。如果你的数据库已经初始化了,你可以把它设置为false。
其次,可以使用@Temporal定义数据库中date/datetime/timestamp值的精度。如果只有日期的精度,那么可以使用@Temporal(TemporalType.DATE)来定义数据库列的精度。例如,您可以使用具有相应精度的Java类。

@Temporal(TemporalType.DATE)
private java.util.Date updatedAt;

字符串

相关问题