feat(community): add findAllWithDetails query to MembershipRepository

Add @Query with JOIN FETCH on meteringPoint and energyCommunity, no WHERE clause
to return all memberships regardless of status. Includes reflection-based unit test
verifying method existence and annotation correctness.
This commit is contained in:
Bernhard Müller 2026-07-23 13:41:43 +02:00
parent a651ec946c
commit c5ae6032c9
2 changed files with 44 additions and 0 deletions

View File

@ -37,6 +37,9 @@ public interface MembershipRepository extends JpaRepository<Membership, UUID> {
@Query("SELECT m FROM Membership m JOIN FETCH m.meteringPoint mp JOIN FETCH m.energyCommunity ec WHERE m.status = 'PENDING'") @Query("SELECT m FROM Membership m JOIN FETCH m.meteringPoint mp JOIN FETCH m.energyCommunity ec WHERE m.status = 'PENDING'")
List<Membership> findPendingWithDetails(); List<Membership> findPendingWithDetails();
@Query("SELECT m FROM Membership m JOIN FETCH m.meteringPoint mp JOIN FETCH m.energyCommunity ec")
List<Membership> findAllWithDetails();
@Query("SELECT mp.userId FROM Membership m JOIN m.meteringPoint mp WHERE m.energyCommunity.id = :communityId AND m.status != 'INACTIVE'") @Query("SELECT mp.userId FROM Membership m JOIN m.meteringPoint mp WHERE m.energyCommunity.id = :communityId AND m.status != 'INACTIVE'")
List<UUID> findActiveMemberUserIdsByCommunityId(@Param("communityId") UUID communityId); List<UUID> findActiveMemberUserIdsByCommunityId(@Param("communityId") UUID communityId);

View File

@ -0,0 +1,41 @@
package at.mueller.eeg.backend.community.repository;
import at.mueller.eeg.backend.community.domain.Membership;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class MembershipRepositoryTest {
@Test
void findAllWithDetails_methodExists() throws NoSuchMethodException {
Method method = MembershipRepository.class.getMethod("findAllWithDetails");
assertNotNull(method);
assertEquals(List.class, method.getReturnType());
}
@Test
void findAllWithDetails_hasQueryAnnotation() {
Method method = null;
try {
method = MembershipRepository.class.getMethod("findAllWithDetails");
} catch (NoSuchMethodException e) {
fail("findAllWithDetails method not found on MembershipRepository");
}
org.springframework.data.jpa.repository.Query queryAnnotation =
method.getAnnotation(org.springframework.data.jpa.repository.Query.class);
assertNotNull(queryAnnotation, "findAllWithDetails should have @Query annotation");
String queryValue = queryAnnotation.value();
assertTrue(queryValue.contains("JOIN FETCH m.meteringPoint"),
"Query should JOIN FETCH meteringPoint");
assertTrue(queryValue.contains("JOIN FETCH m.energyCommunity"),
"Query should JOIN FETCH energyCommunity");
assertFalse(queryValue.contains("WHERE"),
"Query should not have WHERE clause (returns all statuses)");
}
}