Skip to content

Commit

Permalink
Support entity with EmbeddedId
Browse files Browse the repository at this point in the history
  • Loading branch information
helgewessels committed Nov 11, 2024
1 parent 774ab0f commit 88e70fa
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ public abstract class RepositoryStub<T, ID> implements Repository<T, ID> {

/**
* Extracts the {@link org.springframework.data.annotation.Id} or {@link jakarta.persistence.Id}
* annotated field value from the entity.
* or {@link jakarta.persistence.EmbeddedId} annotated field value from the entity.
*
* @param entity the entity to extract the id from
* @return the value of the {@code Id} annotated field.
* @return the value of the {@code Id} or {@code EmbeddedId} annotated field.
*/
protected ID getId(T entity) {
for (Field field : entity.getClass().getDeclaredFields()) {
if (field.getAnnotation(org.springframework.data.annotation.Id.class) != null
|| field.getAnnotation(jakarta.persistence.Id.class) != null) {
|| field.getAnnotation(jakarta.persistence.Id.class) != null
|| field.getAnnotation(jakarta.persistence.EmbeddedId.class) != null) {
try {
field.setAccessible(true);
return (ID) field.get(entity);
Expand All @@ -36,6 +37,6 @@ protected ID getId(T entity) {
}
}

throw new IllegalArgumentException("No Id annotated field found in the entity");
throw new IllegalArgumentException("No Id or EmbeddedId annotated field found in the entity");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ void save() {
assertThat(repository.findById(entity.getUuid())).isPresent().get().isEqualTo(entity);
}

@Test
void save_embeddedId() {
var repository = new EmbeddedIdTestCrudRepositoryStub();

EmbeddedIdTestEntity entity = repository.save(EmbeddedIdTestEntity.randomEntity());

assertThat(repository.findById(entity.getId())).isPresent().get().isEqualTo(entity);
}

@Test
void saveAll() {
var repository = new TestCrudRepositoryStub();
Expand Down Expand Up @@ -107,6 +116,17 @@ void delete() {
assertThat(repository.existsById(entity.getUuid())).isFalse();
}

@Test
void delete_embeddedId() {
var repository = new EmbeddedIdTestCrudRepositoryStub();
var entity = repository.save(EmbeddedIdTestEntity.randomEntity());
assumeThat(repository.existsById(entity.getId())).isTrue();

repository.delete(entity);

assertThat(repository.existsById(entity.getId())).isFalse();
}

@Test
void deleteAllById() {
var repository = new TestCrudRepositoryStub();
Expand All @@ -129,6 +149,18 @@ void deleteAll_entities() {
assertThat(repository.count()).isZero();
}

@Test
void deleteAll_entities_embeddedId() {
var repository = new EmbeddedIdTestCrudRepositoryStub();
var entities =
repository.saveAll(
List.of(EmbeddedIdTestEntity.randomEntity(), EmbeddedIdTestEntity.randomEntity()));

repository.deleteAll(entities);

assertThat(repository.count()).isZero();
}

@Test
void deleteAll() {
var repository = new TestCrudRepositoryStub();
Expand All @@ -141,6 +173,9 @@ void deleteAll() {

private static class TestCrudRepositoryStub extends CrudRepositoryStub<TestEntity, UUID> {}

private static class EmbeddedIdTestCrudRepositoryStub
extends CrudRepositoryStub<EmbeddedIdTestEntity, EmbeddedIdTestEntity.Id> {}

private static List<UUID> idsOf(Iterable<TestEntity> entities) {
return stream(entities.spliterator(), false).map(TestEntity::getUuid).toList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.stubit.springdata;

import static java.util.UUID.randomUUID;

import jakarta.persistence.Embeddable;
import jakarta.persistence.EmbeddedId;
import java.util.Objects;
import java.util.UUID;
import org.stubit.random.RandomString;

class EmbeddedIdTestEntity {

@EmbeddedId private Id id;
private String name;

public EmbeddedIdTestEntity(Id id, String name) {
this.id = id;
this.name = name;
}

public static EmbeddedIdTestEntity randomEntity() {
return new EmbeddedIdTestEntity(new Id(randomUUID()), RandomString.latinLetters(8));
}

public Id getId() {
return id;
}

public void setId(Id id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EmbeddedIdTestEntity entity = (EmbeddedIdTestEntity) o;
return Objects.equals(id, entity.id) && Objects.equals(name, entity.name);
}

@Override
public int hashCode() {
return Objects.hash(id, name);
}

@Embeddable
static class Id {
private UUID uuid;

public Id() {}

public Id(UUID uuid) {
this.uuid = uuid;
}

public UUID getId() {
return uuid;
}

public void setId(UUID uuid) {
this.uuid = uuid;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Id entity = (Id) o;
return Objects.equals(uuid, entity.uuid);
}

@Override
public int hashCode() {
return Objects.hash(uuid);
}
}
}

0 comments on commit 88e70fa

Please sign in to comment.