Skip to content

Commit f062194

Browse files
authored
JAVA-41261 Moved code of article spring-hibernate-n1-problem and Upda… (#18391)
* JAVA-41261 Moved code of article spring-hibernate-n1-problem and Updated all Readme * JAVA-41261 Moved code of article spring-boot-hikari and fixed integration issue of spring-boot-persistence-5
1 parent 8124653 commit f062194

File tree

65 files changed

+1833
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1833
-7
lines changed

persistence-modules/spring-boot-persistence-2/README.md

Lines changed: 2 additions & 2 deletions

persistence-modules/spring-boot-persistence-2/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@
126126
<artifactId>oracle-spring-boot-starter-ucp</artifactId>
127127
<version>${oracle-spring-boot-starter-ucp.version}</version>
128128
</dependency>
129+
<dependency>
130+
<groupId>io.hypersistence</groupId>
131+
<artifactId>hypersistence-utils-hibernate-62</artifactId>
132+
<version>${hypersistence-utils.version}</version>
133+
</dependency>
134+
<dependency>
135+
<groupId>com.vladmihalcea</groupId>
136+
<artifactId>db-util</artifactId>
137+
<version>${db.util.version}</version>
138+
</dependency>
129139

130140
</dependencies>
131141

@@ -150,6 +160,8 @@
150160
<junit-jupiter.version>5.9.3</junit-jupiter.version> <!-- Upgrading to latest version breaks the tests -->
151161
<spring-boot.version>3.1.5</spring-boot.version>
152162
<oracle-spring-boot-starter-ucp.version>24.2.0</oracle-spring-boot-starter-ucp.version>
163+
<hypersistence-utils.version>3.7.0</hypersistence-utils.version>
164+
<db.util.version>1.0.7</db.util.version>
153165
</properties>
154166

155167
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.listvsset;
2+
3+
import java.lang.reflect.ParameterizedType;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
public abstract class ParametrizationAware<T> {
8+
9+
public List<Class<T>> getParametrizationClass() {
10+
ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
11+
return Arrays.stream(type.getActualTypeArguments())
12+
.map(s -> ((Class<T>) s)).toList();
13+
}
14+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.listvsset;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.function.Predicate;
6+
import java.util.function.ToIntFunction;
7+
import org.springframework.data.jpa.repository.JpaRepository;
8+
import org.springframework.transaction.annotation.Transactional;
9+
10+
@Transactional
11+
public class Service<S> extends ParametrizationAware<S> {
12+
13+
private final JpaRepository<S, Long> repository;
14+
15+
public Service(JpaRepository<S, Long> repository) {
16+
this.repository = repository;
17+
}
18+
19+
public JpaRepository<S, Long> getRepository() {
20+
return repository;
21+
}
22+
23+
public int countNumberOfRequestsWithFunction(ToIntFunction<List<S>> function) {
24+
return function.applyAsInt(repository.findAll());
25+
}
26+
27+
28+
public Optional<S> getUserById(Long id) {
29+
return repository.findById(id);
30+
}
31+
32+
public void deleteAll() {
33+
repository.deleteAll();
34+
}
35+
36+
public List<S> saveAll(Iterable<S> entities) {
37+
return repository.saveAll(entities);
38+
}
39+
40+
public List<S> findAll() {
41+
return repository.findAll();
42+
}
43+
44+
public Optional<S> getUserByIdWithPredicate(long id, Predicate<S> predicate) {
45+
Optional<S> user = repository.findById(id);
46+
user.ifPresent(predicate::test);
47+
return user;
48+
}
49+
50+
public int getUserByIdWithFunction(Long id, ToIntFunction<S> function) {
51+
52+
Optional<S> optionalUser = repository.findById(id);
53+
if (optionalUser.isPresent()) {
54+
return function.applyAsInt(optionalUser.get());
55+
} else {
56+
return 0;
57+
}
58+
}
59+
60+
public void save(S entity) {
61+
repository.save(entity);
62+
}
63+
}

0 commit comments

Comments
 (0)