Hibernate并将@GeneratedValue与CockroachDB一起使用会导致SQLGrammarException

Hibernate并将@GeneratedValue与CockroachDB一起使用会导致SQLGrammarException,hibernate,jpa,spring-boot,kotlin,cockroachdb,Hibernate,Jpa,Spring Boot,Kotlin,Cockroachdb,下面的最小示例使用Spring Boot、Hibernate、JpaRepository、CockroachDB和Kotlin生成的id生成一个org.Hibernate.exception.sqlgrammareexception 我还测试了PostgresSQL而不是CockroachDB,这很好 |------------------------------------------------| | | PostgresSQL | CockroachD

下面的最小示例使用Spring Boot、Hibernate、JpaRepository、CockroachDB和Kotlin生成的id生成一个
org.Hibernate.exception.sqlgrammareexception

我还测试了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)
}

/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")
}
运行数据服务:

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/"}
然而,当使用PostgreSQL读取蟑螂数据库时,结果很好

{"value":"foo","id":123}
你知道是什么原因导致了这个问题,或者在使用蟑螂B时如何避免它吗

以下是完整的日志:

  • 蟑螂:
  • 春天:

    • 弹簧日志有以下条目:

      Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "sequence"
      
      Detail: source SQL: 
        create sequence hibernate_sequence start 1 increment 1
      
      CockroachDB 1.x中不支持序列,但2.0中支持序列。beta状态确实意味着它还没有被认为可以投入生产使用,但是如果你只是在玩蟑螂B,它可能会很好

      CockroachDB 2.0中的序列遵循postgres语法和行为,但
      循环
      除外(尚未实现)。此处使用的语句不使用它,仅
      START 1 INCREMENT 1


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

      该错误不是sql错误,而是介于两者之间的错误。能否添加有关实际查询运行和sql错误的详细信息?您可以了解如何在CockroachDB中记录查询。@Marc:刚刚将日志添加到我的问题中。如果不够,请告诉我。
      {"value":"foo","id":123}
      
      Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "sequence"
      
      Detail: source SQL: 
        create sequence hibernate_sequence start 1 increment 1