PrimaryKeyClass can now subclass other types than Object.

We dropped the requirement that composite keys may only subclass Object to allow usage with other superclasses such as Java records.

Closes #1126
This commit is contained in:
Mark Paluch
2021-05-06 09:09:02 +02:00
parent 25e5970bf5
commit a60b2c48cf
3 changed files with 4 additions and 16 deletions

View File

@@ -62,12 +62,6 @@ public class PrimaryKeyClassEntityMetadataVerifier implements CassandraPersisten
Table.class.getSimpleName(), PrimaryKeyClass.class.getSimpleName())));
}
// Ensure PrimaryKeyClass only extends Object
if (!entityType.getSuperclass().equals(Object.class)) {
exceptions.add(
new MappingException(String.format("@%s must only extend Object", PrimaryKeyClass.class.getSimpleName())));
}
entity.forEach(property -> {
if (property.isCompositePrimaryKey()) {
compositePrimaryKeys.add(property);
@@ -93,7 +87,7 @@ public class PrimaryKeyClassEntityMetadataVerifier implements CassandraPersisten
entity.getType().getName(), PrimaryKeyColumn.class.getSimpleName())));
}
// At least one of the PrimaryKeyColumns must have a type PARTIONED
// At least one of the PrimaryKeyColumns must have a type PARTITIONED
if (partitionKeyColumns.isEmpty()) {
exceptions
.add(new MappingException(String.format("At least one of the @%s annotations must have a type of PARTITIONED",

View File

@@ -131,15 +131,9 @@ class PrimaryKeyClassEntityMetadataVerifierUnitTests {
}
}
@Test // DATACASS-258
void shouldFailForPrimaryKeyDerivedFromOtherThanObject() {
try {
@Test // DATACASS-258, #1126
void shouldAllowPrimaryKeyDerivedFromOtherThanObject() {
verifier.verify(getEntity(SubclassPK.class));
fail("Missing MappingException");
} catch (MappingException e) {
assertThat(e).hasMessageContaining("@PrimaryKeyClass must only extend Object");
}
}
@Test // DATACASS-213

View File

@@ -312,7 +312,7 @@ include::../{example-root}/LoginEvent.java[tags=class]
==== Primary Key Class
A primary key class is a composite primary key class that is mapped to multiple fields or properties of the entity.
It is annotated with `@PrimaryKeyClass` and must define `equals` and `hashCode` methods.
It is annotated with `@PrimaryKeyClass` and should define `equals` and `hashCode` methods.
The semantics of value equality for these methods should be consistent with the database equality for the database types to which the key is mapped.
Primary key classes can be used with repositories (as the `Id` type) and to represent an entity's identity in a single complex object.
The following example shows a composite primary key class: