Skip to content

Package: Fetcher

Fetcher

nameinstructionbranchcomplexitylinemethod
fetch(Object, Function)
M: 0 C: 37
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * SolidBlue 3: Data safety
5: * http://tidalwave.it/projects/solidblue3
6: *
7: * Copyright (C) 2023 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/solidblue3j-src
23: * git clone https://github.com/tidalwave-it/solidblue3j-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.util.spring.jpa.impl;
28:
29: import jakarta.annotation.Nonnull;
30: import java.util.Collection;
31: import java.util.function.Function;
32: import jakarta.persistence.EntityManager;
33: import org.springframework.stereotype.Component;
34: import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35: import jakarta.transaction.Transactional;
36: import lombok.AllArgsConstructor;
37: import lombok.extern.slf4j.Slf4j;
38:
39: /***********************************************************************************************************************
40: *
41: * A convenience class to fetch relation collections of an Entity. Being a separate object, it is transactional even
42: * if called from outside a transactional context.
43: *
44: * @stereotype Repository
45: * @author Fabrizio Giudici
46: *
47: **********************************************************************************************************************/
48: @Component @AllArgsConstructor @Slf4j
49: public class Fetcher
50: {
51: @Nonnull
52: private final EntityManager em;
53:
54: /*******************************************************************************************************************
55: *
56: * Fetches elements in a lazy related collection in a transactional context.
57: *
58: * @param entity the entity
59: * @param function the function to extract the related collection
60: * @return the related collection
61: * @param <E> the static type of the entity
62: * @param <Q> the static type of the related JPA entity
63: * @param <R> the static type of the collection of Q
64: *
65: ******************************************************************************************************************/
66: @Transactional
67: @Nonnull @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
68: public <E, R extends Collection<Q>, Q> R fetch (@Nonnull final E entity,
69: @Nonnull final Function<? super E, R> function)
70: {
71: final var puUtil = em.getEntityManagerFactory().getPersistenceUnitUtil();
72: log.info("fetch({}, id={})", entity.getClass().getSimpleName(), puUtil.getIdentifier(entity));
73: final var result = function.apply(em.merge(entity));
74: result.size(); // trigger fetch
75: log.info(">>>> returning {} items", result.size());
76: log.trace(">>>> returning {}", result);
77: return result;
78: }
79: }