问题
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. :-)
回答1:
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());
}
}
回答2:
You'll also need to Override the getEntityBasePackages() method in your AbstractCassandraConfiguration implementation. This will allow Spring to find any classes that you've annotated with @Table, and create the tables.
@Override
public String[] getEntityBasePackages() {
return new String[]{"com.example"};
}
回答3:
- You'll need to include spring-data-cassandra dependency in your pom.xml file.
Configure your TestConfig.class as below:
@Configuration @PropertySource(value = { "classpath:Your .properties file here" }) @EnableCassandraRepositories(basePackages = { "base-package name of your Repositories'" }) public class CassandraConfig { @Autowired private Environment environment; @Bean public CassandraClusterFactoryBean cluster() { CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean(); cluster.setContactPoints(env.getProperty("contactpoints from your properties file")); cluster.setPort(Integer.parseInt(env.getProperty("ports from your properties file"))); return cluster; } @Bean public CassandraConverter converter() { return new MappingCassandraConverter(mappingContext()); } @Bean public CassandraSessionFactoryBean session() throws Exception { CassandraSessionFactoryBean session = new CassandraSessionFactoryBean(); session.setCluster(cluster().getObject()); session.setKeyspaceName(env.getProperty("keyspace from your properties file")); session.setConverter(converter()); session.setSchemaAction(SchemaAction.CREATE_IF_NOT_EXISTS); return session; } @Bean public CassandraOperations cassandraTemplate() throws Exception { return new CassandraTemplate(session().getObject()); } @Bean public CassandraMappingContext mappingContext() throws ClassNotFoundException { CassandraMappingContext mappingContext= new CassandraMappingContext(); mappingContext.setInitialEntitySet(getInitialEntitySet()); return mappingContext; } @Override public String[] getEntityBasePackages() { return new String[]{"base-package name of all your entity annotated with @Table"}; } @Override protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException { return CassandraEntityClassScanner.scan(getEntityBasePackages()); } }
This last getInitialEntitySet method might be an Optional one. Try without this too.
Make sure your Keyspace, contactpoints and port in .properties file. Like :
cassandra.contactpoints=localhost,127.0.0.1 cassandra.port=9042 cassandra.keyspace='Your Keyspace name here'
回答4:
Use this in the application.properties file
spring.data.cassandra.schema-action=CREATE_IF_NOT_EXISTS
来源:https://stackoverflow.com/questions/25357372/how-create-table-with-spring-data-cassandara