Skip to content

Package: JpaPersistableSupport

JpaPersistableSupport

nameinstructionbranchcomplexitylinemethod
findAll()
M: 0 C: 25
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
findById(Id)
M: 0 C: 27
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
persist()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
remove()
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2025 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 the License.
12: * 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 an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
22: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.thesefoolishthings.examples.dci.persistable.jpa.role.impl;
27:
28: import javax.annotation.Nonnull;
29: import java.util.List;
30: import java.util.Optional;
31: import java.util.function.Function;
32: import java.util.stream.Collectors;
33: import it.tidalwave.util.Id;
34: import it.tidalwave.role.Removable;
35: import it.tidalwave.role.io.Persistable;
36: import it.tidalwave.thesefoolishthings.examples.dci.persistable.jpa.role.Findable;
37: import lombok.AllArgsConstructor;
38:
39: /***************************************************************************************************************************************************************
40: *
41: * @author Fabrizio Giudici
42: *
43: **************************************************************************************************************************************************************/
44: @AllArgsConstructor
45: public abstract class JpaPersistableSupport<T, E> implements Persistable, Removable, Findable<T>
46: {
47: @Nonnull
48: private final T datum;
49:
50: @Nonnull
51: private final Class<E> entityClass;
52:
53: @Nonnull
54: private final Function<E, T> fromEntity;
55:
56: @Nonnull
57: private final Function<T, E> toEntity;
58:
59: @Nonnull
60: private final JpaPersistenceContext context;
61:
62: @Override
63: public void persist()
64: {
65: context.persist(toEntity.apply(datum));
66: }
67:
68: @Override
69: public void remove()
70: {
71: context.remove(toEntity.apply(datum));
72: }
73:
74: @Override @Nonnull
75: public List<T> findAll()
76: {
77: final var query = "SELECT p FROM %s p";
78: return context.createQuery(String.format(query, entityClass.getSimpleName()), entityClass)
79: .getResultStream()
80: .map(fromEntity)
81: .collect(Collectors.toList());
82: }
83:
84: @Override @Nonnull
85: public Optional<T> findById (@Nonnull final Id id)
86: {
87: final var query = "SELECT p FROM %s p WHERE p.id = :id";
88: return context.createQuery(String.format(query, entityClass.getSimpleName()), entityClass)
89: .setParameter("id", id.stringValue())
90: .getResultStream()
91: .map(fromEntity)
92: .findFirst();
93: }
94: }