spring-projects / spring-data-cassandra

Provides support to increase developer productivity in Java when using Apache Cassandra. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-cassandra/
Apache License 2.0
380 stars 311 forks source link

Spring in Kotlin, can't create data tables #1334

Closed SaherAlSous closed 1 year ago

SaherAlSous commented 1 year ago

hello... I'm using cassandra for spring in kotlin project. and i'm creating the tables using the @Table annotation like this

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

and i'm loading the initial value of the tables in the @Configuration class like this:

@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.data")
    }

    @Bean(autowireCandidate = true)
    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))
        }
    }
}

inside the application.properties file inside the Resources i made the configuration like this

#Cassandra settings
spring.data.cassandra.keyspace-name = tacocloud
spring.data.cassandra.schema-action = create
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 = ***

I keep getting this error

Caused by: com.datastax.oss.driver.api.core.servererrors.InvalidQueryException: table ingredients does not exist Caused by: org.springframework.data.cassandra.CassandraInvalidQueryException: Query; CQL [TRUNCATE ingredients]; table ingredients does not exist

I'm using these dependencies

    implementation("org.springframework.boot:spring-boot-starter-data-cassandra")
    // https://mvnrepository.com/artifact/org.springframework.data/spring-data-cassandra
    implementation("org.springframework.data:spring-data-cassandra:4.0.0")

Whats wrong in my implementation, why can't I create the tables? just to note that when i comment the code related to interacting with the data tables, the server works fine. until I do any action related to cassandra tables.

any solution?

SaherAlSous commented 1 year ago

anyone?

SaherAlSous commented 1 year ago

Is there a way to create the data tables using beans? Other than @Table annotations I mean

Hsain-98 commented 1 year ago

Facing the same problem 🙄🫤

SaherAlSous commented 1 year ago

it happened the same with JDBC tables, annotations didn't create any db table... but in jdbc we can create it using schema.sql. one guy advised me to use migration tool to create the tables with it. will study it today and post the results if it works.

SaherAlSous commented 1 year ago

@Hsain-98 listen... I solved it by removing the @Data annotation of lombok... try that and tell me what happens... i just kept the @Table annotation and it is working now...

SaherAlSous commented 1 year ago

@Hsain-98 https://stackoverflow.com/questions/74832684/spring-in-kotlin-creating-cassandra-tables-doesnt-work