Merge branch 'master' into BAEL-6421-PrintWriter-write-vs-print
This commit is contained in:
commit
e4aa7a1206
|
@ -0,0 +1,82 @@
|
|||
package com.baeldung.algorithms.happynumber;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class HappyNumberDecider {
|
||||
|
||||
public static boolean isHappyNumber(int n) {
|
||||
Set<Integer> checkedNumbers = new HashSet<>();
|
||||
while (true) {
|
||||
n = sumDigitsSquare(n);
|
||||
if (n == 1) {
|
||||
return true;
|
||||
}
|
||||
if (checkedNumbers.contains(n)) {
|
||||
return false;
|
||||
}
|
||||
checkedNumbers.add(n);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isHappyNumberFloyd(int n) {
|
||||
int slow = n;
|
||||
int fast = n;
|
||||
do {
|
||||
slow = sumDigitsSquare(slow);
|
||||
fast = sumDigitsSquare(sumDigitsSquare(fast));
|
||||
} while (slow != fast);
|
||||
|
||||
return slow == 1;
|
||||
}
|
||||
|
||||
private static int sumDigitsSquare(int n) {
|
||||
int squareSum = 0;
|
||||
while (n != 0) {
|
||||
squareSum += (n % 10) * (n % 10);
|
||||
n /= 10;
|
||||
}
|
||||
return squareSum;
|
||||
}
|
||||
}
|
||||
|
||||
public class HappyNumberUnitTest {
|
||||
|
||||
@Test
|
||||
void whenUsingIsHappyNumber_thenGetTheExpectedResult() {
|
||||
assertTrue(HappyNumberDecider.isHappyNumber(7));
|
||||
assertTrue(HappyNumberDecider.isHappyNumber(10));
|
||||
assertTrue(HappyNumberDecider.isHappyNumber(13));
|
||||
assertTrue(HappyNumberDecider.isHappyNumber(19));
|
||||
assertTrue(HappyNumberDecider.isHappyNumber(23));
|
||||
|
||||
assertFalse(HappyNumberDecider.isHappyNumber(4));
|
||||
assertFalse(HappyNumberDecider.isHappyNumber(6));
|
||||
assertFalse(HappyNumberDecider.isHappyNumber(11));
|
||||
assertFalse(HappyNumberDecider.isHappyNumber(15));
|
||||
assertFalse(HappyNumberDecider.isHappyNumber(20));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingIsHappyNumber2_thenGetTheExpectedResult() {
|
||||
assertTrue(HappyNumberDecider.isHappyNumberFloyd(7));
|
||||
assertTrue(HappyNumberDecider.isHappyNumberFloyd(10));
|
||||
assertTrue(HappyNumberDecider.isHappyNumberFloyd(13));
|
||||
assertTrue(HappyNumberDecider.isHappyNumberFloyd(19));
|
||||
assertTrue(HappyNumberDecider.isHappyNumberFloyd(23));
|
||||
|
||||
assertFalse(HappyNumberDecider.isHappyNumberFloyd(4));
|
||||
assertFalse(HappyNumberDecider.isHappyNumberFloyd(6));
|
||||
assertFalse(HappyNumberDecider.isHappyNumberFloyd(11));
|
||||
assertFalse(HappyNumberDecider.isHappyNumberFloyd(15));
|
||||
assertFalse(HappyNumberDecider.isHappyNumberFloyd(20));
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
### Relevant Articles:
|
||||
- [Vigenère Cipher in Java](https://www.baeldung.com/java-vigenere-cipher)
|
|
@ -87,6 +87,7 @@
|
|||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.immutables;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ImmutableCollectionsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenUnmodifiableMap_whenPutNewEntry_thenThrowsUnsupportedOperationException() {
|
||||
Map<String, String> modifiableMap = new HashMap<>();
|
||||
modifiableMap.put("name1", "Michael");
|
||||
modifiableMap.put("name2", "Harry");
|
||||
|
||||
Map<String, String> unmodifiableMap = Collections.unmodifiableMap(modifiableMap);
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> unmodifiableMap.put("name3", "Micky"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenUnmodifiableMap_whenPutNewEntryUsingOriginalReference_thenSuccess() {
|
||||
Map<String, String> modifiableMap = new HashMap<>();
|
||||
modifiableMap.put("name1", "Michael");
|
||||
modifiableMap.put("name2", "Harry");
|
||||
|
||||
Map<String, String> unmodifiableMap = Collections.unmodifiableMap(modifiableMap);
|
||||
modifiableMap.put("name3", "Micky");
|
||||
|
||||
assertEquals(modifiableMap, unmodifiableMap);
|
||||
assertTrue(unmodifiableMap.containsKey("name3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenImmutableMap_whenPutNewEntry_thenThrowsUnsupportedOperationException() {
|
||||
Map<String, String> immutableMap = Map.of("name1", "Michael", "name2", "Harry");
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> immutableMap.put("name3", "Micky"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenImmutableMap_whenUsecopyOf_thenExceptionOnPut() {
|
||||
Map<String, String> immutableMap = Map.of("name1", "Michael", "name2", "Harry");
|
||||
Map<String, String> copyOfImmutableMap = Map.copyOf(immutableMap);
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> copyOfImmutableMap.put("name3", "Micky"));
|
||||
}
|
||||
}
|
||||
|
|
@ -5,3 +5,4 @@
|
|||
- [Sorting One List Based on Another List in Java](https://www.baeldung.com/java-sorting-one-list-using-another)
|
||||
- [Reset ListIterator to First Element of the List in Java](https://www.baeldung.com/java-reset-listiterator)
|
||||
- [Modify and Print List Items With Java Streams](https://www.baeldung.com/java-stream-list-update-print-elements)
|
||||
- [Add One Element to an Immutable List in Java](https://www.baeldung.com/java-immutable-list-add-element)
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.addtoimmutablelist;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class AddElementsToImmutableListUnitTest {
|
||||
|
||||
public static <T> List<T> appendAnElement(List<T> immutableList, T element) {
|
||||
List<T> tmpList = new ArrayList<>(immutableList);
|
||||
tmpList.add(element);
|
||||
return Collections.unmodifiableList(tmpList);
|
||||
}
|
||||
|
||||
@SafeVarargs
|
||||
public static <T> List<T> appendElements(List<T> immutableList, T... elements) {
|
||||
List<T> tmpList = new ArrayList<>(immutableList);
|
||||
tmpList.addAll(Arrays.asList(elements));
|
||||
return Collections.unmodifiableList(tmpList);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCallingAppendAnElement_thenGetExpectedResult() {
|
||||
List<String> myList = List.of("A", "B", "C", "D", "E");
|
||||
List<String> expected = List.of("A", "B", "C", "D", "E", "F");
|
||||
List<String> result = appendAnElement(myList, "F");
|
||||
assertThat(result).isEqualTo(expected)
|
||||
.isUnmodifiable();
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenCallingAppendElements_thenGetExpectedResult() {
|
||||
List<String> myList = List.of("A", "B", "C", "D", "E");
|
||||
List<String> expected1 = List.of("A", "B", "C", "D", "E", "F");
|
||||
List<String> result1 = appendElements(myList, "F");
|
||||
assertThat(result1).isEqualTo(expected1)
|
||||
.isUnmodifiable();
|
||||
|
||||
List<String> expected2 = List.of("A", "B", "C", "D", "E", "F", "G", "H", "I");
|
||||
List<String> result2 = appendElements(myList, "F", "G", "H", "I");
|
||||
assertThat(result2).isEqualTo(expected2)
|
||||
.isUnmodifiable();
|
||||
}
|
||||
}
|
|
@ -10,5 +10,6 @@ This module contains articles about core Java input and output (IO)
|
|||
- [Read a File and Split It Into Multiple Files in Java](https://www.baeldung.com/java-read-file-split-into-several)
|
||||
- [Read and Write Files in Java Using Separate Threads](https://www.baeldung.com/java-read-write-files-different-threads)
|
||||
- [Convert an OutputStream to a Byte Array in Java](https://www.baeldung.com/java-outputstream-byte-array)
|
||||
- [Reading a .gz File Line by Line Using GZIPInputStream](https://www.baeldung.com/java-gzipinputstream-read-gz-file-line-by-line)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-io-4)
|
||||
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
package com.baeldung.usinggzipInputstream;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class Main {
|
||||
static String filePath = Objects.requireNonNull(Main.class.getClassLoader().getResource("myFile.gz")).getFile();
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
// Test readGZipFile method
|
||||
List<String> fileContents = readGZipFile(filePath);
|
||||
System.out.println("Contents of GZIP file:");
|
||||
fileContents.forEach(System.out::println);
|
||||
|
||||
// Test findInZipFile method
|
||||
String searchTerm = "Line 1 content";
|
||||
List<String> foundLines = findInZipFile(filePath, searchTerm);
|
||||
System.out.println("Lines containing '" + searchTerm + "' in GZIP file:");
|
||||
foundLines.forEach(System.out::println);
|
||||
|
||||
|
||||
// Test useContentsOfZipFile method
|
||||
System.out.println("Using contents of GZIP file with consumer:");
|
||||
useContentsOfZipFile(filePath, linesStream -> {
|
||||
linesStream.filter(line -> line.length() > 10).forEach(System.out::println);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public static List<String> readGZipFile(String filePath) throws IOException {
|
||||
List<String> lines = new ArrayList<>();
|
||||
try (InputStream inputStream = new FileInputStream(filePath);
|
||||
GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(gzipInputStream);
|
||||
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
|
||||
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
lines.add(line);
|
||||
}
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
public static List<String> findInZipFile(String filePath, String toFind) throws IOException {
|
||||
try (InputStream inputStream = new FileInputStream(filePath);
|
||||
GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(gzipInputStream);
|
||||
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
|
||||
|
||||
return bufferedReader.lines().filter(line -> line.contains(toFind)).collect(toList());
|
||||
}
|
||||
}
|
||||
|
||||
public static void useContentsOfZipFile(String filePath, Consumer<Stream<String>> consumer) throws IOException {
|
||||
try (InputStream inputStream = new FileInputStream(filePath);
|
||||
GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(gzipInputStream);
|
||||
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
|
||||
|
||||
consumer.accept(bufferedReader.lines());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.usinggzipInputstream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ReadingGZIPUsingGZIPInputStreamUnitTest {
|
||||
String testFilePath = Objects.requireNonNull(ReadingGZIPUsingGZIPInputStreamUnitTest.class.getClassLoader().getResource("myFile.gz")).getFile();
|
||||
List<String> expectedFilteredLines = Arrays.asList("Line 1 content", "Line 2 content", "Line 3 content");
|
||||
|
||||
@Test
|
||||
void givenGZFile_whenUsingGZIPInputStream_thenReadLines() throws IOException {
|
||||
try (Stream<String> lines = Main.readGZipFile(testFilePath).stream()) {
|
||||
List<String> result = lines
|
||||
.filter(expectedFilteredLines::contains)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(expectedFilteredLines, result);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenGZFile_whenUsingtestFindInZipFile_thenReadLines() throws IOException {
|
||||
String toFind = "Line 1 content";
|
||||
|
||||
List<String> result = Main.findInZipFile(testFilePath, toFind);
|
||||
|
||||
assertEquals("Line 1 content", result.get(0));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenGZFile_whenUsingContentsOfZipFile_thenReadLines() throws IOException {
|
||||
AtomicInteger count = new AtomicInteger(0);
|
||||
|
||||
Main.useContentsOfZipFile(testFilePath, linesStream -> {
|
||||
linesStream.filter(line -> line.length() > 10).forEach(line -> count.incrementAndGet());
|
||||
});
|
||||
|
||||
assertEquals(3, count.get());
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -72,4 +72,5 @@
|
|||
<properties>
|
||||
<junit-jupiter-version>5.9.3</junit-jupiter-version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.inputstreamreader;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
public class InputStreamReaderUnitTest {
|
||||
@Test
|
||||
public void givenAStringWrittenToAFile_whenReadByInputStreamReader_thenShouldMatchWhenRead(@TempDir Path tempDir) throws IOException {
|
||||
String sampleTxt = "Good day. This is just a test. Good bye.";
|
||||
Path sampleOut = tempDir.resolve("sample-out.txt");
|
||||
List<String> lines = Arrays.asList(sampleTxt);
|
||||
Files.write(sampleOut, lines);
|
||||
String absolutePath = String.valueOf(sampleOut.toAbsolutePath());
|
||||
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(absolutePath), StandardCharsets.UTF_8)) {
|
||||
boolean isMatched = false;
|
||||
int b;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while ((b = reader.read()) != -1) {
|
||||
sb.append((char) b);
|
||||
if (sb.toString().contains(sampleTxt)) {
|
||||
isMatched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assertThat(isMatched).isTrue();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,4 +4,5 @@ This module contains article about constructors in Java
|
|||
|
||||
### Relevant Articles:
|
||||
- [Different Ways to Create an Object in Java](https://www.baeldung.com/java-different-ways-to-create-objects)
|
||||
- [When to Use Setter Methods or Constructors for Setting a Variable’s Value in Java](https://www.baeldung.com/java-setter-method-vs-constructor)
|
||||
- More articles: [[<-- Prev]](/core-java-modules/core-java-lang-oop-constructors)
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.statementsbeforesuper;
|
||||
|
||||
class Child extends Parent {
|
||||
Child() {
|
||||
super(); // Or super(10); Correct placements
|
||||
System.out.println("Child constructor");
|
||||
additionalInitialization();
|
||||
// super(); Compilation error: Constructor call must be the first statement in a constructor
|
||||
}
|
||||
|
||||
private void additionalInitialization() {
|
||||
System.out.println("Additional initialization in Child");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.statementsbeforesuper;
|
||||
|
||||
public class Parent {
|
||||
public Parent(int id) {
|
||||
System.out.println("Parametrized Parent constructor");
|
||||
}
|
||||
|
||||
public Parent() {
|
||||
System.out.println("Parent constructor");
|
||||
}
|
||||
}
|
|
@ -49,7 +49,7 @@
|
|||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<version>0.8.8</version>
|
||||
<version>${jacoco-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
|
@ -73,6 +73,7 @@
|
|||
<source.version>1.8</source.version>
|
||||
<target.version>1.8</target.version>
|
||||
<spring.version>5.3.4</spring.version>
|
||||
<jacoco-maven-plugin.version>0.8.11</jacoco-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -49,7 +49,7 @@
|
|||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<version>0.8.8</version>
|
||||
<version>${jacoco-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
|
@ -73,6 +73,7 @@
|
|||
<source.version>1.8</source.version>
|
||||
<target.version>1.8</target.version>
|
||||
<spring.version>5.3.4</spring.version>
|
||||
<jacoco-maven-plugin.version>0.8.11</jacoco-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,3 +1,4 @@
|
|||
## Relevant Articles:
|
||||
- [Handle Duplicate Keys When Producing Map Using Java Stream](https://www.baeldung.com/java-duplicate-keys-when-producing-map-using-stream)
|
||||
- [Convert a Stream into a Map or Multimap in Java](https://www.baeldung.com/java-convert-stream-map-multimap)
|
||||
- [Flatten a Stream of Maps to a Single Map in Java](https://www.baeldung.com/java-flatten-stream-map)
|
||||
|
|
|
@ -0,0 +1,143 @@
|
|||
package com.baeldung.streams.mapstreamtomap;
|
||||
|
||||
import static java.lang.Math.max;
|
||||
import static java.util.stream.Collectors.flatMapping;
|
||||
import static java.util.stream.Collectors.groupingBy;
|
||||
import static java.util.stream.Collectors.mapping;
|
||||
import static java.util.stream.Collectors.reducing;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class MapStreamToMapUnitTest {
|
||||
|
||||
Map<String, Integer> playerMap1 = new HashMap<String, Integer>() {{
|
||||
put("Kai", 92);
|
||||
put("Liam", 100);
|
||||
}};
|
||||
Map<String, Integer> playerMap2 = new HashMap<String, Integer>() {{
|
||||
put("Eric", 42);
|
||||
put("Kevin", 77);
|
||||
}};
|
||||
Map<String, Integer> playerMap3 = new HashMap<String, Integer>() {{
|
||||
put("Saajan", 35);
|
||||
}};
|
||||
Map<String, Integer> playerMap4 = new HashMap<String, Integer>() {{
|
||||
put("Kai", 76);
|
||||
}};
|
||||
Map<String, Integer> playerMap5 = new HashMap<String, Integer>() {{
|
||||
put("Kai", null);
|
||||
put("Jerry", null);
|
||||
}};
|
||||
|
||||
@Test
|
||||
void givenMapsStream_whenUsingFlatMapAndToMap_thenMultipleMapsMergedIntoOneMap() {
|
||||
|
||||
Map<String, Integer> expectedMap = new HashMap<String, Integer>() {{
|
||||
put("Saajan", 35);
|
||||
put("Liam", 100);
|
||||
put("Kai", 92);
|
||||
put("Eric", 42);
|
||||
put("Kevin", 77);
|
||||
}};
|
||||
|
||||
Map<String, Integer> mergedMap = Stream.of(playerMap1, playerMap2, playerMap3)
|
||||
.flatMap(map -> map.entrySet()
|
||||
.stream())
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
|
||||
assertEquals(expectedMap, mergedMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMapsWithDuplicateKeys_whenUsingFlatMapAndToMap_thenMultipleMapsMergedIntoOneMap() {
|
||||
|
||||
Map<String, Integer> expectedMap = new HashMap<String, Integer>() {{
|
||||
put("Saajan", 35);
|
||||
put("Liam", 100);
|
||||
put("Kai", 92); // max of 76 and 92
|
||||
put("Eric", 42);
|
||||
put("Kevin", 77);
|
||||
}};
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> Stream.of(playerMap1, playerMap2, playerMap3, playerMap4)
|
||||
.flatMap(map -> map.entrySet()
|
||||
.stream())
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)), "Duplicate key Kai (attempted merging values 92 and 76)");
|
||||
|
||||
Map<String, Integer> mergedMap = Stream.of(playerMap1, playerMap2, playerMap3, playerMap4)
|
||||
.flatMap(map -> map.entrySet()
|
||||
.stream())
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, Integer::max));
|
||||
|
||||
assertEquals(expectedMap, mergedMap);
|
||||
}
|
||||
|
||||
private Integer maxInteger(Integer int1, Integer int2) {
|
||||
if (int1 == null) {
|
||||
return int2;
|
||||
}
|
||||
if (int2 == null) {
|
||||
return int1;
|
||||
}
|
||||
return max(int1, int2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMapsWithDuplicateKeysAndNullValues_whenUsingFlatMapWithForEach_thenMultipleMapsMergedIntoOneMap() {
|
||||
|
||||
Map<String, Integer> expectedMap = new HashMap<String, Integer>() {{
|
||||
put("Saajan", 35);
|
||||
put("Liam", 100);
|
||||
put("Kai", 92); // max of 92, 76, and null
|
||||
put("Eric", 42);
|
||||
put("Kevin", 77);
|
||||
put("Jerry", null);
|
||||
}};
|
||||
|
||||
assertThrows(NullPointerException.class, () -> Stream.of(playerMap1, playerMap2, playerMap3, playerMap4, playerMap5)
|
||||
.flatMap(map -> map.entrySet()
|
||||
.stream())
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, Integer::max)));
|
||||
|
||||
Map<String, Integer> mergedMap = new HashMap<>();
|
||||
Stream.of(playerMap1, playerMap2, playerMap3, playerMap4, playerMap5)
|
||||
.flatMap(map -> map.entrySet()
|
||||
.stream())
|
||||
.forEach(entry -> {
|
||||
String k = entry.getKey();
|
||||
Integer v = entry.getValue();
|
||||
if (mergedMap.containsKey(k)) {
|
||||
mergedMap.put(k, maxInteger(mergedMap.get(k), v));
|
||||
} else {
|
||||
mergedMap.put(k, v);
|
||||
}
|
||||
});
|
||||
assertEquals(expectedMap, mergedMap);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMapsWithDuplicateKeysAndNullValues_whenUsingReduce_thenMultipleMapsMergedIntoOneMap() {
|
||||
|
||||
Map<String, Integer> expectedMap = new HashMap<String, Integer>() {{
|
||||
put("Saajan", 35);
|
||||
put("Liam", 100);
|
||||
put("Kai", 92); // max of 92, 76, and null
|
||||
put("Eric", 42);
|
||||
put("Kevin", 77);
|
||||
put("Jerry", null);
|
||||
}};
|
||||
Map<String, Integer> mergedMap = Stream.of(playerMap1, playerMap2, playerMap3, playerMap4, playerMap5)
|
||||
.flatMap(x -> x.entrySet()
|
||||
.stream())
|
||||
.collect(groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, reducing(null, this::maxInteger))));
|
||||
assertEquals(expectedMap, mergedMap);
|
||||
}
|
||||
}
|
|
@ -6,3 +6,5 @@
|
|||
- [Get First n Characters in a String in Java](https://www.baeldung.com/get-first-n-characters-in-a-string-in-java)
|
||||
- [Remove Only Trailing Spaces or Whitespace From a String in Java](https://www.baeldung.com/java-string-remove-only-trailing-whitespace)
|
||||
- [Get the Initials of a Name in Java](https://www.baeldung.com/java-shorten-name-initials)
|
||||
- [Normalizing the EOL Character in Java](https://www.baeldung.com/java-normalize-end-of-line-character)
|
||||
- [Converting UTF-8 to ISO-8859-1 in Java](https://www.baeldung.com/java-utf-8-iso-8859-1-conversion)
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
## Dozer
|
||||
|
||||
This module contains articles about Dozer
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [A Guide to Mapping With Dozer](https://www.baeldung.com/dozer)
|
|
@ -1,32 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>dozer</artifactId>
|
||||
<name>dozer</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sf.dozer</groupId>
|
||||
<artifactId>dozer</artifactId>
|
||||
<version>${dozer.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<dozer.version>5.5.1</dozer.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1168,7 +1168,7 @@
|
|||
<maven-war-plugin.version>3.2.2</maven-war-plugin.version>
|
||||
<jib-maven-plugin.version>0.9.11</jib-maven-plugin.version>
|
||||
<frontend-maven-plugin.version>1.7.6</frontend-maven-plugin.version>
|
||||
<jacoco-maven-plugin.version>0.8.2</jacoco-maven-plugin.version>
|
||||
<jacoco-maven-plugin.version>0.8.11</jacoco-maven-plugin.version>
|
||||
<lifecycle-mapping.version>1.0.0</lifecycle-mapping.version>
|
||||
<scala-maven-plugin.version>3.4.2</scala-maven-plugin.version>
|
||||
<sonar-maven-plugin.version>3.5.0.1254</sonar-maven-plugin.version>
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
<hazelcast-hibernate52.version>1.2</hazelcast-hibernate52.version>
|
||||
<hibernate.version>5.2.8.Final</hibernate.version>
|
||||
<hikaricp.version>5.1.0</hikaricp.version>
|
||||
<jacoco-maven-plugin.version>0.7.9</jacoco-maven-plugin.version>
|
||||
<jacoco-maven-plugin.version>0.8.11</jacoco-maven-plugin.version>
|
||||
<java.version>1.8</java.version>
|
||||
<javassist.version>3.21.0-GA</javassist.version>
|
||||
<jcache.version>1.0.0</jcache.version>
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
<hazelcast-hibernate52.version>1.2</hazelcast-hibernate52.version>
|
||||
<hibernate.version>5.2.8.Final</hibernate.version>
|
||||
<hikaricp.version>5.1.0</hikaricp.version>
|
||||
<jacoco-maven-plugin.version>0.7.9</jacoco-maven-plugin.version>
|
||||
<jacoco-maven-plugin.version>0.8.11</jacoco-maven-plugin.version>
|
||||
<javassist.version>3.21.0-GA</javassist.version>
|
||||
<jcache.version>1.0.0</jcache.version>
|
||||
<jhipster.server.version>1.1.0</jhipster.server.version>
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
<hazelcast-hibernate52.version>1.2</hazelcast-hibernate52.version>
|
||||
<hibernate.version>5.2.8.Final</hibernate.version>
|
||||
<hikaricp.version>5.1.0</hikaricp.version>
|
||||
<jacoco-maven-plugin.version>0.7.9</jacoco-maven-plugin.version>
|
||||
<jacoco-maven-plugin.version>0.8.11</jacoco-maven-plugin.version>
|
||||
<javassist.version>3.21.0-GA</javassist.version>
|
||||
<jcache.version>1.0.0</jcache.version>
|
||||
<jhipster.server.version>1.1.0</jhipster.server.version>
|
||||
|
|
|
@ -893,7 +893,7 @@
|
|||
<gatling.version>2.2.3</gatling.version>
|
||||
<hibernate.version>5.2.8.Final</hibernate.version>
|
||||
<hikaricp.version>5.1.0</hikaricp.version>
|
||||
<jacoco-maven-plugin.version>0.7.9</jacoco-maven-plugin.version>
|
||||
<jacoco-maven-plugin.version>0.8.11</jacoco-maven-plugin.version>
|
||||
<javassist.version>3.21.0-GA</javassist.version>
|
||||
<jcache.version>1.0.0</jcache.version>
|
||||
<jhipster.server.version>1.1.0</jhipster.server.version>
|
||||
|
|
|
@ -8,3 +8,4 @@ This module contains articles about JSON Conversions
|
|||
- [Convert Byte Array to JSON and Vice Versa in Java](https://www.baeldung.com/java-json-byte-array-conversion)
|
||||
- [Preventing Gson from Expressing Integers as Floats](https://www.baeldung.com/java-gson-prevent-expressing-integers-as-floats)
|
||||
- [Simplified Array Operations on JsonNode Without Typecasting in Jackson](https://www.baeldung.com/java-jsonnode-persistence-simplified-array-operations)
|
||||
- [How to Convert Excel to JSON in Java](https://www.baeldung.com/java-excel-json-conversion)
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.includenullinjson;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Customer {
|
||||
@JsonProperty
|
||||
private final String name;
|
||||
@JsonProperty
|
||||
private final String address;
|
||||
@JsonProperty
|
||||
private final int age;
|
||||
|
||||
public Customer(String name, String address, int age) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer{" +
|
||||
"name='" + name + '\'' +
|
||||
", address='" + address + '\'' +
|
||||
", age=" + age +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.baeldung.includenullinjson;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class IncludeNullValuesInJsonUnitTest {
|
||||
String expectedJson = "{\"name\":\"John\",\"address\":null,\"age\":25}";
|
||||
Customer obj = new Customer("John", null, 25);
|
||||
|
||||
@Test
|
||||
public void givenObjectWithNullField_whenJacksonUsed_thenIncludesNullValue() throws JsonProcessingException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
|
||||
String json = mapper.writeValueAsString(obj);
|
||||
assertEquals(expectedJson, json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenObjectWithNullField_whenGsonUsed_thenIncludesNullValue() {
|
||||
Gson gson = new GsonBuilder().serializeNulls().create();
|
||||
String json = gson.toJson(obj);
|
||||
assertEquals(expectedJson, json);
|
||||
}
|
||||
}
|
|
@ -12,4 +12,5 @@ This module contains articles about libraries for data processing in Java.
|
|||
- [Introduction to Eclipse Collections](https://www.baeldung.com/eclipse-collections)
|
||||
- [Introduction to Caffeine](https://www.baeldung.com/java-caching-caffeine)
|
||||
- [Guide to Using ModelMapper](https://www.baeldung.com/java-modelmapper)
|
||||
- [A Guide to Mapping With Dozer](https://www.baeldung.com/dozer)
|
||||
- More articles: [[<-- prev]](/../libraries-data-2)
|
||||
|
|
|
@ -71,6 +71,17 @@
|
|||
<artifactId>modelmapper</artifactId>
|
||||
<version>${org.modelmapper.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sf.dozer</groupId>
|
||||
<artifactId>dozer</artifactId>
|
||||
<version>${dozer.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -96,6 +107,7 @@
|
|||
<caffeine.version>3.1.8</caffeine.version>
|
||||
<findbugs.version>3.0.2</findbugs.version>
|
||||
<org.modelmapper.version>3.2.0</org.modelmapper.version>
|
||||
<dozer.version>5.5.1</dozer.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -171,7 +171,7 @@ public class DozerIntegrationTest {
|
|||
configureMapper("dozer_custom_convertor.xml");
|
||||
|
||||
String dateTime = "2007-06-26T21:22:39Z";
|
||||
long timestamp = new Long("1182882159000");
|
||||
long timestamp = Long.parseLong("1182882159000");
|
||||
|
||||
Person3 person = new Person3("Rich", dateTime);
|
||||
Personne3 person0 = mapper.map(person, Personne3.class);
|
||||
|
@ -182,7 +182,7 @@ public class DozerIntegrationTest {
|
|||
|
||||
@Test
|
||||
public void givenSrcAndDestWithDifferentFieldTypes_whenAbleToCustomConvertBidirectionally_thenCorrect() {
|
||||
long timestamp = new Long("1182882159000");
|
||||
long timestamp = Long.parseLong("1182882159000");
|
||||
Personne3 person = new Personne3("Rich", timestamp);
|
||||
configureMapper("dozer_custom_convertor.xml");
|
||||
|
|
@ -38,7 +38,7 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<jacoco-maven-plugin.version>0.8.8</jacoco-maven-plugin.version>
|
||||
<jacoco-maven-plugin.version>0.8.11</jacoco-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -84,4 +84,7 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -30,6 +30,7 @@
|
|||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>${maven-resources-plugin.version}</version>
|
||||
<configuration>
|
||||
<resources combine.children="append">
|
||||
<resource>
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>${maven-resources-plugin.version}</version>
|
||||
<configuration>
|
||||
<resources combine.self="override">
|
||||
<resource>
|
||||
|
|
|
@ -101,6 +101,7 @@
|
|||
<plugin>
|
||||
<groupId>io.micronaut.build</groupId>
|
||||
<artifactId>micronaut-maven-plugin</artifactId>
|
||||
<version>${micronaut-build.version}</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
|
@ -154,6 +155,7 @@
|
|||
<packaging>jar</packaging>
|
||||
<compiler.plugin.version>3.12.1</compiler.plugin.version>
|
||||
<micronaut.runtime>netty</micronaut.runtime>
|
||||
<micronaut-build.version>3.5.4</micronaut-build.version>
|
||||
<shade.plugin.version>3.2.0</shade.plugin.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -52,6 +52,7 @@
|
|||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
|
|
@ -39,6 +39,7 @@
|
|||
<plugin>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-maven-plugin</artifactId>
|
||||
<version>${jetty-plugin.version}</version>
|
||||
<configuration>
|
||||
<webApp>
|
||||
<contextPath>/</contextPath>
|
||||
|
@ -50,6 +51,7 @@
|
|||
|
||||
<properties>
|
||||
<maven-war-plugin.version>3.3.2</maven-war-plugin.version>
|
||||
<jetty-plugin.version>11.0.20</jetty-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -29,7 +29,7 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
<version>2.7.0</version>
|
||||
<version>${spring-boot.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -38,7 +38,12 @@
|
|||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<spring-boot.version>2.7.0</spring-boot.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -81,6 +81,16 @@
|
|||
<artifactId>hypersistence-utils-hibernate-60</artifactId>
|
||||
<version>${hypersistance-utils-hibernate-60.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.liquibase</groupId>
|
||||
<artifactId>liquibase-core</artifactId>
|
||||
<version>${liquibase-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
@ -91,6 +101,8 @@
|
|||
<maven.deploy.skip>true</maven.deploy.skip>
|
||||
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
|
||||
<hypersistance-utils-hibernate-60.version>3.3.1</hypersistance-utils-hibernate-60.version>
|
||||
<lombok.version>1.18.30</lombok.version>
|
||||
<liquibase-core.version>4.24.0</liquibase-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -4,6 +4,7 @@ import com.baeldung.hibernate.oneToMany.model.Cart;
|
|||
import com.baeldung.hibernate.oneToMany.model.CartOIO;
|
||||
import com.baeldung.hibernate.oneToMany.model.Item;
|
||||
import com.baeldung.hibernate.oneToMany.model.ItemOIO;
|
||||
import com.baeldung.hibernate.subselect.RuntimeConfiguration;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
|
@ -38,6 +39,7 @@ public class HibernateAnnotationUtil {
|
|||
.addAnnotatedClass(CartOIO.class)
|
||||
.addAnnotatedClass(Item.class)
|
||||
.addAnnotatedClass(ItemOIO.class)
|
||||
.addAnnotatedClass(RuntimeConfiguration.class)
|
||||
.buildMetadata();
|
||||
|
||||
return metadata.buildSessionFactory();
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.hibernate.subselect;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import java.time.Instant;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.hibernate.annotations.Subselect;
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@Entity
|
||||
// language=sql
|
||||
@Subselect(value =
|
||||
"SELECT\n"
|
||||
+ " ss.id,\n"
|
||||
+ " ss.attr_key,\n"
|
||||
+ " ss.val,\n"
|
||||
+ " ss.created_at\n"
|
||||
+ " FROM system_settings AS ss\n"
|
||||
+ " INNER JOIN (\n"
|
||||
+ " SELECT\n"
|
||||
+ " ss2.attr_key as k2,\n"
|
||||
+ " MAX(ss2.created_at) as ca2\n"
|
||||
+ " FROM system_settings ss2\n"
|
||||
+ " GROUP BY ss2.attr_key\n"
|
||||
+ " ) AS t ON t.k2 = ss.attr_key AND t.ca2 = ss.created_at\n"
|
||||
+ " WHERE ss.type = 'SYSTEM' AND ss.active IS TRUE\n")
|
||||
public class RuntimeConfiguration {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@Column(name = "attr_key")
|
||||
private String key;
|
||||
|
||||
@Column(name = "val")
|
||||
private String value;
|
||||
|
||||
@Column(name = "created_at")
|
||||
private Instant createdAt;
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
<databaseChangeLog
|
||||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
|
||||
|
||||
<changeSet id="4" author="Mikhail Polivakha">
|
||||
<createTable tableName="system_settings">
|
||||
<column name="id" type="bigserial" autoIncrement="true">
|
||||
<constraints primaryKey="true" nullable="false"/>
|
||||
</column>
|
||||
<column name="attr_key" type="text">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
<column name="val" type="text">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
<column name="type" type="text">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
<column name="active" type="boolean">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
<column name="created_at" type="timestamptz">
|
||||
<constraints nullable="false"/>
|
||||
</column>
|
||||
</createTable>
|
||||
|
||||
<insert tableName="system_settings">
|
||||
<column name="id">1</column>
|
||||
<column name="attr_key">splitting.enabled</column>
|
||||
<column name="val">true</column>
|
||||
<column name="active">true</column>
|
||||
<column name="type">SYSTEM</column>
|
||||
<column name="created_at" valueComputed="NOW()"/>
|
||||
</insert>
|
||||
|
||||
<insert tableName="system_settings">
|
||||
<column name="id">2</column>
|
||||
<column name="attr_key">splitting.enabled</column>
|
||||
<column name="val">false</column>
|
||||
<column name="active">false</column>
|
||||
<column name="type">SYSTEM</column>
|
||||
<column name="created_at" valueComputed="NOW()"/>
|
||||
</insert>
|
||||
|
||||
<insert tableName="system_settings">
|
||||
<column name="id">3</column>
|
||||
<column name="attr_key">redelivery.enabled</column>
|
||||
<column name="val">false</column>
|
||||
<column name="active">true</column>
|
||||
<column name="type">ORDER</column>
|
||||
<column name="created_at" valueComputed="NOW()"/>
|
||||
</insert>
|
||||
</changeSet>
|
||||
</databaseChangeLog>
|
|
@ -0,0 +1,7 @@
|
|||
<databaseChangeLog
|
||||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
|
||||
|
||||
<include file="V1__init.xml" relativeToChangelogFile="true" />
|
||||
</databaseChangeLog>
|
|
@ -0,0 +1,65 @@
|
|||
package com.baeldung.hibernate.subselect;
|
||||
|
||||
import com.baeldung.hibernate.oneToMany.config.HibernateAnnotationUtil;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
import liquibase.Contexts;
|
||||
import liquibase.LabelExpression;
|
||||
import liquibase.Liquibase;
|
||||
import liquibase.database.DatabaseFactory;
|
||||
import liquibase.database.jvm.JdbcConnection;
|
||||
import liquibase.exception.LiquibaseException;
|
||||
import liquibase.resource.ClassLoaderResourceAccessor;
|
||||
import org.hibernate.Session;
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.CriteriaQuery;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SubselectIntegrationTest {
|
||||
|
||||
@BeforeAll
|
||||
static void setUp() {
|
||||
Session currentSession = HibernateAnnotationUtil.getSessionFactory().getCurrentSession();
|
||||
currentSession.beginTransaction();
|
||||
|
||||
currentSession.doWork(it -> {
|
||||
Liquibase liquibase;
|
||||
try {
|
||||
liquibase = new Liquibase(
|
||||
"migrations/master.xml",
|
||||
new ClassLoaderResourceAccessor(),
|
||||
DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(it))
|
||||
);
|
||||
liquibase.update(new Contexts(), new LabelExpression(),true);
|
||||
} catch (LiquibaseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
|
||||
currentSession.getTransaction().commit();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenEntityMarkedWithSubselect_whenSelectingRuntimeConfigByKey_thenSelectedSuccessfully() {
|
||||
String key = "splitting.enabled";
|
||||
Session entityManager = HibernateAnnotationUtil.getSessionFactory().getCurrentSession();
|
||||
|
||||
entityManager.beginTransaction();
|
||||
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
|
||||
CriteriaQuery<RuntimeConfiguration> query = criteriaBuilder.createQuery(RuntimeConfiguration.class);
|
||||
|
||||
Root<RuntimeConfiguration> root = query.from(RuntimeConfiguration.class);
|
||||
|
||||
RuntimeConfiguration configurationParameter = entityManager.createQuery(
|
||||
query.select(root).where(criteriaBuilder.equal(root.get("key"), key))
|
||||
).getSingleResult();
|
||||
|
||||
entityManager.getTransaction().commit();
|
||||
|
||||
Assertions.assertThat(configurationParameter.getValue()).isEqualTo("true");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.hibernate.listentity.entity;
|
||||
|
||||
import java.util.List;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Department {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToMany(mappedBy = "department", fetch = FetchType.EAGER)
|
||||
private List<Employee> employees;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<Employee> getEmployees() {
|
||||
return employees;
|
||||
}
|
||||
|
||||
public void setEmployees(List<Employee> employees) {
|
||||
this.employees = employees;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.hibernate.listentity.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@NamedQuery(name = "findAllEmployees", query = "SELECT e FROM Employee e")
|
||||
@NamedQuery(name = "findEmployeesByDepartment", query = "SELECT e FROM Employee e WHERE e.department = :department ORDER BY e.lastName ASC")
|
||||
public class Employee {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private String department;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getDepartment() {
|
||||
return department;
|
||||
}
|
||||
|
||||
public void setDepartment(String department) {
|
||||
this.department = department;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
package com.baeldung.hibernate.listentity.service;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.CriteriaQuery;
|
||||
import jakarta.persistence.criteria.Path;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.hibernate.listentity.entity.Department;
|
||||
import com.baeldung.hibernate.listentity.entity.Employee;
|
||||
|
||||
@Service
|
||||
public class EmployeeService {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
public List<Employee> getAllEmployeesUsingJPQL() {
|
||||
String jpqlQuery = "SELECT e FROM Employee e";
|
||||
Query query = entityManager.createQuery(jpqlQuery, Employee.class);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
public List<Employee> getAllEmployeesByDepartmentUsingJPQL() {
|
||||
String jpqlQuery = "SELECT e FROM Employee e WHERE e.department = 'Engineering' ORDER BY e.lastName ASC";
|
||||
Query query = entityManager.createQuery(jpqlQuery, Employee.class);
|
||||
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
public List<Employee> getAllEmployeesUsingNamedQuery() {
|
||||
Query query = entityManager.createNamedQuery("findAllEmployees", Employee.class);
|
||||
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
public List<Employee> getAllEmployeesByDepartmentUsingNamedQuery(String department) {
|
||||
Query query = entityManager.createNamedQuery("findEmployeesByDepartment", Employee.class);
|
||||
query.setParameter("department", department);
|
||||
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
public List<Employee> getAllEmployeesUsingCriteriaAPI() {
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);
|
||||
Root<Employee> employeeRoot = criteriaQuery.from(Employee.class);
|
||||
|
||||
criteriaQuery.select(employeeRoot);
|
||||
Query query = entityManager.createQuery(criteriaQuery);
|
||||
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
public List<Employee> getAllEmployeesByDepartmentUsingCriteriaAPI(String department) {
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Employee> criteriaQuery = criteriaBuilder.createQuery(Employee.class);
|
||||
Root<Employee> employeeRoot = criteriaQuery.from(Employee.class);
|
||||
Predicate departmentPredicate = criteriaBuilder.equal(employeeRoot.get("department"), department);
|
||||
Path<Object> sortByPath = employeeRoot.get("lastName");
|
||||
criteriaQuery.orderBy(criteriaBuilder.asc(sortByPath));
|
||||
criteriaQuery.select(employeeRoot)
|
||||
.where(departmentPredicate);
|
||||
Query query = entityManager.createQuery(criteriaQuery);
|
||||
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
public List<Employee> getAllEmployeesByDepartmentUsingOneToManyAnnotations(String departmentName) {
|
||||
try {
|
||||
// Retrieve the department by its name
|
||||
Department department = entityManager.createQuery("SELECT d FROM Department d WHERE d.name = :name", Department.class)
|
||||
.setParameter("name", departmentName)
|
||||
.getSingleResult();
|
||||
|
||||
// Return the list of employees associated with the department
|
||||
return department.getEmployees();
|
||||
} catch (NoResultException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,2 +1,3 @@
|
|||
### Relevant Articles:
|
||||
- [Introduction to Liquibase Rollback](http://www.baeldung.com/liquibase-rollback)
|
||||
- [Creating PostgreSQL Schema Before Liquibase Execution](https://www.baeldung.com/java-postgresql-create-schema-before-liquibase)
|
||||
|
|
|
@ -124,6 +124,7 @@
|
|||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring.boot.dependencies}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
|
|
@ -55,6 +55,7 @@
|
|||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring.boot.dependencies}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -683,7 +683,6 @@
|
|||
<module>di-modules</module>
|
||||
<module>disruptor</module>
|
||||
<module>docker-modules</module>
|
||||
<module>dozer</module>
|
||||
<module>drools</module>
|
||||
<module>dubbo</module>
|
||||
<module>feign</module>
|
||||
|
@ -931,7 +930,6 @@
|
|||
<module>di-modules</module>
|
||||
<module>disruptor</module>
|
||||
<module>docker-modules</module>
|
||||
<module>dozer</module>
|
||||
<module>drools</module>
|
||||
<module>dubbo</module>
|
||||
<module>feign</module>
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>${maven-jar-plugin.version}</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
|
|
|
@ -51,6 +51,7 @@
|
|||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>${maven-failsafe-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
|
|
|
@ -77,6 +77,7 @@
|
|||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>${maven-jar-plugin.version}</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
|
|
|
@ -83,6 +83,7 @@
|
|||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>${maven-jar-plugin.version}</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
|
|
|
@ -27,6 +27,8 @@
|
|||
<module>spring-boot-basic-customization</module>
|
||||
<module>spring-boot-basic-customization-2</module>
|
||||
<module>spring-boot-bootstrap</module>
|
||||
<module>spring-boot-caching</module> <!-- failing after upgrading to spring boot 3.2.x -->
|
||||
<module>spring-boot-caching-2</module> <!-- failing after upgrading to spring boot 3.2.x -->
|
||||
<module>spring-boot-client</module>
|
||||
<module>spring-boot-config-jpa-error</module>
|
||||
<module>spring-boot-ctx-fluent</module>
|
||||
|
@ -54,7 +56,7 @@
|
|||
<module>spring-boot-process-automation</module>
|
||||
<module>spring-boot-logging-logback</module>
|
||||
<module>spring-boot-logging-log4j2</module>
|
||||
<!-- <module>spring-boot-mvc</module>--> <!-- failing after upgrading to spring boot 3.2.x -->
|
||||
<module>spring-boot-mvc</module>
|
||||
<module>spring-boot-mvc-2</module>
|
||||
<module>spring-boot-mvc-3</module>
|
||||
<module>spring-boot-mvc-4</module>
|
||||
|
@ -86,8 +88,6 @@
|
|||
<module>spring-boot-data-2</module>
|
||||
<module>spring-boot-validation</module>
|
||||
<!-- <module>spring-boot-data-3</module>--> <!-- failing after upgrading to spring boot 3.2.x -->
|
||||
<!-- <module>spring-caching</module>--> <!-- failing after upgrading to spring boot 3.2.x -->
|
||||
<!-- <module>spring-caching-2</module>--> <!-- failing after upgrading to spring boot 3.2.x -->
|
||||
<!-- <module>spring-boot-redis</module>--> <!-- failing after upgrading to spring boot 3.2.x -->
|
||||
<module>spring-boot-cassandre</module>
|
||||
<module>spring-boot-react</module>
|
||||
|
@ -101,7 +101,7 @@
|
|||
<module>spring-boot-resilience4j</module>
|
||||
<module>spring-boot-properties</module>
|
||||
<module>spring-boot-properties-2</module>
|
||||
<!-- <module>spring-boot-properties-3</module>--> <!-- failing after upgrading to spring boot 3.2.x -->
|
||||
<module>spring-boot-properties-3</module> <!-- failing after upgrading to spring boot 3.2.x -->
|
||||
<module>spring-boot-properties-4</module>
|
||||
<module>spring-boot-properties-migrator-demo</module>
|
||||
<module>spring-boot-aws</module> <!-- This project is Spring Boot 2 and cannot be updated -->
|
||||
|
|
|
@ -3,4 +3,5 @@
|
|||
- [Setting Time-To-Live Value for Caching](https://www.baeldung.com/spring-setting-ttl-value-cache)
|
||||
- [Get All Cached Keys with Caffeine Cache in Spring Boot](https://www.baeldung.com/spring-boot-caffeine-spring-get-all-keys)
|
||||
- [Implement Two-Level Cache With Spring](https://www.baeldung.com/spring-two-level-cache)
|
||||
- More articles: [[<-- prev]](/spring-boot-modules/spring-boot-caching)
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue