Spring in Kotlin, creating Cassandra tables doesn't work

210 views Asked by At

it seems that there might be an issue with annotating data tables with kotlin, it didn't work with jdbc and now it is the same with cassandra.

I keep getting this

Caused by: org.springframework.data.cassandra.CassandraInvalidQueryException: Query; CQL [TRUNCATE ingredients]; table ingredients does not exist

@Data
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
@Table("ingredients")
data class Ingredients(
    @PrimaryKey
    val id: String,
    val name: String,
    val type: Type
)

here is the UDT

@Data
@RequiredArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
@UserDefinedType("ingredients")
data class IngredientUDT(
    var name: String,
    var type: Type
)

here is the Configuration class


@Configuration
@EnableCassandraRepositories
class DataInitializer: AbstractCassandraConfiguration() {
 
    @Primary
    fun cqlSession(): CqlSession {
        return CqlSession.builder().build()
    }

    override fun getKeyspaceName(): String {
        return "tacocloud"
    }

    override fun getEntityBasePackages(): Array<String> {
        return arrayOf("sia.tacocloud")
    }

    @Bean
    fun dataLoader(repo: IngredientsRepository): ApplicationRunner {
        return ApplicationRunner {
            repo.deleteAll()
            repo.save(Ingredients("FLTO", "Flour Tortilla", Type.WRAP))
            repo.save(Ingredients("COTO", "Corn Tortilla", Type.WRAP))
            repo.save(Ingredients("GRBF", "Ground Beef", Type.PROTEIN))
            repo.save(Ingredients("CARN", "Carnitas", Type.PROTEIN))
            repo.save(Ingredients("TMTO", "Diced Tomatoes", Type.VEGGIES))
            repo.save(Ingredients("LETC", "Lettuce", Type.VEGGIES))
            repo.save(Ingredients("CHED", "Cheddar", Type.CHEESE))
            repo.save(Ingredients("JACK", "Monterrey Jack", Type.CHEESE))
            repo.save(Ingredients("SLSA", "Salsa", Type.CHEESE))
            repo.save(Ingredients("SRCR", "Sour Cream", Type.CHEESE))
        }
    }
}

here is the properties file

#Cassandra settings
spring.data.cassandra.keyspace-name = tacocloud
spring.data.cassandra.schema-action = RECREATE
spring.data.cassandra.local-datacenter = datacenter1
basic.load-balancing-policy.local-datacenter = datacenter1
spring.data.cassandra.contact-points=127.0.0.1
spring.data.cassandra.port=9042
spring.data.cassandra.username = ***
spring.data.cassandra.password = ***

whats wrong with this?

1

There are 1 answers

0
Saher Al-Sous On BEST ANSWER

I solved it by removing the @Data annotation from Data classes... didn't thought it would be that simple... but it worked.