使用 spring-data-cassandra 的 spring boot 应用程序中的 TTL 支持
我正在尝试在使用 Spring Boot 应用程序插入和更新数据时向每列添加 TTL.为此,我使用 spring-data-cassandra 1.1.3.RELEASE
I am trying to add TTL to each column while insertion and update data using spring boot applicaiton. For this i am using spring-data-cassandra 1.1.3.RELEASE
为此我编写了一个接口 CustomTTLRepository:
For this i had written one interface CustomTTLRepository:
@NoRepositoryBean
public interface CustomTTLRepository<T, ID extends Serializable> extends TypedIdCassandraRepository<T, ID> {
<S extends T> S save(S s, int ttl);
}
实现 CustomTTLRepositoryImpl:
Implementation CustomTTLRepositoryImpl:
@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);
}
}
但是当我尝试部署此应用程序时出现以下错误:
But when i am trying to deploy this application i am getting following error:
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)
提交任何帖子可能是一个旧线程.但它帮助我完成了手头的任务.所以这里是对我有用的解决方案
It may be an old thread to submit any post. But it helped me to complete my task at hand. So here are the solutions that worked for me
- 自定义基础存储库
- 自定义单个存储库
自定义基本存储库的更改
1.1.扩展 CassandraRepository
以添加接受 TTL 的保存方法
1.1. Extend CassandraRepository
to add save method that accepts TTL
@NoRepositoryBean
public interface ExtendedCassandraRepository<T, ID> extends CassandraRepository<T, ID> {
<S extends T> S save(S entity, int ttl);
}
1.2.提供新的保存方法的实现
1.2. Provide implementation for new save method
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.为我的域实体创建存储库
1.3. Create repository for my domain entity
@Repository
public interface MyDomainEntityRepository extends ExtendedCassandraRepository<MyDomainEntity,String> {
}
1.4.更新 repositoryBaseClass
@EnableCassandraRepositories(basePackages = "my.repository", repositoryBaseClass = ExtendedCassandraRepositoryImpl.class)
自定义单个存储库的更改
2.1.使用新的保存方法定义片段接口
2.1. Define a fragment interface with new save method
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.在存储库中扩展此方法
2.3. Extend this method in repository
@Repository
public interface MyDomainEntityRepository extends CassandraRepository<MyDomainEntity,String>, CustomizedSave<MyDomainEntity> {
}