I am trying to use Spring Data Cassandra using Composite primary key class. But when I try to query data I get an exception:
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**
This is my code:
@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);
}
This is how I create table (using embedded Cassandra and 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>());
}
What did I do wrong?
Cannot use composite primary key directly
is currently a limitation of how query mapping is implemented. Looking at your code you could expect exact property matching. There are a lot of other possibilities (findByKeyGreaterThan
,findByKeyIn
, dealing withnull
properties in your key) which raise a couple of questions how to deal with these possibilities. Care to file a ticket at jira.spring.io/browse/DATACASS so we can continue the discussion in our issue tracker? – Distributive