Issue
While trying to update a project using spring-boot-starter-data-cassandra
from Spring Boot 2.4.6
to 2.5.0
, I run into a problem of my @Column
annotations being ignored.
Using the following annotation
@Column("blabla")
val buz: Long
results in this error:
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
So the query uses buz
instead of blabla
as intended. With Spring Boot 2.4.6
instead of 2.5.0
it works fine. Has something changed, so that I need to adjust my code, or is this a bug?
The problem seems to come from spring-data-cassandra
, which updates with Spring Boot.
- When using Spring Boot
2.4.6
andimplementation(group = "org.springframework.data", name = "spring-data-cassandra", version = "3.1.9")
things are fine. - When using Spring Boot
2.4.6
andimplementation(group = "org.springframework.data", name = "spring-data-cassandra", version = "3.2.0")
things fail.
It looks like the whole @Column
annotation is ignored because forceQuote = true
is also not used when I add it.
The following minimal example can be used (docker build .
) to reproduce the error:
Dockerfile
FROM openjdk:11-jdk-slim
WORKDIR /home/test
ADD . /home/test
RUN ./gradlew test
build.gradle.kts
import 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
}
}
settings.gradle.kts
rootProject.name = "acmetest"
src/main/kotlin/com/acme/Application.kt
package 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>
src/main/resources/application.yml
spring:
data:
cassandra:
contact-points: localhost
port: 9142
keyspace_name: foo
local-datacenter: datacenter1
src/test/kotlin/com/acme/integration/TestFullStack.kt
package 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))
}
}
src/test/resources/cql/foo.cql
DROP 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)
);
Solution
Ok, the issue seems to be with having the members of Bar
already declared in the constructor. I.e., replacing this
@Table("bar")
class Bar(
@PrimaryKey("baz")
val baz: Long,
@Column("blabla")
val buz: Long
)
with that
@Table("bar")
class Bar(baz: Long, buz: Long) {
@PrimaryKey("baz")
val baz: Long = baz
@Column("blabla")
val buz: Long = buz
}
makes it work again.
With version 3.1.9
of org.springframework.data:spring-data-cassandra
both are working fine, but the first one breaks when updating to 3.2.0
. We've opened an issue: https://github.com/spring-projects/spring-data-cassandra/issues/1136
Edit 2022-02-01: The following works too:
@Table("bar")
class Bar(
@PrimaryKey("baz")
val baz: Long,
@field:Column("blabla")
val buz: Long
)
Answered By - Tobias Hermann
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.