Skip to content

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

Open
@spring-projects-issues

Description

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

in: mappingMapping and conversion infrastructuretype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions