Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
spring-projects-issues opened this issue Jul 22, 2016 · 0 comments
Assignees
Labels
in: mapping Mapping and conversion infrastructure type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

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

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement in: mapping Mapping and conversion infrastructure labels Dec 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: mapping Mapping and conversion infrastructure type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants