在插入和更新数据时,我正在尝试向每列添加TTL,同时使用spring boot应用程序。为此,我使用弹簧数据cassandra 1.1.3.RELEASE
为此,我编写了一个界面CustomTTL存储库:
@NoRepositoryBean
public interface CustomTTLRepository<T, ID extends Serializable> extends TypedIdCassandraRepository<T, ID> {
<S extends T> S save(S s, int ttl);
}
实施自定义TTLRepositoryImpl:
@NoRepositoryBean
public class CustomTTLRepositoryImpl<T, ID extends Serializable> extends SimpleCassandraRepository<T, ID> implements CustomTTLRepository<T, ID> {
public CustomTTLRepositoryImpl(CassandraEntityInformation<T, ID> metadata, CassandraTemplate template) {
super(metadata, template);
this.entityInformation = metadata;
this.template = template;
}
@Override
public <S extends T> S save(S s, int ttl) {
WriteOptions writeOptions=new WriteOptions();
writeOptions.setTtl(ttl);
return template.insert(s, writeOptions);
}
}
但当我试图部署这个应用程序时,我得到了以下错误:
Caused by: java.lang.IllegalArgumentException: encountered unsupported query parameter type [class java.lang.Object] in method public abstract java.lang.Object com.cisco.operation.CustomTTLRepository.save(java.lang.Object,int)
at org.springframework.data.cassandra.repository.query.CassandraQueryMethod.verify(CassandraQueryMethod.java:104)
at org.springframework.data.cassandra.repository.query.CassandraQueryMethod.<init>(CassandraQueryMethod.java:68)
at org.springframework.data.cassandra.repository.support.CassandraRepositoryFactory$CassandraQueryLookupStrategy.resolveQuery(CassandraRepositoryFactory.java:106)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:369)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:192)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225)
提交任何帖子都可能是一个旧线程。但它帮助我完成了手头的任务。以下是对我有效的解决方案
- 自定义基本存储库
- 自定义单个存储库
自定义基本存储库的更改
1.1.扩展CassandraRepository
以添加接受TTL 的保存方法
@NoRepositoryBean
public interface ExtendedCassandraRepository<T, ID> extends CassandraRepository<T, ID> {
<S extends T> S save(S entity, int ttl);
}
1.2.提供新保存方法的实施
public class ExtendedCassandraRepositoryImpl<T, ID> extends SimpleCassandraRepository<T, ID> implements ExtendedCassandraRepository<T, ID> {
private final CassandraEntityInformation<T, ID> entityInformation;
private final CassandraOperations operations;
public ExtendedCassandraRepositoryImpl(CassandraEntityInformation metadata, CassandraOperations operations) {
super(metadata, operations);
this.entityInformation = metadata;
this.operations = operations;
}
@Override
public <S extends T> S save(S entity, int ttl) {
InsertOptions insertOptions = org.springframework.data.cassandra.core.InsertOptions.builder().ttl(ttl).build();
operations.insert(entity, insertOptions);
return entity;
}
}
1.3.为我的域实体创建存储库
@Repository
public interface MyDomainEntityRepository extends ExtendedCassandraRepository<MyDomainEntity,String> {
}
1.4.更新repositoryBaseClass
@EnableCassandraRepositories(basePackages = "my.repository", repositoryBaseClass = ExtendedCassandraRepositoryImpl.class)
自定义单个存储库的更改
2.1用新的保存方法定义片段接口
public interface CustomizedSave<T> {
<S extends T> S save(S entity, int ttl);
}
2.2.提供实施
public class CustomizedSaveImpl<T> implements CustomizedSave<T> {
@Autowired
private CassandraOperations operations;
@Override
public <S extends T> S save(S entity, int ttl) {
InsertOptions insertOptions = org.springframework.data.cassandra.core.InsertOptions.builder().ttl(ttl).build();
operations.insert(entity, insertOptions);
return entity;
}
}
2.3.在存储库中扩展此方法
@Repository
public interface MyDomainEntityRepository extends CassandraRepository<MyDomainEntity,String>, CustomizedSave<MyDomainEntity> {
}
参考文件https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.custom-实现
尽管答案可能为时已晚,但我已经尝试了CassandraOperations
,它运行良好,请查看下面的存储库实现
@Repository
public class DomainRepository {
@Autowired
CassandraOperations cassandraOperations;
@Autowired
WriteOptions writeOptions;
@Override
public void save(DomainObject domainObject) {
writeOptions.setTtl(100);
cassandraOperations.insert(domainObject,writeOptions);
}
}
感谢