代码之家  ›  专栏  ›  技术社区  ›  user3310115

未使用springboot在cassandra db中创建表

  •  0
  • user3310115  · 技术社区  · 6 年前

    我试图在启动spring boot应用程序时在cassandra db中创建表,但它似乎无法创建表。下面是我的配置。我有 @EnableCassandraRepositories 在我的申请课上。默认情况下,我已经创建了我的键空间。所以我要做的只是桌子。

    配置

    @Configuration
    public class CassandraConfig extends AbstractCassandraConfiguration {
        @Value("${cassandra.contactpoints}")
        private String contactPoints;
        @Value("${cassandra.port}")
        private int port;
        @Value("${cassandra.keyspace}")
        private String keySpace;
        @Value("${cassandra.basePackages}")
        private String basePackages;
    
        @Autowired
        private Environment environment;
    
        @Override
        protected String getKeyspaceName() {
            return keySpace;
        }
    
        @Override
        @Bean
        public CassandraClusterFactoryBean cluster() {
            final CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
            cluster.setContactPoints(contactPoints);
            cluster.setPort(port);
            return cluster;
        }
    
        @Override
        @Bean
        public CassandraMappingContext cassandraMapping() throws ClassNotFoundException {
            return new BasicCassandraMappingContext();
        }
    

    }

    实体

    @Table
    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    public class AssessmentAttemptDetailsEntity implements Serializable {
    
    
        @PrimaryKeyColumn(type = PrimaryKeyType.PARTITIONED)
        private String assessmentId;
    
        @PrimaryKeyColumn(type = PrimaryKeyType.CLUSTERED)
        private String attempid;
    
    }
    

    应用

    @SpringBootApplication
    @ComponentScan(basePackages = {"com.lte.assessmentanalytics.service","com.lte.assessmentanalytics.config", "com.lte.assessmentanalytics.model", "com.lte.assessmentanalytics.listener"})
    @EnableCassandraRepositories("com.lte.assessmentanalytics.model")
    public class AssessmentanalyticsApplication {
    
        @Autowired
        private AssessmentAttemptRepository assessmentAttemptRepository;
    
    
        public static void main(String[] args) {
            SpringApplication.run(AssessmentanalyticsApplication.class, args);
        }
    }
    

    知识库

    @Repository
    public interface AssessmentAttemptRepository extends CassandraRepository<AssessmentAttemptDetailsEntity, Long> {
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   user3310115    6 年前

    我可以通过修改 CassandraConfig 类到。

    @Configuration
    @EnableCassandraRepositories("com.lte.assessmentanalytics.model")
    public class CassandraConfig extends AbstractCassandraConfiguration {
        @Value("${cassandra.contactpoints}")
        private String contactPoints;
        @Value("${cassandra.port}")
        private int port;
        @Value("${cassandra.keyspace}")
        private String keySpace;
        @Value("${cassandra.basePackages}")
        private String basePackages;
    
    
        @Override
        protected String getKeyspaceName() {
            return keySpace;
        }
    
        @Override
        protected String getContactPoints() {
            return contactPoints;
        }
    
        @Override
        protected int getPort() {
            return port;
        }
    
        @Override
        public SchemaAction getSchemaAction() {
            return SchemaAction.CREATE_IF_NOT_EXISTS;
        }
    
        @Override
        public String[] getEntityBasePackages() {
            return new String[] {basePackages};
        }
    
    }