我开始寻找一个很好的解决方案,如何使用Spring CassandraOperations很好地持久化实体.问题开始是因为我的实体中的某些字段不支持cassandra,例如joda DateTime.
解决方法是在java.util.Date类型的同一实体中使用其他字段而不是joda DateTime,使用@Transient标记不需要的字段.但这不干净,所以我开始寻找自动自定义转换.
目前spring-data-cassandra参考文献没有提供如何注册自定义转换器的信息. http://docs.spring.io/spring-data/cassandra/docs/current/reference/html/#cassandra.custom-converters
是否可以在CassandraSessionfactorybean中注册自定义转换器(如此处为Spring Data Cassandra LocalDateTime Conversion)?
这是我的代码
@组态
公共课CassandraConfig {
@Value("${cassandra.contactpoints}")
private String cassandraContactPoint;
@Value("${cassandra.port}")
private int cassandraPort;
@Value("${cassandra.keyspace}")
private String cassandraKeySpace;
@Bean
public CassandraClusterfactorybean cluster() {
CassandraClusterfactorybean cluster = new CassandraClusterfactorybean();
cluster.setContactPoints(cassandraContactPoint);
cluster.setPort(cassandraPort);
return cluster;
}
@Bean
public CassandraMappingContext mappingContext() {
return new BasicCassandraMappingContext();
}
@Bean
public CassandraConverter converter() {
return new MappingCassandraConverter(mappingContext());
}
@Bean
public CassandraSessionfactorybean session() throws Exception {
CassandraSessionfactorybean session = new CassandraSessionfactorybean();
session.setCluster(cluster().getObject());
session.setKeyspaceName(cassandraKeySpace);
session.setConverter(converter());
session.setSchemaAction(SchemaAction.NONE);
return session;
}
@Bean
public CassandraOperations cassandraTemplate() throws Exception {
return new CassandraTemplate(session().getObject());
}
@Bean
public ConversionService getConversionService() {
ConversionServicefactorybean bean = new ConversionServicefactorybean();
bean.setConverters(new HashSet<>(getConverters()));
bean.afterPropertiesSet();
return bean.getObject();
}
private Set
}
最佳答案
原文链接:https://www.f2er.com/spring/432368.html