【发布时间】: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')
}
【问题讨论】:
-
你需要告诉hibernate
studentStatus有一个默认值,否则它会使用NULL作为默认值
标签: mysql hibernate spring-boot gradle kotlin