Interface MyRepository, where I added another save method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@NoRepositoryBean | |
public interface MyRepository<T, ID extends Serializable> extends TypedIdCassandraRepository<T, ID> { | |
<S extends T> S save(S s, int ttl); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@NoRepositoryBean | |
public class MyRepositoryImpl<lT, ID extends Serializable> extends SimpleCassandraRepository<T, ID> implements MyRepository<T, ID> { | |
public MyRepositoryImpl(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); | |
} | |
} |
Notice that the documentation is for JPA repositories. For Cassandra repository one more step was needed (I did not find the way around it). Reporsitory Factory Bean needed to be created:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable> extends RepositoryFactoryBeanSupport<T, S, ID> { | |
@Autowired | |
private CassandraTemplate cassandraTemplate; | |
@Override | |
protected RepositoryFactorySupport createRepositoryFactory() { | |
return new MyRepositoryFactory(cassandraTemplate); | |
} | |
public void setCassandraTemplate(CassandraTemplate cassandraTemplate) { | |
this.cassandraTemplate = cassandraTemplate; | |
} | |
@Override | |
public void afterPropertiesSet() { | |
super.afterPropertiesSet(); | |
Assert.notNull(cassandraTemplate, "cassandraTemplate must not be null!"); | |
setMappingContext(cassandraTemplate.getConverter().getMappingContext()); | |
} | |
public static class MyRepositoryFactory extends CassandraRepositoryFactory { | |
private final CassandraTemplate cassandraTemplate; | |
public MyRepositoryFactory(CassandraTemplate cassandraTemplate) { | |
super(cassandraTemplate); | |
this.cassandraTemplate = cassandraTemplate; | |
} | |
@Override | |
@SuppressWarnings({ "rawtypes", "unchecked" }) | |
protected Object getTargetRepository(RepositoryMetadata metadata) { | |
CassandraEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType()); | |
return new MyRepositoryImpl(entityInformation, cassandraTemplate); | |
} | |
@Override | |
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) { | |
return SimpleRepository.class; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableCassandraRepositories( repositoryFactoryBeanClass = MyRepositoryFactoryBean.class) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface SampleRepository extends MyRepository<Sample, String> { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Autoriwed | |
SampleRepository sampleRepository; | |
.... | |
public void useSampleRepository(...) { | |
.... | |
sampleRepository.save(sample, ttl); | |
} |
Hello
ReplyDeleteI tried above code but 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.(CassandraQueryMethod.java:68)
Please help.
The argument for the save method should be class which is annotated with @Table
DeleteWhat is sampleRepository.save(sample, ttl);
ReplyDeletein your case?
Ho do you get sample repository from RepositoryFactorySupport?
Could you describe in details ?
MyRepositoryFactoryBean is specified in @EnableCassandraRepositories annotations. Spring takes care of initating and creating the repository beans. You just need to autowire them. I updated the post to show how to use SampleRepository.
ReplyDeletegreat article!!!!!This is very importent information for us.I like all content and information.I have read it.You know more about this please visit again.
ReplyDeleteCassandra Training in Chennai
Perde Modelleri
ReplyDeletesms onay
Mobil Ödeme Bozdurma
nft nasıl alınır
ankara evden eve nakliyat
trafik sigortası
dedektör
web sitesi kurma
aşk kitapları