如何更新Spring CassandraRepository

时间:2016-07-30 03:47:45

标签: java spring spring-boot cassandra spring-data-cassandra

假设我在cassandra中有一个名为“UserPrincipal”的用户表,该存储库将类似于以下内容

public interface UserRepository extends CassandraRepository<UserPrincipal> 
{
   @Query("SELECT * FROM UserPrincipal WHERE email = ?0")
   UserPrincipal findByEmailAddress(String emailAddress);   
}

如果我需要用username查询表,我必须对表进行非规范化并创建一个副本,让我们称之为UserPrincipalByUsername,它与第一个相同,只与主键不同,现在,我可以使用吗?以下接口作为存储库?那么同时保存/从两个表中删除用户以保持数据一致性呢?

public interface UserRepository extends CassandraRepository<UserPrincipal> 
{
   @Query("SELECT * FROM UserPrincipal WHERE email = ?0")
   UserPrincipal findByEmailAddress(String emailAddress);   

   @Query("SELECT * FROM UserPrincipalByUsername WHERE username= ?0")
   UserPrincipal findByUsername(String username);   
}

可以注意到,可以使用两个单独的接口来单独处理每个表,但是,我需要有一些逻辑来保持某些点的一致性。

我使用的是Cassandra 2.0.11,CQL规范3.1.1,Spring数据Cassandra 1.3.2和Spring boot 1.3.1

1 个答案:

答案 0 :(得分:0)

我发现解决这个问题的唯一方法是,如问题中所提到的,使用两个单独的接口来单独处理每个表,我添加了一个包装类,使用一个来使用它们save调用,但这并不总是保证一致性(例如服务器/系统故障),但在我的特定应用程序中这是可以的。