Hibernate和@GeneratedValue与CockroachDB一起使用导致SQLGrammarException

时间:2018-03-19 13:19:49

标签: hibernate jpa spring-boot kotlin cockroachdb

以下使用Spring Boot,Hibernate,JpaRepository,CockroachDB和Kotlin生成的id的最小示例生成org.hibernate.exception.SQLGrammarException

我还使用PostgresSQL而不是CockroachDB进行了测试,这很好。

|------------------------------------------------|
|                 |  PostgresSQL  |  CockroachDB |
|-----------------+---------------+--------------|
| no generated id |  OK           |  OK          |
| generated id    |  OK           |  ERROR       |
|------------------------------------------------|

这是重现问题的代码。

./src/main/kotlin/ThingService.kt

package things

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

import javax.persistence.Entity
import javax.persistence.Id
import javax.persistence.Table
import javax.persistence.Column
import javax.persistence.GeneratedValue

import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody

import org.springframework.data.jpa.repository.JpaRepository

import com.fasterxml.jackson.annotation.JsonIgnore

interface ThingRepository : JpaRepository<Thing, Long> {
}

@RestController
class ThingController(private val repository: ThingRepository) {
    @PutMapping("/thing/")
    fun save(@RequestBody t:Thing): Thing
            = repository.save(t)
}

@Entity
data class Thing (
    @Column(name="value")
    var value: String,
    @Id
    @GeneratedValue
    @Column(name="id")
    @JsonIgnore
    var id: Long = -1
)

@SpringBootApplication
class Application {
}

fun main(args: Array<String>) {
    runApplication<Application>(*args)
}

./src/main/resources/application.properties

server.port=8082

spring.datasource.url=jdbc:postgresql://localhost:26257/things_db?sslmode=disable
spring.datasource.username=root
spring.datasource.password=123

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL94Dialect

spring.jpa.hibernate.ddl-auto=update

./build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    val kotlinVersion = "1.2.20"
    id("org.springframework.boot") version "2.0.0.RELEASE"
    id("org.jetbrains.kotlin.jvm") version kotlinVersion
    id("org.jetbrains.kotlin.plugin.spring") version kotlinVersion
    id("org.jetbrains.kotlin.plugin.jpa") version kotlinVersion
    id("io.spring.dependency-management") version "1.0.4.RELEASE"
}

version = "1.0.0-SNAPSHOT"

tasks.withType<KotlinCompile> {
    kotlinOptions {
        jvmTarget = "1.8"
        freeCompilerArgs = listOf("-Xjsr305=strict")
    }
}

val test by tasks.getting(Test::class) {
    useJUnitPlatform()
}

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    compile("org.jetbrains.kotlin:kotlin-reflect")
    compile("org.hibernate:hibernate-core")
    compile("org.springframework.retry:spring-retry:1.2.2.RELEASE")
    compile("org.postgresql:postgresql")
    compile("org.json:json:20180130")
    testCompile("org.springframework.boot:spring-boot-starter-test") {
        exclude(module = "junit")
    }
    testImplementation("org.junit.jupiter:junit-jupiter-api")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}

以下是重现问题的步骤。

下载并初始化CockroachDB:

# download
wget -qO- https://binaries.cockroachdb.com/cockroach-v1.1.6.linux-amd64.tgz | tar xvz

# start
./cockroach-v1.1.6.linux-amd64/cockroach start --insecure
# leave terminal open in background

# init
./cockroach-v1.1.6.linux-amd64/cockroach sql --insecure -e "CREATE USER root WITH PASSWORD '123';"
./cockroach-v1.1.6.linux-amd64/cockroach sql --insecure -e "CREATE DATABASE things_db;"
./cockroach-v1.1.6.linux-amd64/cockroach sql --insecure -e "GRANT ALL ON DATABASE things_db TO root;"

运行数据服务:

gradle bootRun

现在尝试将一个东西放入服务中:

curl -w "\n" -H 'Content-Type: application/json' -X PUT -d '{"value":"foo", "id":123}' http://localhost:8082/thing/

回复是:

{"timestamp":"2018-03-19T13:05:12.856+0000","status":500,"error":"Internal Server Error","message":"could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet","path":"/thing/"}

然而,当使用CockroachDB的PostgreSQL实例时,结果很好

{"value":"foo","id":123}

知道导致问题的原因或使用CockroachDB时如何避免这种问题?

以下是完整日志:

1 个答案:

答案 0 :(得分:1)

Spring日志包含以下条目:

Keras 2.1.3 + TensorFlow 1.4.1: 4 hours
Keras 2.1.4 + TensorFlow 1.5.0: 200 hours

CockroachDB 1.x中不支持序列,但它们在2.0中为404.html。测试版状态确实意味着它不被认为可以用于生产用途,但如果你只是在玩CockroachDB,它可能会没问题。

CockroachDB 2.0中的序列遵循postgres语法和行为,Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "sequence" Detail: source SQL: create sequence hibernate_sequence start 1 increment 1 除外(尚未实现)。此处使用的语句不使用它,仅使用cycle

您可以在still in beta中找到有关序列的更多详细信息。