【问题标题】: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作为默认值

标签: mysql hibernate spring-boot gradle kotlin


【解决方案1】:

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

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

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

【讨论】:

  • 不,我对新值有疑问。当我添加新列时,我希望所有新列值都等于 UNKNOWN 而不是 null。但我得到了空值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-26
  • 1970-01-01
  • 2020-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多