Skip to content

Commit

Permalink
Composite key for jps repo with @IdClass
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Dec 14, 2023
1 parent 1539beb commit de4a132
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public class Annotations {
public static final String NO_REPO_BEAN = "org.springframework.data.repository.NoRepositoryBean";
public static final String SPRING_ENTITY_ID = "org.springframework.data.annotation.Id";
public static final String JPA_JAKARTA_ENTITY_ID = "jakarta.persistence.Id";
public static final String JPA_JAVAX_ENTITY_ID = "javax.persistence.Id";
public static final String JPA_JAVAX_ENTITY_ID = "javax.persistence.Id";
public static final String JPA_JAKARTA_EMBEDDED_ID = "jakarta.persistence.EmbeddedId";
public static final String JPA_JAVAX_EMBEDDED_ID = "javax.persistence.EmbeddedId";
public static final String JPA_JAKARTA_ID_CLASS = "jakarta.persistence.IdClass";
public static final String JPA_JAVAX_ID_CLASS = "javax.persistence.IdClass";

public static final String AUTOWIRED = "org.springframework.beans.factory.annotation.Autowired";
public static final String INJECT = "javax.inject.Inject";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -331,7 +332,17 @@ private static ITypeBinding findIdFieldType(ITypeBinding type) {

private static List<ITypeBinding> findAnnotatedIdTypes(ITypeBinding type, Set<String> visited) {
List<ITypeBinding> idTypes = new ArrayList<>();
List<String> idAnnotations = List.of(Annotations.SPRING_ENTITY_ID, Annotations.JPA_JAKARTA_ENTITY_ID, Annotations.JPA_JAVAX_ENTITY_ID);
for (IAnnotationBinding a : type.getAnnotations()) {
switch (a.getAnnotationType().getQualifiedName()) {
case Annotations.JPA_JAKARTA_ID_CLASS:
case Annotations.JPA_JAVAX_ID_CLASS:
Optional<Object> opt = Arrays.stream(a.getAllMemberValuePairs()).filter(p -> "value".equals(p.getName())).map(p -> p.getValue()).findFirst();
if (opt.isPresent() && opt.get() instanceof ITypeBinding) {
return List.of((ITypeBinding) opt.get());
}
}
}
List<String> idAnnotations = List.of(Annotations.SPRING_ENTITY_ID, Annotations.JPA_JAKARTA_ENTITY_ID, Annotations.JPA_JAVAX_ENTITY_ID, Annotations.JPA_JAKARTA_EMBEDDED_ID, Annotations.JPA_JAVAX_EMBEDDED_ID);
for (IVariableBinding m : type.getDeclaredFields()) {
String s = fieldSignature(m);
if (!visited.contains(s) && isAnnotationCompatible(m.getAnnotations(), idAnnotations)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1047,16 +1047,15 @@ interface EmployeeRepository {}
}

@Test
void compositeId() throws Exception {
void compositeId_1() throws Exception {

Path customerSource = createFile("Customer.java", """
package demo;
import org.springframework.data.annotation.Id;
import jakarta.persistence.IdClass;
@IdClass(CustomerId.class)
public class Customer {
@Id String id;
@Id String id_additional;
}
""");

Expand All @@ -1078,5 +1077,45 @@ interface CustomerRepository extends Repository<Customer, CustomerId> {}
assertEquals(0, problems.size());

}


@Test
void compositeId_2() throws Exception {

Path customerSource = createFile("Customer.java", """
package demo;
import jakarta.persistence.IdClass;
import org.springframework.data.annotation.Id;
@IdClass(CustomerId.class)
public class Customer {
@Id Long id
}
""");

Path customerId = createFile("CustomerId.java", """
package demo;
public record CustomerId(String id) {}
""");

String source = """
package demo;
import org.springframework.data.repository.Repository;
interface CustomerRepository extends Repository<Customer, Long> {}
""";
List<ReconcileProblem> problems = reconcile("CustomerRepository.java", source, false, customerSource, customerId);

assertEquals(1, problems.size());
ReconcileProblem problem = problems.get(0);

assertEquals(Boot2JavaProblemType.DOMAIN_ID_FOR_REPOSITORY, problem.getType());

String markedStr = source.substring(problem.getOffset(), problem.getOffset() + problem.getLength());
assertEquals("Long", markedStr);
assertEquals("Expected Domain ID type is 'demo.CustomerId'", problem.getMessage());

}
}

0 comments on commit de4a132

Please sign in to comment.