2017-01-31 30 views
2

我試圖使用自定義存儲庫來保存具有TTL(生存時間)值的實體。我已經做了大量的搜索並在線閱讀文檔,但我仍然遇到異常。Spring Cassandra TTL自定義存儲庫保存

感謝任何幫助。

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property saveWithTTL found for type Task! 

的片段如下:

任務(實體):

@Table 
public class Task { 
    @PrimaryKeyColumn(ordinal = 0, type = PrimaryKeyType.PARTITIONED) 
    private String uuid; 

    private Type type; 
    private Status status; 
    private String parentId; 

    private String body; 

} 

CassandraDbConfig:

@Configuration 
@PropertySource(value = "classpath:cassandra.properties") 
@EnableCassandraRepositories(repositoryBaseClass = TTLRepositoryCustomImpl.class) 
public class CassandraDbConfig extends DefaultCassandraConfig { 

} 

TTLRepositoryCustom:

@NoRepositoryBean 
public interface TTLRepositoryCustom<T> extends CassandraRepository<T> { 

    T saveWithTTL(T entity, Integer ttl); 
} 

TTLRepositoryCustomImpl:

public class TTLRepositoryCustomImpl<T> extends SimpleCassandraRepository<T, MapId>implements TTLRepositoryCustom<T> { 

    public TTLRepositoryCustomImpl(final CassandraEntityInformation<T, MapId> metadata, 
      final CassandraOperations operations) { 
     super(metadata, operations); 
    } 

    @Override 
    public T saveWithTTL(T entity, Integer ttl) { 
     WriteOptions options = new WriteOptions(); 
     options.setTtl(ttl); 
     return operations.insert(entity, options); 
    } 
} 

TaskDbRepository:

@Repository 
public interface TaskDbRepository extends TTLRepositoryCustom<Task> { 

} 

完整的堆棧跟蹤:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property saveWithTTL found for type Task! 
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) 
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) 
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) 
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272) 
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243) 
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76) 
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:247) 
    at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:398) 
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:378) 
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:86) 
    at org.springframework.data.cassandra.repository.query.PartTreeCassandraQuery.<init>(PartTreeCassandraQuery.java:47) 
    at org.springframework.data.cassandra.repository.support.CassandraRepositoryFactory$CassandraQueryLookupStrategy.resolveQuery(CassandraRepositoryFactory.java:163) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:436) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:221) 

回答

0

您可以使用像這樣在服務類中的春天啓動的應用程序(與其他'正常'cassandra回購在應用程序中),TTL將在幾秒鐘內,我知道你要求一個完整的TTL回購impl ementation,但是如果你只想用TTL來保存,這可能會很方便。

@Autowired 
private CassandraOperations cassandraOperations; 

private void saveWithTTL(Task task) 
{ 
    String cql = "insert into task (uuid, body) values ('"+task.getUuid()+"', "+task.getBody()+") USING TTL 86400;"; 
    cassandraOperations.execute(cql); 
}