spring-projects / spring-projects/spring-data-cassandra

Allow @PrimaryKeyClass(MyKeyClass.class) usage on composite key entities [DATACASS-324]

Open
#492 0 comments 0 reactions 1 assignee View on GitHub

@mp911de is already working on this.

Since Dec 31, 2020.

in: mapping type: enhancement
Dominant language
Java
Stars
400
Forks
318
PR merge metrics
No merged PRs in 30d

Description

Mark Paluch opened DATACASS-324 and commented

Entities with a composite key inside the entity class require using MapId as Id or the entity itself when invoking operations with a given id:

@Table
public class TypeWithCompositeKey {

	@PrimaryKeyColumn(type = PrimaryKeyType.PARTITIONED, ordinal = 1) 
	private String firstname;
	@PrimaryKeyColumn(type = PrimaryKeyType.PARTITIONED, ordinal = 2) 
	private String lastname;
}
TypeWithCompositeKey entity = ...

cassandraTemplate.selectOne(TypeWithCompositeKey.class, entity);

cassandraTemplate.selectOne(TypeWithCompositeKey.class, BasicMapId.id("firstname", "the-first-name").with("lastname", "the-last-name"));

The use of the entity as id is type-safe and possible for certain cases, but using the entity as it's identifier is not very common. MapId is a flexible way to specify id's, but it's not type-safe and error prone at the time of writing.

Using entities with composite Id's declared inside the entity with a dedicated Id class would benefit from type safety and a more clear usage guiding the user. A declaration could look like:

public class TypeWithCompositeKeyIdClass {

	private String firstname;
	private String lastname;
}

@PrimaryKeyClass(TypeWithCompositeKeyIdClass.class)
public class TypeWithCompositeKey {

	@PrimaryKeyColumn(type = PrimaryKeyType.PARTITIONED, ordinal = 1) 
	private String firstname;
	@PrimaryKeyColumn(type = PrimaryKeyType.PARTITIONED, ordinal = 2) 
	private String lastname;
}

The annotation @PrimaryKeyClass(TypeWithCompositeKeyIdClass.class) is added to the entity class itself referencing the Id class that can be used for Id-based operations. On startup, the mapping context will verify that the Id class fits the entity by verifying types and names of the properties. This validation consolidates the validation to the startup. Using the id class in the code provides type safety and property name safety to the user


No further details from DATACASS-324

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.