Gek*_*ter 3 java composite-key cassandra spring-data spring-data-cassandra
我正在尝试使用复合主键类使用 Spring Data Cassandra。但是当我尝试查询数据时出现异常:
org.springframework.data.repository.query.QueryCreationException: **Could not create query for public abstract java.util.List com.amdocs.cassandrapoc.repository.ServiceRepository.findByKey(com.amdocs.cassandrapoc.entities.Key)! Reason: Cannot use composite primary key directly. Reference a property of the composite primary key**
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
@PrimaryKeyClass
public class Key implements Serializable {
@PrimaryKeyColumn(name = "id", ordinal = 0, type =
PrimaryKeyType.PARTITIONED)
@CassandraType(type = DataType.Name.UUID)
private UUID id;
@PrimaryKeyColumn(name = "tenant", ordinal = 1, type =
PrimaryKeyType.PARTITIONED)
@CassandraType(type = DataType.Name.TEXT)
private String tenant;
(Equals, hashcode, getters, setters omitted)
}
Table(value = "service")
public class Service implements Serializable {
@PrimaryKey
private Key key;
@Column
private String value;
(constructors, setters, getters omitted)
}
@Repository
public interface ServiceRepository extends CassandraRepository<Service, Key> {
List<Service> findByKey(Key key);
}
Run Code Online (Sandbox Code Playgroud)
这就是我创建表的方式(使用嵌入式 Cassandra 和 junit):
@Autowired
private CassandraAdminOperations adminTemplate;
private final String DATA_TABLE_NAME = "service";
@Before
public void createTable() {
adminTemplate.createTable(
true, CqlIdentifier.of(DATA_TABLE_NAME),
Service.class, new HashMap<String, Object>());
}
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
我遇到了同样的问题。我通过以下实现解决了我的问题。
@Repository
public interface ServiceRepository extends CassandraRepository<Service, Key> {
List<Service> findByKeyIdAndKeyTenant(UUID id, String tenant);
}
Run Code Online (Sandbox Code Playgroud)
尝试使用 CrudRepository 而不是 CassandraRepository:
基于 CassandraRepository 的存储库可以定义单个主键、使用主键类或不带主键类的复合主键。使用复合主键但没有主键类的类型必须使用 MapId 来声明其键值。
| 归档时间: |
|
| 查看次数: |
4721 次 |
| 最近记录: |