gpt4 book ai didi

java - 使用 spring-data-cassandra 的 spring boot 应用程序中的 TTL 支持

转载 作者:搜寻专家 更新时间:2023-10-31 19:44:46 24 4
gpt4 key购买 nike

我正在尝试在使用 spring boot 应用程序插入和更新数据时向每一列添加 TTL。为此,我正在使用 spring-data-cassandra 1.1.3.RELEASE

为此我编写了一个接口(interface) CustomTTLRepository:

@NoRepositoryBean

public interface CustomTTLRepository<T, ID extends Serializable> extends TypedIdCassandraRepository<T, ID> {

<S extends T> S save(S s, int ttl);

}

实现 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);
}

但是当我尝试部署此应用程序时出现以下错误:

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. 自定义基本存储库
  2. 自定义个人存储库

自定义基础存储库的更改

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。使用新的保存方法定义片段接口(interface)

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-implementations

关于java - 使用 spring-data-cassandra 的 spring boot 应用程序中的 TTL 支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34505376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com