我正在使用带有Kotlin和Hibernate的Spring Boot 2构建应用程序。 当我运行任何测试时,一切似乎都正确。
但是,当我尝试使用IDEA Spring Boot Run配置运行应用程序时,在使用位置出现以下错误:
java.lang.IllegalStateException: ServiceEntity_.alias must not be null
ServiceEntity_
是使用hibernate-jpamodelgen
和kotlin-kapt
生成的类。没什么特别的。
ServiceEntity _ (build/generated/source/kapt/main/com/example/entity
)
package com.example.entity;
import javax.annotation.Generated;
import javax.persistence.metamodel.SingularAttribute;
import javax.persistence.metamodel.StaticMetamodel;
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(ServiceEntity.class)
public abstract class ServiceEntity_ {
public static volatile SingularAttribute<ServiceEntity, String> alias;
public static volatile SingularAttribute<ServiceEntity, Integer> id;
public static final String ALIAS = "alias";
public static final String ID = "id";
}
此外,我还有下一个build.gradle
脚本。
buildscript {
ext.kotlin_version = '1.2.51'
ext.spring_boot_version = '2.0.3.RELEASE'
ext.spring_fw_version = '5.0.4.RELEASE'
ext.hibernate_version = '5.3.1.Final'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath("org.springframework.boot:spring-boot-gradle-plugin:$spring_boot_version")
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version")
}
}
group 'com.example'
version '0.0.1'
apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'kotlin-kapt'
apply plugin: "kotlin-jpa"
apply plugin: 'application'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
kapt {
correctErrorTypes = true
}
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
kapt "org.hibernate:hibernate-jpamodelgen:${hibernate_version}"
compile group: 'org.springframework.boot', name: 'spring-boot-devtools', version: "$spring_boot_version"
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: "$spring_boot_version"
compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: "$spring_boot_version"
compile group: 'org.springframework.security', name: 'spring-security-web', version: "$spring_fw_version"
compile group: 'org.springframework', name: 'spring-test', version: "$spring_fw_version"
compile("org.springframework.boot:spring-boot-starter-actuator")
compile group: 'org.apache.commons', name: 'commons-text', version: '1.4'
compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3.3'
compile group: 'org.mockito', name: 'mockito-core', version: '2.16.0'
compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-kotlin', version: '2.9.+'
compile group: 'commons-codec', name: 'commons-codec', version: '1.11'
compile group: 'org.hibernate', name: 'hibernate-core', version: "$hibernate_version"
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.2'
compile group: 'junit', name: 'junit', version: '4.12'
}
sourceCompatibility = 1.8
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
task buildArtifacts(dependsOn: bootJar) {
copy {
from bootJar
into "artifacts"
rename { String fileName ->
fileName.replace("-" + bootJar.getVersion(), "")
}
}
copy {
from "application.properties"
into "artifacts"
}
copy {
from "application-prod.properties"
into "artifacts"
}
}
有一件事很奇怪。当我手动运行buildArtifacts
然后运行生成的jar
时,所有工作都很好,就像在测试中一样。单元测试用@RunWith(SpringRunner::class)
和@SpringBootTest
注释。
我想,bootRun
任务似乎已损坏或配置错误。