Skip to content

Commit fbbcec9

Browse files
committed
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
1 parent e93d850 commit fbbcec9

File tree

3 files changed

+4
-16
lines changed

3 files changed

+4
-16
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/mapping/PrimaryKeyClassEntityMetadataVerifier.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@ public void verify(CassandraPersistentEntity<?> entity) throws MappingException
6262
Table.class.getSimpleName(), PrimaryKeyClass.class.getSimpleName())));
6363
}
6464

65-
// Ensure PrimaryKeyClass only extends Object
66-
if (!entityType.getSuperclass().equals(Object.class)) {
67-
exceptions.add(
68-
new MappingException(String.format("@%s must only extend Object", PrimaryKeyClass.class.getSimpleName())));
69-
}
70-
7165
entity.forEach(property -> {
7266
if (property.isCompositePrimaryKey()) {
7367
compositePrimaryKeys.add(property);
@@ -93,7 +87,7 @@ public void verify(CassandraPersistentEntity<?> entity) throws MappingException
9387
entity.getType().getName(), PrimaryKeyColumn.class.getSimpleName())));
9488
}
9589

96-
// At least one of the PrimaryKeyColumns must have a type PARTIONED
90+
// At least one of the PrimaryKeyColumns must have a type PARTITIONED
9791
if (partitionKeyColumns.isEmpty()) {
9892
exceptions
9993
.add(new MappingException(String.format("At least one of the @%s annotations must have a type of PARTITIONED",

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/mapping/PrimaryKeyClassEntityMetadataVerifierUnitTests.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,9 @@ void shouldFailWithPrimaryKeyClassAndPrimaryKeyAnnotations() {
131131
}
132132
}
133133

134-
@Test // DATACASS-258
135-
void shouldFailForPrimaryKeyDerivedFromOtherThanObject() {
136-
137-
try {
134+
@Test // DATACASS-258, #1126
135+
void shouldAllowPrimaryKeyDerivedFromOtherThanObject() {
138136
verifier.verify(getEntity(SubclassPK.class));
139-
fail("Missing MappingException");
140-
} catch (MappingException e) {
141-
assertThat(e).hasMessageContaining("@PrimaryKeyClass must only extend Object");
142-
}
143137
}
144138

145139
@Test // DATACASS-213

src/main/asciidoc/reference/mapping.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ include::../{example-root}/LoginEvent.java[tags=class]
312312
==== Primary Key Class
313313

314314
A primary key class is a composite primary key class that is mapped to multiple fields or properties of the entity.
315-
It is annotated with `@PrimaryKeyClass` and must define `equals` and `hashCode` methods.
315+
It is annotated with `@PrimaryKeyClass` and should define `equals` and `hashCode` methods.
316316
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.
317317
Primary key classes can be used with repositories (as the `Id` type) and to represent an entity's identity in a single complex object.
318318
The following example shows a composite primary key class:

0 commit comments

Comments
 (0)