Skip to content

Commit

Permalink
Support Spring Data JpaRepository#getReferenceById(ID)
Browse files Browse the repository at this point in the history
  • Loading branch information
aureamunoz committed Jul 19, 2024
1 parent 0aa268a commit 29a679a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void add(ClassCreator classCreator, FieldDescriptor entityClassFieldDescr
allMethodsToBeImplementedToResult);
generateExistsById(classCreator, entityClassFieldDescriptor, generatedClassName, entityTypeStr, idTypeStr,
allMethodsToBeImplementedToResult);
generateGetOne(classCreator, entityClassFieldDescriptor, generatedClassName, entityTypeStr, idTypeStr,
generateGetReferenceById(classCreator, entityClassFieldDescriptor, generatedClassName, entityTypeStr, idTypeStr,
allMethodsToBeImplementedToResult);
generateFindAll(classCreator, entityClassFieldDescriptor, generatedClassName, entityTypeStr,
allMethodsToBeImplementedToResult);
Expand Down Expand Up @@ -505,42 +505,42 @@ private void generateExistsById(ClassCreator classCreator, FieldDescriptor entit
}
}

private void generateGetOne(ClassCreator classCreator, FieldDescriptor entityClassFieldDescriptor,
private void generateGetReferenceById(ClassCreator classCreator, FieldDescriptor entityClassFieldDescriptor,
String generatedClassName, String entityTypeStr, String idTypeStr,
Map<MethodDescriptor, Boolean> allMethodsToBeImplementedToResult) {

MethodDescriptor getOneDescriptor = MethodDescriptor.ofMethod(generatedClassName, "getOne",
MethodDescriptor getReferenceByIdDescriptor = MethodDescriptor.ofMethod(generatedClassName, "getReferenceById",
entityTypeStr, idTypeStr);
MethodDescriptor bridgeGetOneDescriptor = MethodDescriptor.ofMethod(generatedClassName, "getOne",
MethodDescriptor bridgegetReferenceByIdDescriptor = MethodDescriptor.ofMethod(generatedClassName, "getReferenceById",
Object.class, Object.class);

if (allMethodsToBeImplementedToResult.containsKey(getOneDescriptor)
|| allMethodsToBeImplementedToResult.containsKey(bridgeGetOneDescriptor)) {
if (allMethodsToBeImplementedToResult.containsKey(getReferenceByIdDescriptor)
|| allMethodsToBeImplementedToResult.containsKey(bridgegetReferenceByIdDescriptor)) {

if (!classCreator.getExistingMethods().contains(getOneDescriptor)) {
try (MethodCreator findById = classCreator.getMethodCreator(getOneDescriptor)) {
if (!classCreator.getExistingMethods().contains(getReferenceByIdDescriptor)) {
try (MethodCreator findById = classCreator.getMethodCreator(getReferenceByIdDescriptor)) {

ResultHandle entity = findById.invokeStaticMethod(ofMethod(RepositorySupport.class, "getOne",
ResultHandle entity = findById.invokeStaticMethod(ofMethod(RepositorySupport.class, "getReferenceById",
Object.class, AbstractJpaOperations.class, Class.class, Object.class),
findById.readStaticField(operationsField),
findById.readInstanceField(entityClassFieldDescriptor, findById.getThis()),
findById.getMethodParam(0));

findById.returnValue(entity);
}
try (MethodCreator bridgeGetOne = classCreator.getMethodCreator(bridgeGetOneDescriptor)) {
MethodDescriptor getOne = MethodDescriptor.ofMethod(generatedClassName, "getOne",
try (MethodCreator bridgeGetOne = classCreator.getMethodCreator(bridgegetReferenceByIdDescriptor)) {
MethodDescriptor getReferenceById = MethodDescriptor.ofMethod(generatedClassName, "getReferenceById",
entityTypeStr, idTypeStr);
ResultHandle methodParam = bridgeGetOne.getMethodParam(0);
ResultHandle castedMethodParam = bridgeGetOne.checkCast(methodParam, idTypeStr);
ResultHandle result = bridgeGetOne.invokeVirtualMethod(getOne, bridgeGetOne.getThis(),
ResultHandle result = bridgeGetOne.invokeVirtualMethod(getReferenceById, bridgeGetOne.getThis(),
castedMethodParam);
bridgeGetOne.returnValue(result);
}
}

allMethodsToBeImplementedToResult.put(getOneDescriptor, true);
allMethodsToBeImplementedToResult.put(bridgeGetOneDescriptor, true);
allMethodsToBeImplementedToResult.put(getReferenceByIdDescriptor, true);
allMethodsToBeImplementedToResult.put(bridgegetReferenceByIdDescriptor, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
Expand All @@ -21,6 +22,7 @@
import org.hibernate.Hibernate;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
Expand All @@ -31,6 +33,7 @@
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;

import io.quarkus.spring.data.runtime.FunctionalityNotImplemented;
import io.quarkus.test.QuarkusUnitTest;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
Expand Down Expand Up @@ -148,6 +151,35 @@ public void testListOrderByNullHandling() throws MalformedURLException {
assertEquals(uuidForTheNullUrlRecord, list.get(list.size() - 1).getUuid());
}

@Test
@Order(9)
@Transactional
public void testGetReferenceById() throws Exception {
populateData(new BasicTypeData());
assertThat(repo.getReferenceById(1)).isNotNull();
}

@Test
@Order(10)
@Transactional
public void testGetOneNotImplemented() throws Exception {
Assertions.assertThrows(FunctionalityNotImplemented.class, () -> repo.getOne(1));
}

@Test
@Order(11)
@Transactional
public void testGetByIdNotImplemented() throws Exception {
Assertions.assertThrows(FunctionalityNotImplemented.class, () -> repo.getById(1));
}

@Test
@Order(12)
@Transactional
public void testDeleteInBatchNotImplemented() throws Exception {
Assertions.assertThrows(FunctionalityNotImplemented.class, () -> repo.deleteInBatch(new ArrayList<>()));
}

private BasicTypeData populateData(BasicTypeData basicTypeData) throws MalformedURLException {
basicTypeData.setDoubleValue(Math.PI);
basicTypeData.setBigDecimalValue(BigDecimal.valueOf(Math.PI * 2.0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,25 @@ public static void deleteAll(AbstractJpaOperations<PanacheQuery<?>> operations,
}
}

/**
* Add call to the Panache method implementing the actual retrieving of a reference to an entity with the given class and
* identifier.
*
* @param operations an instance of {@code AbstractJpaOperations} used to perform JPA operations
* @param entityClass the {@code Class} object of the entity type to be retrieved
* @param id the identifier of the entity to be retrieved
* @return a reference to the entity of the specified class with the given identifier
* @deprecated use {@link RepositorySupport#getReferenceById)} instead.
*/
@Deprecated
public static Object getOne(AbstractJpaOperations<PanacheQuery<?>> operations, Class<?> entityClass, Object id) {
return operations.getSession(entityClass).getReference(entityClass, id);
}

public static Object getReferenceById(AbstractJpaOperations<PanacheQuery<?>> operations, Class<?> entityClass, Object id) {
return operations.getSession(entityClass).getReference(entityClass, id);
}

public static void clear(Class<?> clazz) {
Panache.getSession(clazz).clear();
}
Expand Down

0 comments on commit 29a679a

Please sign in to comment.