为啥添加一列时所有行都得到空值?

Posted

技术标签:

【中文标题】为啥添加一列时所有行都得到空值?【英文标题】:Why do when adding a column all rows get the value of null?为什么添加一列时所有行都得到空值? 【发布时间】:2019-05-31 05:30:07 【问题描述】:

我正在尝试向现有表中添加一个新列。预计为 UNKNOWN,而不是所有行都获得 null 值。请帮忙。这是我的代码。

等级.kt

package com.example.demo.model

import javax.persistence.*

@Entity
class Grade 
    @GeneratedValue
    @Id
    var id: Long? = null
    var grade: Int? = null
    var name: String? = null
    var enabled: Boolean = false
    @Enumerated(EnumType.STRING)
    var studentStatus: StudentStatus = StudentStatus.UNKNOWN


首先我创建了这个没有学生状态的表。现在我想将 studentStatus var 添加到表中,我发现我必须在 Gradle.kt 中将其声明为 StudentStatus.UNKNOW 但它却变为 null。

StudentStatus.kt

package com.example.demo.model

enum class StudentStatus
    PASS,
    FAIL,
    UNKNOWN

GradleRepository.kt

package com.example.demo.repository

import com.example.demo.model.Grade
import org.springframework.data.repository.CrudRepository

interface GradeRepository : CrudRepository<Grade, Long>

构建.gradle

buildscript 
    ext 
        kotlinVersion = '1.2.71'
        springBootVersion = '2.1.1.RELEASE'
    
    repositories 
        mavenCentral()
    
    dependencies 
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
        classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlinVersion")
        classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlinVersion")
    


apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'kotlin-jpa'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
compileKotlin 
    kotlinOptions 
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    

compileTestKotlin 
    kotlinOptions 
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    


repositories 
    mavenCentral()



dependencies 
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
    implementation('org.springframework.boot:spring-boot-starter-mustache')
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('com.fasterxml.jackson.module:jackson-module-kotlin')
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    runtimeOnly('com.h2database:h2')
    runtimeOnly('mysql:mysql-connector-java')
    testImplementation('org.springframework.boot:spring-boot-starter-test')

【问题讨论】:

你需要告诉hibernatestudentStatus有一个默认值,否则它会使用NULL作为默认值 【参考方案1】:

当您在类上定义附加属性时,Hibernate 可以向现有表添加新列。但是 Hibernate 不会自动迁移现有的值/行。

对于这种情况,您应该使用Liquibase 或Flyway 等数据库迁移工具。 Spring Boot supports both。这些工具可帮助您编写一个迁移,该迁移会在下一次应用程序启动时使用缺失值更新数据库中的现有条目。

我假设您只对现有值有问题,因为定义的默认值对于新插入的值看起来是正确的。

【讨论】:

不,我对新值有疑问。当我添加新列时,我希望所有新列值都等于 UNKNOWN 而不是 null。但我得到了空值。

以上是关于为啥添加一列时所有行都得到空值?的主要内容,如果未能解决你的问题,请参考以下文章

在 jquery 的自动完成中添加具有空值的自定义项

猫鼬帮助,为啥我得到空值?

在 Unix shell 中添加一列数字

在当前日期添加一年 PYTHON

为啥我在 Flutter 中使用 Firestore 中的 foreach 方法后得到空值?

R - 为啥向数据表添加 1 列几乎会使使用的峰值内存增加一倍?