Tob*_*ann 3 cassandra kotlin spring-boot
spring-boot-starter-data-cassandra在尝试使用Spring Boot2.4.6更新项目时2.5.0,我遇到了注释被忽略的问题@Column。
使用以下注释
@Column("blabla")
val buz: Long
Run Code Online (Sandbox Code Playgroud)
结果出现此错误:
Query; CQL [INSERT INTO bar (baz,buz) VALUES (?,?)]; Undefined column name buz; nested exception is com.datastax.oss.driver.api.core.servererrors.InvalidQueryException: Undefined column name buz
Run Code Online (Sandbox Code Playgroud)
因此查询使用buz而不是blabla按预期使用。用Spring Boot2.4.6代替2.5.0它工作得很好。是否发生了某些变化,因此我需要调整我的代码,或者这是一个错误?
问题似乎来自于spring-data-cassandra,它使用 Spring Boot 进行更新。
2.4.6都implementation(group = "org.springframework.data", name = "spring-data-cassandra", version = "3.1.9")很好。2.4.6,implementation(group = "org.springframework.data", name = "spring-data-cassandra", version = "3.2.0")事情会失败。看起来整个@Column注释被忽略,因为forceQuote = true当我添加它时也没有使用它。
可以使用以下最小示例 ( docker build .) 来重现该错误:
DockerfileFROM openjdk:11-jdk-slim
WORKDIR /home/test
ADD . /home/test
RUN ./gradlew test
Run Code Online (Sandbox Code Playgroud)
build.gradle.ktsimport org.jetbrains.kotlin.gradle.tasks.KotlinCompile
val kotlinVersion = "1.5.10"
val springBootVersion = "2.4.6"
plugins {
val pluginKotlinVersion = "1.5.10"
val pluginSpringBootVersion = "2.5.0"
id("org.springframework.boot") version pluginSpringBootVersion
kotlin("jvm") version pluginKotlinVersion
kotlin("plugin.spring") version pluginKotlinVersion
kotlin("plugin.jpa") version pluginKotlinVersion
}
group = "com.acme"
version = "1.0.0-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
dependencies {
implementation(
group = "org.springframework.boot",
name = "spring-boot-starter-data-cassandra",
version = springBootVersion
)
// 3.1.9 -> fine
// 3.2.0 -> broken
// 3.2.1 -> broken
implementation(group = "org.springframework.data", name = "spring-data-cassandra", version = "3.2.0")
implementation(group = "org.springframework.boot", name = "spring-boot-starter-web", version = springBootVersion)
implementation(group = "org.jetbrains.kotlin", name = "kotlin-reflect", version = kotlinVersion)
implementation(
group = "org.cassandraunit",
name = "cassandra-unit-spring",
version = "4.3.1.0"
) {
exclude(group = "org.hibernate")
testImplementation(group = "com.google.guava", name = "guava") {
version {
// https://github.com/jsevellec/cassandra-unit/issues/248
// https://issues.apache.org/jira/browse/CASSANDRA-15245
strictly("18.0")
}
}
}
testImplementation(
group = "org.springframework.boot",
name = "spring-boot-starter-test",
version = springBootVersion
)
}
tasks {
withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
withType<Test> {
testLogging.exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
}
Run Code Online (Sandbox Code Playgroud)
settings.gradle.ktsrootProject.name = "acmetest"
Run Code Online (Sandbox Code Playgroud)
src/main/kotlin/com/acme/Application.ktpackage com.acme
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.data.cassandra.core.mapping.Column
import org.springframework.data.cassandra.core.mapping.PrimaryKey
import org.springframework.data.cassandra.core.mapping.Table
import org.springframework.data.cassandra.repository.CassandraRepository
import org.springframework.stereotype.Repository
@SpringBootApplication
class Application
fun main(args: Array<String>) {
runApplication<Application>(*args)
}
@Table("bar")
class Bar(
@PrimaryKey("baz")
val baz: Long,
@Column("blabla")
val buz: Long
)
@Repository
interface BarRepository : CassandraRepository<Bar, Long>
Run Code Online (Sandbox Code Playgroud)
src/main/resources/application.ymlspring:
data:
cassandra:
contact-points: localhost
port: 9142
keyspace_name: foo
local-datacenter: datacenter1
Run Code Online (Sandbox Code Playgroud)
src/test/kotlin/com/acme/integration/TestFullStack.ktpackage com.acme.integration
import com.acme.Bar
import com.acme.BarRepository
import org.cassandraunit.spring.CassandraDataSet
import org.cassandraunit.spring.CassandraUnitDependencyInjectionTestExecutionListener
import org.cassandraunit.spring.EmbeddedCassandra
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.TestExecutionListeners
import org.springframework.test.context.junit4.SpringRunner
@ActiveProfiles("test")
@RunWith(SpringRunner::class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestExecutionListeners(
listeners = [CassandraUnitDependencyInjectionTestExecutionListener::class],
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS
)
@CassandraDataSet(value = ["cql/foo.cql"], keyspace = "foo")
@EmbeddedCassandra
class TestFullStack {
@Autowired
lateinit var barRepository: BarRepository
@Test
fun `test init`() {
barRepository.save(Bar(1, 2))
}
}
Run Code Online (Sandbox Code Playgroud)
src/test/resources/cql/foo.cqlDROP KEYSPACE IF EXISTS foo;
CREATE KEYSPACE foo WITH REPLICATION = {'class':'SimpleStrategy', 'replication_factor':1};
CREATE TABLE foo.bar
(
baz bigint,
blabla bigint,
PRIMARY KEY (baz)
);
Run Code Online (Sandbox Code Playgroud)
好吧,问题似乎在于Bar构造函数中已经声明了成员。即,替换这个
@Table("bar")
class Bar(
@PrimaryKey("baz")
val baz: Long,
@Column("blabla")
val buz: Long
)
Run Code Online (Sandbox Code Playgroud)
接着就,随即
@Table("bar")
class Bar(baz: Long, buz: Long) {
@PrimaryKey("baz")
val baz: Long = baz
@Column("blabla")
val buz: Long = buz
}
Run Code Online (Sandbox Code Playgroud)
让它再次发挥作用。
两个版本3.1.9都org.springframework.data:spring-data-cassandra工作正常,但第一个版本在更新到3.2.0. 我们提出了一个问题:https ://github.com/spring-projects/spring-data-cassandra/issues/1136
编辑 2022-02-01:以下内容也有效:
@Table("bar")
class Bar(
@PrimaryKey("baz")
val baz: Long,
@field:Column("blabla")
val buz: Long
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1440 次 |
| 最近记录: |