我已经开始将一个Spring Boot应用程序从Java迁移到Kotlin.在此之前,一切都运行得很完美.现在我不能更新通过PUT/POST接收的任何实体,因为数据不包含来自数据类的缺省值
一个实体看起来是这样的:
package org.some.soft.domain
import java.util.*
import javax.persistence.*
@Entity
@Table(name = "book_item")
data class BookItem (
@ManyToOne(cascade = [CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH])
var book: Book,
@Column(name = "format", nullable = false)
var format: BookFormat = BookFormat.HARDCOVER,
@Column(name = "barcode", nullable = false)
var barcode: String = "0000000000000",
@Column(name = "label", unique = true)
var label: String? = null,
@Column(name = "isReferenceOnly", nullable = false)
var referenceOnly: Boolean = false,
@Column(name = "borrowed")
var borrowed: Date? = null,
@Column(name = "dueDate")
var dueDate: Date? = null,
@Column(name = "price")
var price: Double? = null,
@Column(name = "status", nullable = false)
var status: BookStatus = BookStatus.AVAILABLE,
@Column(name = "dateOfPurchase")
var dateOfPurchase: Date = Date(),
@Column(name = "publicationDate", nullable = false)
var publicationDate: Date = Date(),
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
var id: Long? = null,
)
我通过以下控制器接收数据:
@PutMapping("/book/items")
fun updateBookItem(@RequestBody book: @Valid BookItem): ResponseEntity<BookItem> {
val result = bookService.updateBook(book)
return ResponseEntity
.ok()
.headers(HeaderUtil.createEntityUpdateAlert(applicationName, true, ENTITY_NAME, book.id.toString()))
.body(result)
}
该服务只是从JPA-Repository(bookItemRepository.save(bookItem)
)调用了save方法,这就是我收到以下错误的步骤:
NULL not allowed for column "PUBLICATION_DATE"; SQL statement:
update book_item set barcode=?, book_id=?, borrowed=?, date_of_purchase=?, due_date=?, format=?, label=?, price=?, publication_date=?, is_reference_only=?, status=? where id=? [23502-214]
在this个教程之后,通过复制来自另一个JHipster项目的Gradle版本,我添加了以下插件:
apply plugin: "kotlin" // Required for Kotlin integration
apply plugin: "kotlin-kapt" // Required for annotations processing
apply plugin: "kotlin-spring" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#spring-support
apply plugin: "kotlin-allopen" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#using-in-gradle
apply plugin: "kotlin-jpa" // See https://kotlinlang.org/docs/reference/compiler-plugins.html#jpa-support
apply plugin: "io.gitlab.arturbosch.detekt"
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlin_version}"
implementation "org.jetbrains.kotlin:kotlin-reflect:${kotlin_version}"
kapt "org.mapstruct:mapstruct-processor:${mapstructVersion}"
kapt "org.hibernate:hibernate-jpamodelgen:${hibernateVersion}"
kapt "org.glassfish.jaxb:jaxb-runtime:${jaxbRuntimeVersion}"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:${kotlin_version}"
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
}
allOpen {
annotation("javax.persistence.Entity")
annotation("javax.persistence.MappedSuperclass")
annotation("javax.persistence.Embeddable")
}
但是我还是遇到了一个问题,那就是Spring Boot没有使用我的数据类的默认值.我也进行了调试,属性没有用它们的缺省值进行初始化.
我还try 将var
更改为val
,但错误仍然存在
注意:我不会从客户端发送空值.我的请求正文只包含不是null
的值
非常提前感谢!