我正在尝试向现有表中添加新列。应该是未知的,但所有行的值都为null。请帮忙。这是我的密码。
0.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的表。现在我想将studentstatus var添加到表中,我发现我必须在gradle.kt中将它声明为studentstatus.unknow,但是它会变为null。
学生状态.kt
package com.example.demo.model
enum class StudentStatus{
PASS,
FAIL,
UNKNOWN
}
渐变存储.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')
}
1条答案
按热度按时间aoyhnmkz1#
当您在类上定义附加属性时,hibernate可以向现有表中添加新列。但是hibernate不会自动迁移现有的值/行。
对于这种情况,应该使用liquibase或flyway之类的数据库迁移工具。Spring Boot支持两个。这些工具可以帮助您编写在下一次应用程序启动时超过一次的迁移,并使用缺少的值更新数据库中的现有条目。
我假设只有现有值存在问题,因为定义的默认值对于新插入的值看起来是正确的。