springboot—不是在cassandradb中使用springboot创建的表

wxclj1h5  于 2021-06-14  发布在  Cassandra
关注(0)|答案(1)|浏览(424)

我试图在启动spring启动应用程序时在cassandradb中创建表,但它似乎无法创建表。下面是我的配置。我有 @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> {
}
ilmyapht

ilmyapht1#

我可以通过修改我的 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};
    }

}

相关问题