How create table with spring data cassandara?

Cherry picture Cherry · Aug 18, 2014 · Viewed 8.4k times · Source

I have created my own repository like that:

public interface MyRepository extends TypedIdCassandraRepository<MyEntity, String> {
}

So the question how automatically create cassandra table for that? Currently Spring injects MyRepository which tries to insert entity to non-existent table.

So is there a way to create cassandra tables (if they do not exist) during spring container start up?

P.S. It would be very nice if there is just config boolean property without adding lines of xml and creation something like BeanFactory and etc. :-)

Answer

roblovelock picture roblovelock · Aug 18, 2014

Overide the getSchemaAction property on the AbstractCassandraConfiguration class

@Configuration
@EnableCassandraRepositories(basePackages = "com.example")
public class TestConfig extends AbstractCassandraConfiguration {

    @Override
    public String getKeyspaceName() {
        return "test_config";
    }

    @Override
    public SchemaAction getSchemaAction() {
        return SchemaAction.RECREATE_DROP_UNUSED;
    }

    @Bean
    public CassandraOperations cassandraOperations() throws Exception {
        return new CassandraTemplate(session().getObject());
    }

}