如何用spring数据cassandara创建表? [英] How create table with spring data cassandara?

查看:101
本文介绍了如何用spring数据cassandara创建表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了自己的存储库,

I have created my own repository like that:

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

那么问题是如何自动为此创建cassandra表?当前,Spring注入了MyRepository,它试图将实体插入到不存在的表中.

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

那么有什么方法可以在春季容器启动过程中创建cassandra表(如果不存在)?

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

PS 如果只有配置布尔属性而不添加xml行并创建 BeanFactory 之类的东西,那将是非常好. :-)

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. :-)

推荐答案

覆盖AbstractCassandraConfiguration类的getSchemaAction属性

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());
    }

}

这篇关于如何用spring数据cassandara创建表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆