本文介绍了如何使用 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.

那么有没有办法在 spring 容器启动期间创建 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 创建表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:07