Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-7487-resolve-multiple-completable-futures
This commit is contained in:
commit
9ca5b38ad2
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.equilibriumindex;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class EquilibriumIndexFinder {
|
||||
|
||||
List<Integer> findEquilibriumIndexes(int[] array) {
|
||||
int[] partialSums = new int[array.length + 1];
|
||||
partialSums[0] = 0;
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
partialSums[i+1] = partialSums[i] + array[i];
|
||||
}
|
||||
|
||||
List<Integer> equilibriumIndexes = new ArrayList<Integer>();
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (partialSums[i] == (partialSums[array.length] - (partialSums[i+1]))) {
|
||||
equilibriumIndexes.add(i);
|
||||
}
|
||||
}
|
||||
return equilibriumIndexes;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.equilibriumindex;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class EquilibriumIndexFinderUnitTest {
|
||||
|
||||
@Test
|
||||
void givenArrayHasEquilibriumIndexes_whenFindEquilibriumIndexes_thenListAllEquilibriumIndexes() {
|
||||
int[] array = {1, -3, 0, 4, -5, 4, 0, 1, -2, -1};
|
||||
assertThat(new EquilibriumIndexFinder().findEquilibriumIndexes(array)).containsExactly(1, 4, 9);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithoutEquilibriumIndexes_whenFindEquilibriumIndexes_thenEmptyList() {
|
||||
int[] array = {1, 2, 3};
|
||||
assertThat(new EquilibriumIndexFinder().findEquilibriumIndexes(array)).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithOneElement_whenFindEquilibriumIndexes_thenListFirstIndex() {
|
||||
int[] array = {5};
|
||||
assertThat(new EquilibriumIndexFinder().findEquilibriumIndexes(array)).containsExactly(0);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package com.baeldung.datetounixtimestamp;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DateToUnixTimeStampUnitTest {
|
||||
|
||||
@Test
|
||||
void givenDate_whenUsingInstantClass_thenConvertToUnixTimeStamp() {
|
||||
Instant givenDate = Instant.parse("2020-09-08T12:16:40Z");
|
||||
|
||||
assertEquals(1599567400L, givenDate.getEpochSecond());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDate_whenUsingLocalDateTimeClass_thenConvertToUnixTimeStamp() {
|
||||
LocalDateTime givenDate = LocalDateTime.of(2023, 10, 19, 22, 45);
|
||||
|
||||
assertEquals(1697755500L, givenDate.toEpochSecond(ZoneOffset.UTC));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDate_whenUsingDateClass_thenConvertToUnixTimeStamp() throws ParseException {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
Date givenDate = dateFormat.parse("2023-10-15 22:00:00");
|
||||
|
||||
assertEquals(1697407200L, givenDate.getTime() / 1000);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDate_whenUsingCalendarClass_thenConvertToUnixTimeStamp() {
|
||||
Calendar calendar = new GregorianCalendar(2023, Calendar.OCTOBER, 17);
|
||||
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
assertEquals(1697500800L, calendar.getTimeInMillis() / 1000);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDate_whenUsingJodaTimeInstantClass_thenConvertToUnixTimeStamp() {
|
||||
org.joda.time.Instant givenDate = org.joda.time.Instant.parse("2020-09-08T12:16:40Z");
|
||||
|
||||
assertEquals(1599567400L, givenDate.getMillis() / 1000);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDate_whenUsingJodaTimeDateTimeClass_thenConvertToUnixTimeStamp() {
|
||||
DateTime givenDate = new DateTime("2020-09-09T12:16:40Z");
|
||||
|
||||
assertEquals(1599653800L, givenDate.getMillis() / 1000);
|
||||
}
|
||||
|
||||
}
|
|
@ -51,9 +51,10 @@ public class ReadInputCharByCharUnitTest {
|
|||
System.setIn(inputStream);
|
||||
|
||||
try (Scanner scanner = new Scanner(System.in)) {
|
||||
char[] result = scanner.next().toCharArray();
|
||||
|
||||
assertArrayEquals("TestInput".toCharArray(), result);
|
||||
if (scanner.hasNext()) {
|
||||
char[] result = scanner.next().toCharArray();
|
||||
assertArrayEquals("TestInput".toCharArray(), result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.objectmutability;
|
||||
|
||||
public final class ImmutablePerson {
|
||||
|
||||
private final String name;
|
||||
private final int age;
|
||||
|
||||
public ImmutablePerson(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.objectmutability;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
|
||||
public class ImmutableObjectExamplesUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenImmutableString_whenConcatString_thenNotSameAndCorrectValues() {
|
||||
String originalString = "Hello";
|
||||
String modifiedString = originalString.concat(" World");
|
||||
|
||||
assertNotSame(originalString, modifiedString);
|
||||
|
||||
assertEquals("Hello", originalString);
|
||||
assertEquals("Hello World", modifiedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenImmutableInteger_whenAddInteger_thenNotSameAndCorrectValue() {
|
||||
Integer immutableInt = 42;
|
||||
Integer modifiedInt = immutableInt + 8;
|
||||
|
||||
assertNotSame(immutableInt, modifiedInt);
|
||||
|
||||
assertEquals(42, (int) immutableInt);
|
||||
assertEquals(50, (int) modifiedInt);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.objectmutability;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ImmutablePersonUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenImmutablePerson_whenAccessFields_thenCorrectValues() {
|
||||
ImmutablePerson person = new ImmutablePerson("John", 30);
|
||||
|
||||
assertEquals("John", person.getName());
|
||||
assertEquals(30, person.getAge());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.objectmutability;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class MutableObjectExamplesUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenMutableString_whenAppendElement_thenCorrectValue() {
|
||||
StringBuilder mutableString = new StringBuilder("Hello");
|
||||
mutableString.append(" World");
|
||||
|
||||
assertEquals("Hello World", mutableString.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMutableList_whenAddElement_thenCorrectSize() {
|
||||
List<String> mutableList = new ArrayList<>();
|
||||
mutableList.add("Java");
|
||||
|
||||
assertEquals(1, mutableList.size());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package com.baeldung.infixpostfix;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class InfixToPostFixExpressionConversion {
|
||||
|
||||
private int getPrecedenceScore(char ch) {
|
||||
switch (ch) {
|
||||
case '^':
|
||||
return 3;
|
||||
|
||||
case '*':
|
||||
case '/':
|
||||
return 2;
|
||||
|
||||
case '+':
|
||||
case '-':
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private boolean isOperand(char ch) {
|
||||
return (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9');
|
||||
}
|
||||
|
||||
private char associativity(char ch) {
|
||||
if (ch == '^')
|
||||
return 'R';
|
||||
return 'L';
|
||||
}
|
||||
|
||||
public String infixToPostfix(String infix) {
|
||||
StringBuilder result = new StringBuilder();
|
||||
Stack<Character> stack = new Stack<>();
|
||||
|
||||
for (int i = 0; i < infix.length(); i++) {
|
||||
char ch = infix.charAt(i);
|
||||
|
||||
if (isOperand(ch)) {
|
||||
result.append(ch);
|
||||
} else if (ch == '(') {
|
||||
stack.push(ch);
|
||||
} else if (ch == ')') {
|
||||
while (!stack.isEmpty() && stack.peek() != '(') {
|
||||
result.append(stack.pop());
|
||||
}
|
||||
stack.pop();
|
||||
} else {
|
||||
while (!stack.isEmpty() && (operatorPrecedenceCondition(infix, i, stack))) {
|
||||
result.append(stack.pop());
|
||||
}
|
||||
stack.push(ch);
|
||||
}
|
||||
}
|
||||
|
||||
while (!stack.isEmpty()) {
|
||||
result.append(stack.pop());
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private boolean operatorPrecedenceCondition(String infix, int i, Stack<Character> stack) {
|
||||
return getPrecedenceScore(infix.charAt(i)) < getPrecedenceScore(stack.peek()) || getPrecedenceScore(infix.charAt(i)) == getPrecedenceScore(stack.peek()) && associativity(infix.charAt(i)) == 'L';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.infixpostfix;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
||||
public class InfixToPostfixExpressionUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenSimpleOp_whenNoParenthesis_thenProduceValidPostfix() {
|
||||
String infix = "a+b*c-d";
|
||||
String postfix = "abc*+d-";
|
||||
|
||||
InfixToPostFixExpressionConversion obj = new InfixToPostFixExpressionConversion();
|
||||
|
||||
Assertions.assertEquals(postfix, obj.infixToPostfix(infix));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSimpleOp_whenWithParenthesis_thenProduceValidPostfix() {
|
||||
String infix = "(a+b)*(c-d)";
|
||||
String postfix = "ab+cd-*";
|
||||
|
||||
InfixToPostFixExpressionConversion obj = new InfixToPostFixExpressionConversion();
|
||||
|
||||
Assertions.assertEquals(postfix, obj.infixToPostfix(infix));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenComplexOp_whenInputIsInfix_thenProduceValidPostfix() {
|
||||
String infix = "a^b*(c^d-e)^(f+g*h)-i";
|
||||
String postfix = "ab^cd^e-fgh*+^*i-";
|
||||
|
||||
InfixToPostFixExpressionConversion obj = new InfixToPostFixExpressionConversion();
|
||||
|
||||
Assertions.assertEquals(postfix, obj.infixToPostfix(infix));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.customurlconnection;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
public class CustomURLConnection extends URLConnection {
|
||||
private final String simulatedData = "This is the simulated data from the resource.";
|
||||
private URL url;
|
||||
private boolean connected = false;
|
||||
private String headerValue = "SimulatedHeaderValue";
|
||||
|
||||
protected CustomURLConnection(URL url) {
|
||||
super(url);
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() throws IOException {
|
||||
connected = true;
|
||||
System.out.println("Connection established to: " + url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
if (!connected) {
|
||||
connect();
|
||||
}
|
||||
return new ByteArrayInputStream(simulatedData.getBytes());
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream getOutputStream() throws IOException {
|
||||
ByteArrayOutputStream simulatedOutput = new ByteArrayOutputStream();
|
||||
return simulatedOutput;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getContentLength() {
|
||||
return simulatedData.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHeaderField(String name) {
|
||||
if ("SimulatedHeader".equalsIgnoreCase(name)) { // Example header name
|
||||
return headerValue;
|
||||
} else {
|
||||
return null; // Header not found
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.customurlconnection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLStreamHandler;
|
||||
|
||||
public class CustomURLStreamHandler extends URLStreamHandler {
|
||||
|
||||
@Override
|
||||
protected URLConnection openConnection(URL u) throws IOException {
|
||||
// Create and return an instance of CustomURLConnection
|
||||
return new CustomURLConnection(u);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.customurlconnection;
|
||||
|
||||
import java.net.URLStreamHandler;
|
||||
import java.net.URLStreamHandlerFactory;
|
||||
|
||||
public class CustomURLStreamHandlerFactory implements URLStreamHandlerFactory {
|
||||
|
||||
@Override
|
||||
public URLStreamHandler createURLStreamHandler(String protocol) {
|
||||
// Check if the requested protocol is our custom protocol
|
||||
if ("myprotocol".equals(protocol)) {
|
||||
return new CustomURLStreamHandler();
|
||||
}
|
||||
return null; // Let the default handler deal with other protocols
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.customurlconnection;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
URL.setURLStreamHandlerFactory(new CustomURLStreamHandlerFactory());
|
||||
|
||||
try {
|
||||
URL url = new URL("myprotocol://example.com/resource");
|
||||
|
||||
CustomURLConnection customConnection = (CustomURLConnection) url.openConnection();
|
||||
customConnection.connect();
|
||||
|
||||
InputStream inputStream = customConnection.getInputStream();
|
||||
|
||||
String content = new Scanner(inputStream).useDelimiter("\\A").next();
|
||||
System.out.println(content);
|
||||
|
||||
int contentLength = customConnection.getContentLength();
|
||||
System.out.println("Content Length: " + contentLength);
|
||||
|
||||
String headerValue = customConnection.getHeaderField("SimulatedHeader");
|
||||
System.out.println("Header Value: " + headerValue);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.urlencoder;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SpaceURLEncoderUnitTest {
|
||||
|
||||
@Test
|
||||
void givenSpaceInString_whenUsingDefaultEncoding_thenReturnPlusSign() {
|
||||
String originalString = "Welcome to the Baeldung Website!";
|
||||
String encodedString = URLEncoder.encode(originalString);
|
||||
assertEquals("Welcome+to+the+Baeldung+Website%21", encodedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSpaceInString_whenUsingUTF8Encoding_thenReturnPlusSign() throws UnsupportedEncodingException {
|
||||
String originalString = "Welcome to the Baeldung Website!";
|
||||
String encodedString = URLEncoder.encode(originalString, StandardCharsets.UTF_8.toString());
|
||||
assertEquals("Welcome+to+the+Baeldung+Website%21", encodedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSpaceInString_whenUsingDefaultEncodingAndReplace_thenReturnPct20() throws UnsupportedEncodingException {
|
||||
String originalString = "Welcome to the Baeldung Website!";
|
||||
String encodedString = URLEncoder.encode(originalString)
|
||||
.replace("+", "%20");
|
||||
assertEquals("Welcome%20to%20the%20Baeldung%20Website%21", encodedString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSpaceInString_whenUsingDefaultEncodingAndReplaceAll_thenReturnPct20() throws UnsupportedEncodingException {
|
||||
String originalString = "Welcome to the Baeldung Website!";
|
||||
String encodedString = URLEncoder.encode(originalString)
|
||||
.replaceAll("\\+", "%20");
|
||||
assertEquals("Welcome%20to%20the%20Baeldung%20Website%21", encodedString);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.securerandompositivelong;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class SecureRandomPositiveLongUnitTest {
|
||||
|
||||
@Test
|
||||
void whenGenerateRandomPositiveLong_thenGetPositiveValue() {
|
||||
SecureRandom secureRandom = new SecureRandom();
|
||||
long randomPositiveLong = Math.abs(secureRandom.nextLong());
|
||||
|
||||
assertThat(randomPositiveLong).isNotNegative();
|
||||
}
|
||||
}
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.core.version}</version>
|
||||
</dependency>
|
||||
|
@ -53,7 +53,7 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<hibernate.core.version>5.4.0.Final</hibernate.core.version>
|
||||
<hibernate.core.version>6.4.2.Final</hibernate.core.version>
|
||||
<json-path.version>5.3.2</json-path.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ import java.util.Map;
|
|||
import java.util.Optional;
|
||||
|
||||
public class HandleOptionalTypeExample {
|
||||
static Map<String, User> usersByName = new HashMap();
|
||||
static Map<String, User> usersByName = new HashMap<>();
|
||||
static {
|
||||
User user1 = new User();
|
||||
user1.setUserId(1l);
|
||||
|
|
|
@ -2,9 +2,9 @@ package com.baeldung.optionalreturntype;
|
|||
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.Persistence;
|
||||
|
||||
public class PersistOptionalTypeExample {
|
||||
static String persistenceUnit = "com.baeldung.optionalreturntype";
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package com.baeldung.optionalreturntype;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.Persistence;
|
||||
|
||||
public class PersistOptionalTypeExample2 {
|
||||
static String persistenceUnit = "com.baeldung.optionalreturntype";
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package com.baeldung.optionalreturntype;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.Persistence;
|
||||
|
||||
public class PersistUserExample {
|
||||
static String persistenceUnit = "com.baeldung.optionalreturntype";
|
||||
|
|
|
@ -2,8 +2,8 @@ package com.baeldung.optionalreturntype;
|
|||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class User implements Serializable {
|
||||
|
|
|
@ -3,9 +3,9 @@ package com.baeldung.optionalreturntype;
|
|||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class UserOptional implements Serializable {
|
||||
|
|
|
@ -3,8 +3,8 @@ package com.baeldung.optionalreturntype;
|
|||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class UserOptionalField implements Serializable {
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.stream.range;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class StreamRangeUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenRangeStreamUsingLimitSkip_thenPrintsRange() {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
List<Integer> expectedRange = Arrays.asList(3, 4, 5, 6, 7);
|
||||
|
||||
List<Integer> range = numbers.stream()
|
||||
.skip(2)
|
||||
.limit(5)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
assertEquals(expectedRange, range);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRangeStreamUsingCollectingAndThen_thenPrintsRange() {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
||||
List<Integer> expectedRange = Arrays.asList(3, 4, 5, 6, 7);
|
||||
|
||||
List<Integer> range = numbers.stream()
|
||||
.filter(n -> n >= 3 && n <= 7)
|
||||
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
|
||||
|
||||
assertEquals(expectedRange, range);
|
||||
}
|
||||
}
|
|
@ -60,6 +60,7 @@
|
|||
<module>core-java-methods</module>
|
||||
<module>core-java-networking-3</module>
|
||||
<module>core-java-os</module>
|
||||
<module>core-java-os-2</module>
|
||||
<module>core-java-perf-2</module>
|
||||
<module>core-java-streams-4</module>
|
||||
<module>core-java-streams-5</module>
|
||||
|
|
|
@ -55,14 +55,12 @@
|
|||
<artifactId>httpclient</artifactId>
|
||||
<version>${httpclient.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -14,14 +14,12 @@
|
|||
<artifactId>avaje-inject</artifactId>
|
||||
<version>${avaje.inject.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.avaje</groupId>
|
||||
<artifactId>avaje-inject-test</artifactId>
|
||||
<version>${avaje.inject.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Annotation processors -->
|
||||
<dependency>
|
||||
<groupId>io.avaje</groupId>
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
<?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/xsd/maven-4.0.0.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>k8s-operator</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>k8s-operator</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -11,30 +15,12 @@
|
|||
<relativePath>./../../parent-boot-3</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>k8s-operator</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>k8s-operator</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<josdk.version>4.6.0</josdk.version>
|
||||
<fabric8-client.version>6.9.2</fabric8-client.version>
|
||||
<bouncycastle.version>1.77</bouncycastle.version>
|
||||
<slf4j.version>2.0.9</slf4j.version>
|
||||
<operator-framework-spring-boot.version>5.4.0</operator-framework-spring-boot.version>
|
||||
</properties>
|
||||
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.javaoperatorsdk</groupId>
|
||||
<artifactId>operator-framework-spring-boot-starter</artifactId>
|
||||
<version>${operator-framework-spring-boot.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.javaoperatorsdk</groupId>
|
||||
<artifactId>operator-framework-spring-boot-starter-test</artifactId>
|
||||
|
@ -47,49 +33,42 @@
|
|||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.fabric8</groupId>
|
||||
<artifactId>crd-generator-apt</artifactId>
|
||||
<version>${fabric8-client.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcprov-jdk18on</artifactId>
|
||||
<version>${bouncycastle.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.bouncycastle</groupId>
|
||||
<artifactId>bcpkix-jdk18on</artifactId>
|
||||
<version>${bouncycastle.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
@ -108,4 +87,14 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<josdk.version>4.6.0</josdk.version>
|
||||
<fabric8-client.version>6.9.2</fabric8-client.version>
|
||||
<bouncycastle.version>1.77</bouncycastle.version>
|
||||
<slf4j.version>2.0.9</slf4j.version>
|
||||
<operator-framework-spring-boot.version>5.4.0</operator-framework-spring-boot.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -18,7 +18,7 @@
|
|||
<module>k8s-admission-controller</module>
|
||||
<module>kubernetes-spring</module>
|
||||
<module>k8s-java-heap-dump</module>
|
||||
<module>k8s-operator</module>
|
||||
<module>k8s-operator</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -167,7 +167,6 @@
|
|||
</resources>
|
||||
</build>
|
||||
|
||||
|
||||
<properties>
|
||||
<jcabi-aspects.version>0.22.6</jcabi-aspects.version>
|
||||
<aspectjrt.version>1.9.20.1</aspectjrt.version>
|
||||
|
|
|
@ -41,6 +41,7 @@
|
|||
<version>${asm.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
<?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/xsd/maven-4.0.0.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>logging-techniques</artifactId>
|
||||
|
||||
<parent>
|
||||
<artifactId>logging-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>logging-techniques</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
|
|
@ -55,6 +55,7 @@
|
|||
<version>${validation-api.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
@ -82,6 +83,7 @@
|
|||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<openapi.version>7.1.0</openapi.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
|
|
|
@ -14,15 +14,21 @@
|
|||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<surefire-version>2.22.2</surefire-version>
|
||||
</properties>
|
||||
|
||||
<modules>
|
||||
<module>dummy-surefire-junit47</module>
|
||||
<module>core-java-exclusions</module>
|
||||
</modules>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
|
@ -57,14 +63,8 @@
|
|||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<properties>
|
||||
<surefire-version>2.22.2</surefire-version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -5,11 +5,13 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>patient-data</artifactId>
|
||||
<name>patient-data</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>maven-reactor</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -17,4 +19,5 @@
|
|||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -5,9 +5,11 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>patient-domain</artifactId>
|
||||
<name>patient-domain</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>maven-reactor</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
</project>
|
|
@ -8,11 +8,13 @@
|
|||
<name>maven-reactor</name>
|
||||
<packaging>pom</packaging>
|
||||
<description>Sample multi-module project to explain maven reactor</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>maven-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>patient-web</module>
|
||||
<module>patient-data</module>
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
<version>1.0-SNAPSHOT</version>
|
||||
<name>resume-from</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>business</module>
|
||||
<module>lib</module>
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?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/xsd/maven-4.0.0.xsd">
|
||||
<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/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>micronaut-reactive</artifactId>
|
||||
|
@ -13,17 +14,6 @@
|
|||
<version>4.1.2</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
<properties>
|
||||
<packaging>jar</packaging>
|
||||
<jdk.version>17</jdk.version>
|
||||
<release.version>17</release.version>
|
||||
<micronaut.runtime>netty</micronaut.runtime>
|
||||
<micronaut.aot.enabled>false</micronaut.aot.enabled>
|
||||
<micronaut.aot.packageName>com.baeldung.aot.generated</micronaut.aot.packageName>
|
||||
<micronaut.test.resources.enabled>true</micronaut.test.resources.enabled>
|
||||
<exec.mainClass>com.baeldung.micronautreactive.Application</exec.mainClass>
|
||||
<micronaut-test-resources-testcontainers.version>2.1.0</micronaut-test-resources-testcontainers.version>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
|
@ -111,6 +101,7 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
@ -197,4 +188,16 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<packaging>jar</packaging>
|
||||
<jdk.version>17</jdk.version>
|
||||
<release.version>17</release.version>
|
||||
<micronaut.runtime>netty</micronaut.runtime>
|
||||
<micronaut.aot.enabled>false</micronaut.aot.enabled>
|
||||
<micronaut.aot.packageName>com.baeldung.aot.generated</micronaut.aot.packageName>
|
||||
<micronaut.test.resources.enabled>true</micronaut.test.resources.enabled>
|
||||
<exec.mainClass>com.baeldung.micronautreactive.Application</exec.mainClass>
|
||||
<micronaut-test-resources-testcontainers.version>2.1.0</micronaut-test-resources-testcontainers.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -6,6 +6,18 @@
|
|||
<artifactId>core-java-persistence-3</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-persistence-3</name>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>17</source>
|
||||
<target>17</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package jdbcpagination;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class PaginationLogic {
|
||||
|
||||
public static ResultSet readPageWithLimitAndOffset(Connection connection, int offset, int pageSize) throws SQLException {
|
||||
String sql = """
|
||||
SELECT * FROM employees
|
||||
LIMIT ? OFFSET ?
|
||||
""";
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
||||
preparedStatement.setInt(1, pageSize);
|
||||
preparedStatement.setInt(2, offset);
|
||||
|
||||
return preparedStatement.executeQuery();
|
||||
}
|
||||
|
||||
public static ResultSet readPageWithSortedKeys(Connection connection, int lastFetchedId, int pageSize) throws SQLException {
|
||||
String sql = """
|
||||
SELECT * FROM employees
|
||||
WHERE id > ? LIMIT ?
|
||||
""";
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(sql);
|
||||
preparedStatement.setInt(1, lastFetchedId);
|
||||
preparedStatement.setInt(2, pageSize);
|
||||
|
||||
return preparedStatement.executeQuery();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
package jdbcpagination;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class PaginationLogicUnitTest {
|
||||
private static Connection connection = null;
|
||||
private static final String JDBC_URL = "jdbc:h2:mem:testDB";
|
||||
private static final String USERNAME = "dbUser";
|
||||
private static final String PASSWORD = "dbPassword";
|
||||
|
||||
@BeforeAll
|
||||
public static void setup() throws Exception {
|
||||
connection = connect(JDBC_URL, USERNAME, PASSWORD);
|
||||
populateDB();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
public static void tearDown() throws SQLException {
|
||||
destroyDB();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDBPopulated_WhenReadPaginatedWithLimitAndOffset_ThenReturnsPaginatedResult() throws SQLException {
|
||||
int offset = 0; // offset is set to 0 and keep updating with pageSize
|
||||
int pageSize = 100_000;
|
||||
int totalPages = 0;
|
||||
while (true) {
|
||||
ResultSet resultSet = PaginationLogic.readPageWithLimitAndOffset(connection, offset, pageSize);
|
||||
if (!resultSet.next())
|
||||
break;
|
||||
List<String> resultPage = new ArrayList<>();
|
||||
|
||||
do {
|
||||
resultPage.add(resultSet.getString("first_name"));
|
||||
} while (resultSet.next());
|
||||
assertEquals("firstname" + (resultPage.size() * (totalPages + 1)), resultPage.get(resultPage.size() - 1));
|
||||
offset += pageSize;
|
||||
totalPages++;
|
||||
}
|
||||
assertEquals(10, totalPages);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDBPopulated_WhenReadPaginatedWithSortedKeys_ThenReturnsPaginatedResult() throws SQLException {
|
||||
// find min and max ID
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("SELECT min(id) as min_id, max(id) as max_id FROM employees");
|
||||
ResultSet resultSet = preparedStatement.executeQuery();
|
||||
resultSet.next();
|
||||
|
||||
int minId = resultSet.getInt("min_id");
|
||||
int maxId = resultSet.getInt("max_id");
|
||||
int lastFetchedId = 0; // assign lastFetchedId to minId
|
||||
|
||||
int pageSize = 100_000;
|
||||
int totalPages = 0;
|
||||
|
||||
while ((lastFetchedId + pageSize) <= maxId) {
|
||||
resultSet = PaginationLogic.readPageWithSortedKeys(connection, lastFetchedId, pageSize);
|
||||
if (!resultSet.next())
|
||||
break;
|
||||
List<String> resultPage = new ArrayList<>();
|
||||
do {
|
||||
resultPage.add(resultSet.getString("first_name"));
|
||||
lastFetchedId = resultSet.getInt("id");
|
||||
} while (resultSet.next());
|
||||
assertEquals("firstname" + (resultPage.size() * (totalPages + 1)), resultPage.get(resultPage.size() - 1));
|
||||
totalPages++;
|
||||
}
|
||||
assertEquals(10, totalPages);
|
||||
}
|
||||
|
||||
private static void destroyDB() throws SQLException {
|
||||
String destroy = """
|
||||
DROP table IF EXISTS EMPLOYEES;
|
||||
""";
|
||||
connection.prepareStatement(destroy)
|
||||
.execute();
|
||||
}
|
||||
|
||||
private static void populateDB() throws SQLException {
|
||||
String createTable = """
|
||||
CREATE TABLE EMPLOYEES (
|
||||
id SERIAL PRIMARY KEY,
|
||||
first_name VARCHAR(50),
|
||||
last_name VARCHAR(50),
|
||||
salary DECIMAL(10, 2)
|
||||
);
|
||||
""";
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(createTable);
|
||||
preparedStatement.execute();
|
||||
|
||||
String load = """
|
||||
INSERT INTO EMPLOYEES (first_name, last_name, salary)
|
||||
VALUES(?,?,?)
|
||||
""";
|
||||
IntStream.rangeClosed(1, 1_000_000)
|
||||
.forEach(i -> {
|
||||
PreparedStatement preparedStatement1 = null;
|
||||
try {
|
||||
preparedStatement1 = connection.prepareStatement(load);
|
||||
preparedStatement1.setString(1, "firstname" + i);
|
||||
preparedStatement1.setString(2, "lastname" + i);
|
||||
preparedStatement1.setDouble(3, 100_000 + (1_000_000 - 100_000) + Math.random());
|
||||
|
||||
preparedStatement1.execute();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static Connection connect(String url, String user, String password) throws SQLException {
|
||||
Connection connection = DriverManager.getConnection(url, user, password);
|
||||
if (connection != null) {
|
||||
System.out.println("Connected to database");
|
||||
}
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
}
|
|
@ -28,7 +28,7 @@
|
|||
<version>${org.springframework.data.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate-core.version}</version>
|
||||
</dependency>
|
||||
|
@ -48,12 +48,12 @@
|
|||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-testing</artifactId>
|
||||
<version>${hibernate-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-spatial</artifactId>
|
||||
<version>${hibernate-core.version}</version>
|
||||
</dependency>
|
||||
|
@ -87,7 +87,7 @@
|
|||
<!-- Spring -->
|
||||
<org.springframework.version>6.0.6</org.springframework.version>
|
||||
<org.springframework.data.version>3.0.3</org.springframework.data.version>
|
||||
<hibernate-core.version>6.1.7.Final</hibernate-core.version>
|
||||
<hibernate-core.version>6.4.2.Final</hibernate-core.version>
|
||||
<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>
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
package com.baeldung.hibernate.customtypes;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.tool.schema.extract.spi.ColumnTypeInformation;
|
||||
import org.hibernate.type.BasicType;
|
||||
import org.hibernate.type.descriptor.WrapperOptions;
|
||||
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
|
||||
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
|
||||
import org.hibernate.type.spi.TypeConfiguration;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
@ -48,4 +53,9 @@ public class LocalDateStringJavaDescriptor extends AbstractArrayTypeDescriptor<L
|
|||
|
||||
throw unknownWrap(value.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BasicType<?> resolveType(TypeConfiguration typeConfiguration, Dialect dialect, BasicType basicType, ColumnTypeInformation columnTypeInformation, JdbcTypeIndicators jdbcTypeIndicators) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
package com.baeldung.hibernate.softdelete.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.annotations.SoftDelete;
|
||||
import org.hibernate.annotations.SoftDeleteType;
|
||||
import org.hibernate.type.YesNoConverter;
|
||||
|
||||
import jakarta.persistence.CollectionTable;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.ElementCollection;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.NamedNativeQueries;
|
||||
import jakarta.persistence.NamedNativeQuery;
|
||||
|
||||
@Entity
|
||||
@NamedNativeQueries({
|
||||
@NamedNativeQuery(name = "getDeletedPerson", query = "SELECT id, name FROM SoftDeletePerson sdp where sdp.deleted = true", resultClass = SoftDeletePerson.class) })
|
||||
@SoftDelete
|
||||
public class SoftDeletePerson {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@ElementCollection(fetch = FetchType.EAGER)
|
||||
@CollectionTable(name = "Emails", joinColumns = @JoinColumn(name = "id"))
|
||||
@Column(name = "emailId")
|
||||
@SoftDelete(strategy = SoftDeleteType.ACTIVE, converter = YesNoConverter.class)
|
||||
private List<String> emailIds;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<String> getEmailIds() {
|
||||
return emailIds;
|
||||
}
|
||||
|
||||
public void setEmailIds(List<String> emailIds) {
|
||||
this.emailIds = emailIds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SoftDeletePerson{" + "id=" + id + ", name='" + name + '\'' + ", emailIds=" + emailIds + '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
package com.baeldung.hibernate.softdelete;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.h2.Driver;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.dialect.H2Dialect;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.baeldung.hibernate.softdelete.model.SoftDeletePerson;
|
||||
|
||||
public class SoftDeletePersonIntegrationTest {
|
||||
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private static Session session;
|
||||
|
||||
SoftDeletePerson person1 = new SoftDeletePerson();
|
||||
SoftDeletePerson person2 = new SoftDeletePerson();
|
||||
|
||||
@BeforeAll
|
||||
public static void beforeTests() {
|
||||
Configuration configuration = new Configuration().addAnnotatedClass(SoftDeletePerson.class)
|
||||
.setProperty("hibernate.dialect", H2Dialect.class.getName())
|
||||
.setProperty("hibernate.connection.driver_class", Driver.class.getName())
|
||||
.setProperty("hibernate.connection.url", "jdbc:h2:mem:test")
|
||||
.setProperty("hibernate.connection.username", "sa")
|
||||
.setProperty("hibernate.connection.password", "")
|
||||
.setProperty("hibernate.hbm2ddl.auto", "update")
|
||||
.setProperty("hibernate.show_sql", "true");
|
||||
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties())
|
||||
.build();
|
||||
|
||||
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
SoftDeletePerson person1 = new SoftDeletePerson();
|
||||
person1.setName("Person1");
|
||||
List<String> emailIds = new ArrayList<>();
|
||||
emailIds.add("id1@dummy.com");
|
||||
emailIds.add("id2@dummy.com");
|
||||
person1.setEmailIds(emailIds);
|
||||
SoftDeletePerson person2 = new SoftDeletePerson();
|
||||
person2.setName("Person2");
|
||||
List<String> emailIdsPerson2 = new ArrayList<>();
|
||||
emailIdsPerson2.add("person2Id1@dummy.com");
|
||||
emailIdsPerson2.add("person2Id2@dummy.com");
|
||||
person2.setEmailIds(emailIdsPerson2);
|
||||
session.save(person1);
|
||||
session.save(person2);
|
||||
session.getTransaction()
|
||||
.commit();
|
||||
|
||||
assertNotNull(person1.getName());
|
||||
assertNotNull(person2.getName());
|
||||
System.out.println(person1);
|
||||
System.out.println(person2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenDeletingUsingSoftDelete_ThenEntityAndCollectionAreDeleted() {
|
||||
session.beginTransaction();
|
||||
person1 = session.createQuery("from SoftDeletePerson where name='Person1'", SoftDeletePerson.class)
|
||||
.getSingleResult();
|
||||
person2 = session.createQuery("from SoftDeletePerson where name='Person2'", SoftDeletePerson.class)
|
||||
.getSingleResult();
|
||||
|
||||
assertNotNull(person1);
|
||||
assertNotNull(person2);
|
||||
|
||||
session.delete(person2);
|
||||
List<String> emailIds = person1.getEmailIds();
|
||||
emailIds.remove(0);
|
||||
person1.setEmailIds(emailIds);
|
||||
session.save(person1);
|
||||
session.getTransaction()
|
||||
.commit();
|
||||
List<SoftDeletePerson> activeRows = session.createQuery("from SoftDeletePerson")
|
||||
.list();
|
||||
List<SoftDeletePerson> deletedRows = session.createNamedQuery("getDeletedPerson", SoftDeletePerson.class)
|
||||
.getResultList();
|
||||
session.close();
|
||||
|
||||
assertNotNull(person1.getName());
|
||||
System.out.println("-------------Active Rows-----------");
|
||||
activeRows.forEach(row -> System.out.println(row));
|
||||
System.out.println("-------------Deleted Rows-----------");
|
||||
deletedRows.forEach(row -> System.out.println(row));
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void afterTests() {
|
||||
sessionFactory.close();
|
||||
}
|
||||
}
|
|
@ -1,12 +1,12 @@
|
|||
<?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/xsd/maven-4.0.0.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>java-harperdb</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
|
|
@ -33,7 +33,7 @@
|
|||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
<version>${hibernate-core.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -131,6 +131,7 @@
|
|||
<commons-pool.version>1.6</commons-pool.version>
|
||||
<commons-dbcp.version>1.4</commons-dbcp.version>
|
||||
<apt-maven-plugin.version>1.1.3</apt-maven-plugin.version>
|
||||
<hibernate-core.version>6.4.2.Final</hibernate-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -11,7 +11,7 @@ import org.springframework.test.context.ContextConfiguration;
|
|||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import org.junit.Assert;
|
||||
|
||||
@ContextConfiguration("/test-context.xml")
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
|
@ -64,7 +64,7 @@ public class PersonDaoIntegrationTest {
|
|||
personDao.save(new Person("Kent", "Zivago", 30));
|
||||
|
||||
final int maxAge = personDao.findMaxAge();
|
||||
Assert.assertTrue(maxAge == 35);
|
||||
Assert.assertEquals(35, maxAge);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -74,7 +74,7 @@ public class PersonDaoIntegrationTest {
|
|||
personDao.save(new Person("Kent", "Zivago", 30));
|
||||
|
||||
final Map<String, Integer> maxAge = personDao.findMaxAgeByName();
|
||||
Assert.assertTrue(maxAge.size() == 2);
|
||||
Assert.assertEquals(2, maxAge.size());
|
||||
Assert.assertSame(35, maxAge.get("Ralph"));
|
||||
Assert.assertSame(30, maxAge.get("Kent"));
|
||||
}
|
||||
|
|
|
@ -122,10 +122,10 @@ public class QueryDSLIntegrationTest {
|
|||
.fetch();
|
||||
|
||||
assertEquals("Hello World!", userTitleCounts.get(0).get(blogPost.title));
|
||||
assertEquals(new Long(2), userTitleCounts.get(0).get(count));
|
||||
assertEquals(Long.valueOf(2), userTitleCounts.get(0).get(count));
|
||||
|
||||
assertEquals("My Second Post", userTitleCounts.get(1).get(blogPost.title));
|
||||
assertEquals(new Long(1), userTitleCounts.get(1).get(count));
|
||||
assertEquals(Long.valueOf(1), userTitleCounts.get(1).get(count));
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@
|
|||
<version>${mysql.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
</dependency>
|
||||
|
@ -69,9 +69,9 @@
|
|||
<properties>
|
||||
<mysql.version>8.2.0</mysql.version>
|
||||
<hikari.version>4.0.3</hikari.version>
|
||||
<hibernate.version>5.6.1.Final</hibernate.version>
|
||||
<spring-boot.version>2.6.1</spring-boot.version>
|
||||
<spring-test.version>5.3.13</spring-test.version>
|
||||
<hibernate.version>6.4.2.Final</hibernate.version>
|
||||
<spring-boot.version>3.2.2</spring-boot.version>
|
||||
<spring-test.version>6.0.16</spring-test.version>
|
||||
<junit-jupiter.version>5.8.2</junit-jupiter.version>
|
||||
<h2.version>2.1.214</h2.version>
|
||||
</properties>
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package com.baeldung.readonlytransactions.h2;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "book")
|
||||
|
|
|
@ -5,7 +5,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
|
||||
@Service
|
||||
public class BookService {
|
||||
|
|
|
@ -11,7 +11,7 @@ import com.zaxxer.hikari.HikariDataSource;
|
|||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
|
|
|
@ -7,7 +7,7 @@ import org.springframework.orm.jpa.JpaTransactionManager;
|
|||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
|
|
|
@ -7,9 +7,9 @@ import com.baeldung.readonlytransactions.mysql.entities.Book;
|
|||
import java.util.SplittableRandom;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.Persistence;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.Persistence;
|
||||
|
||||
public class MyRepoJPA extends BaseRepo {
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package com.baeldung.readonlytransactions.mysql.entities;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "book")
|
||||
|
|
|
@ -18,7 +18,7 @@ import com.zaxxer.hikari.HikariDataSource;
|
|||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package com.baeldung.readonlytransactions.mysql.spring.entities;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "book")
|
||||
|
|
|
@ -6,7 +6,7 @@ import org.springframework.data.jpa.repository.Query;
|
|||
import com.baeldung.readonlytransactions.mysql.spring.ReaderDS;
|
||||
import com.baeldung.readonlytransactions.mysql.spring.entities.BookEntity;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
import jakarta.transaction.Transactional;
|
||||
|
||||
public interface BookRepository extends JpaRepository<BookEntity, Long> {
|
||||
|
||||
|
|
|
@ -21,8 +21,8 @@ import com.baeldung.readonlytransactions.mysql.spring.ReadOnlyInterception;
|
|||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, initializers = JPATransactionIntegrationTest.TestConfig.class, classes = { ReadOnlyInterception.class })
|
||||
|
|
|
@ -21,8 +21,8 @@ import com.baeldung.readonlytransactions.h2.BookService;
|
|||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityManagerFactory;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, initializers = SpringTransactionReadOnlyIntegrationTest.TestConfig.class, classes = { BookService.class })
|
||||
|
|
|
@ -8,16 +8,14 @@
|
|||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>scylladb</name>
|
||||
<description>Sample ScyllaDB Project</description>
|
||||
<properties>
|
||||
<testcontainers.version>1.17.6</testcontainers.version>
|
||||
<spring-boot.repackage.skip>true</spring-boot.repackage.skip>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -85,4 +83,9 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<testcontainers.version>1.17.6</testcontainers.version>
|
||||
<spring-boot.repackage.skip>true</spring-boot.repackage.skip>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -59,9 +59,8 @@
|
|||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
<properties>
|
||||
<spring.boot.dependencies>3.2.0</spring.boot.dependencies>
|
||||
<spring.boot.dependencies>3.2.2</spring.boot.dependencies>
|
||||
<org.slf4j.version>2.0.9</org.slf4j.version>
|
||||
<logback.version>1.4.14</logback.version>
|
||||
</properties>
|
||||
|
|
|
@ -57,6 +57,17 @@
|
|||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.modelmapper</groupId>
|
||||
<artifactId>modelmapper</artifactId>
|
||||
<version>${modelmapper.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -69,13 +80,15 @@
|
|||
</build>
|
||||
|
||||
<properties>
|
||||
<spring.boot.dependencies>3.1.0</spring.boot.dependencies>
|
||||
<spring.boot.dependencies>3.2.2</spring.boot.dependencies>
|
||||
<junit-jupiter.version>5.9.3</junit-jupiter.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<db.util.version>1.0.7</db.util.version>
|
||||
<hypersistence-utils.version>3.7.0</hypersistence-utils.version>
|
||||
<jackson.version>2.16.0</jackson.version>
|
||||
<modelmapper.version>3.2.0</modelmapper.version>
|
||||
<lombok.version>1.18.30</lombok.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -4,8 +4,10 @@ import java.util.List;
|
|||
import java.util.Optional;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class GroupService {
|
||||
|
||||
private final GroupRepository groupRepository;
|
||||
|
|
|
@ -4,8 +4,10 @@ import java.util.List;
|
|||
import java.util.Optional;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class GroupService {
|
||||
|
||||
private final GroupRepository groupRepository;
|
||||
|
|
|
@ -4,8 +4,10 @@ import java.util.List;
|
|||
import java.util.Optional;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class GroupService {
|
||||
|
||||
private final GroupRepository groupRepository;
|
||||
|
@ -27,3 +29,4 @@ public class GroupService {
|
|||
groupRepository.save(group);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.paging;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class PagingApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PagingApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.paging;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Entity
|
||||
@Table(name = "school")
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(of = {"id"})
|
||||
public class School {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "school_id")
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.paging;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.NamedAttributeNode;
|
||||
import jakarta.persistence.NamedEntityGraph;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Entity
|
||||
@Table(name = "student")
|
||||
@NamedEntityGraph(name = "Student.school", attributeNodes = @NamedAttributeNode("school"))
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(of = {"id"})
|
||||
public class Student {
|
||||
|
||||
@Id
|
||||
@Column(name = "student_id")
|
||||
private String id;
|
||||
|
||||
@Column(name = "first_name")
|
||||
private String firstName;
|
||||
|
||||
@Column(name = "last_name")
|
||||
private String lastName;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "school_id", referencedColumnName = "school_id")
|
||||
private School school;
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.paging;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
|
||||
public interface StudentCustomQueryRepository extends JpaRepository<Student, String> {
|
||||
|
||||
@Query(value = "SELECT stu FROM Student stu LEFT JOIN FETCH stu.school ",
|
||||
countQuery = "SELECT COUNT(stu) FROM Student stu")
|
||||
Page<Student> findAll(Pageable pageable);
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.paging;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(of = {"id"})
|
||||
public class StudentDTO {
|
||||
|
||||
private String id;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.paging;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface StudentEntityGraphRepository extends JpaRepository<Student, String> {
|
||||
|
||||
@EntityGraph(attributePaths = "school")
|
||||
Page<Student> findAll(Pageable pageable);
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.paging;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface StudentNamedEntityGraphRepository extends JpaRepository<Student, String> {
|
||||
|
||||
@EntityGraph(value = "Student.school")
|
||||
Page<Student> findAll(Pageable pageable);
|
||||
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.paging;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface StudentRepository extends JpaRepository<Student, String> {
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.paging;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(of = {"id"})
|
||||
public class StudentWithSchoolNameDTO {
|
||||
|
||||
private String id;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private String schoolName;
|
||||
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
package com.baeldung.paging;
|
||||
|
||||
import com.baeldung.listvsset.util.TestConfig;
|
||||
import io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import static io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator.assertSelectCount;
|
||||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
|
||||
|
||||
@SpringBootTest(classes = {PagingApplication.class, TestConfig.class}, properties = {
|
||||
"spring.jpa.show-sql=true",
|
||||
"spring.jpa.properties.hibernate.format_sql=true",
|
||||
"spring.jpa.generate-ddl=true",
|
||||
"spring.jpa.defer-datasource-initialization=true",
|
||||
"spring.sql.init.data-locations=classpath:school-student-data.sql"
|
||||
})
|
||||
@Transactional
|
||||
class StudentRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private StudentRepository studentRepository;
|
||||
|
||||
@Autowired
|
||||
private StudentCustomQueryRepository studentCustomQueryRepository;
|
||||
|
||||
@Autowired
|
||||
private StudentEntityGraphRepository studentEntityGraphRepository;
|
||||
|
||||
@Autowired
|
||||
private StudentNamedEntityGraphRepository studentNamedEntityGraphRepository;
|
||||
|
||||
private static final ModelMapper modelMapper = new ModelMapper();
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
SQLStatementCountValidator.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetStudentsWithPageRequestOfTwo_thenReturnTwoRows() {
|
||||
int rows = 2;
|
||||
Pageable pageable = PageRequest.of(0, rows);
|
||||
Page<Student> studentPage = studentRepository.findAll(pageable);
|
||||
|
||||
// Then
|
||||
List<Student> studentList = studentPage.getContent();
|
||||
assertThat(studentList.size()).isEqualTo(rows);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetStudentsWithUnpagedAndSort_thenReturnAllResultsSorted() {
|
||||
Sort sort = Sort.by(Sort.Direction.ASC, "lastName");
|
||||
Pageable pageable = PageRequest.of(0, Integer.MAX_VALUE).withSort(sort);
|
||||
Page<Student> studentPage = studentRepository.findAll(pageable);
|
||||
|
||||
// Then
|
||||
List<Student> studentList = studentPage.getContent();
|
||||
assertThat(studentList.size()).isEqualTo(studentPage.getTotalElements());
|
||||
assertThat(studentList).isSortedAccordingTo(Comparator.comparing(Student::getLastName));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenGetStudentsWithSchool_thenMultipleSelectQueriesAreExecuted() {
|
||||
Page<Student> studentPage = studentRepository.findAll(Pageable.unpaged());
|
||||
studentPage.get().map(student -> modelMapper.map(student, StudentWithSchoolNameDTO.class)).toList();
|
||||
assertSelectCount(studentPage.getContent().size() + 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetStudentsByCustomQuery_thenOneSelectQueryIsExecuted() {
|
||||
Page<Student> studentPage = studentCustomQueryRepository.findAll(Pageable.unpaged());
|
||||
studentPage.get().map(student -> modelMapper.map(student, StudentWithSchoolNameDTO.class)).toList();
|
||||
assertSelectCount(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetStudentsByEntityGraph_thenOneSelectQueryIsExecuted() {
|
||||
Page<Student> studentPage = studentEntityGraphRepository.findAll(Pageable.unpaged());
|
||||
studentPage.get().map(student -> modelMapper.map(student, StudentWithSchoolNameDTO.class)).toList();
|
||||
assertSelectCount(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetStudentsByNamedEntityGraph_thenOneSelectQueryIsExecuted() {
|
||||
Page<Student> studentPage = studentNamedEntityGraphRepository.findAll(Pageable.unpaged());
|
||||
studentPage.get().map(student -> modelMapper.map(student, StudentWithSchoolNameDTO.class)).toList();
|
||||
assertSelectCount(1);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
INSERT INTO school (name) VALUES ('Ada Lovelace CE High School');
|
||||
INSERT INTO school (name) VALUES ('Ealing Fields High School');
|
||||
INSERT INTO school (name) VALUES ('Northolt High School');
|
||||
INSERT INTO school (name) VALUES ('Villiers High School');
|
||||
|
||||
INSERT INTO student(student_id, first_name, last_name, school_id) VALUES('23056746', 'James', 'Drover', 1);
|
||||
INSERT INTO student(student_id, first_name, last_name, school_id) VALUES('23056751', 'Rubin', 'Webber', 2);
|
||||
INSERT INTO student(student_id, first_name, last_name, school_id) VALUES('23063444', 'Sarah', 'Pelham', 3);
|
||||
INSERT INTO student(student_id, first_name, last_name, school_id) VALUES('23065783', 'Lucy', 'Watson', 4);
|
|
@ -49,7 +49,7 @@
|
|||
<dependency>
|
||||
<groupId>org.hibernate.orm</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>6.3.1.Final</version>
|
||||
<version>${hibernate.core.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@ -57,6 +57,7 @@
|
|||
<!-- The main class to start by executing java -jar -->
|
||||
<start-class>com.baeldung.h2db.demo.server.SpringBootApp</start-class>
|
||||
<db-util.version>1.0.7</db-util.version>
|
||||
<hibernate.core.version>6.4.2.Final</hibernate.core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -5,23 +5,8 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-data-jpa-query-3</artifactId>
|
||||
<name>spring-data-jpa-query-3</name>
|
||||
<properties>
|
||||
<javafaker.version>0.15</javafaker.version>
|
||||
</properties>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>9</source>
|
||||
<target>9</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<parent>
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
@ -52,10 +37,27 @@
|
|||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
<scope>test</scope>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>9</source>
|
||||
<target>9</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<javafaker.version>0.15</javafaker.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -8,9 +8,9 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package com.baeldung.aggregation.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package com.baeldung.aggregation.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package com.baeldung.boot.passenger;
|
||||
|
||||
import javax.persistence.Basic;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Basic;
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
|
|
|
@ -2,8 +2,8 @@ package com.baeldung.boot.passenger;
|
|||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package com.baeldung.entitygraph.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class Characteristic {
|
||||
|
|
|
@ -3,11 +3,11 @@ package com.baeldung.entitygraph.model;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.NamedAttributeNode;
|
||||
import javax.persistence.NamedEntityGraph;
|
||||
import javax.persistence.OneToMany;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.NamedAttributeNode;
|
||||
import jakarta.persistence.NamedEntityGraph;
|
||||
import jakarta.persistence.OneToMany;
|
||||
|
||||
@Entity
|
||||
@NamedEntityGraph(name = "Item.characteristics",
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package com.baeldung.exists;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
/**
|
||||
* @author paullatzelsperger
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package com.baeldung.joins.model;
|
||||
|
||||
import java.util.List;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.OneToMany;
|
||||
|
||||
@Entity
|
||||
public class Department {
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package com.baeldung.joins.model;
|
||||
|
||||
import java.util.List;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "joins_employee")
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
package com.baeldung.joins.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
public class Phone {
|
||||
|
|
|
@ -12,11 +12,8 @@ import org.springframework.data.domain.PageRequest;
|
|||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.boot.passenger.Passenger;
|
||||
import com.baeldung.boot.passenger.PassengerRepository;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
|
|
|
@ -6,14 +6,15 @@ import com.baeldung.joins.model.Department;
|
|||
import com.baeldung.joins.model.Phone;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.TypedQuery;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
|
@ -30,7 +31,7 @@ public class JpaJoinsIntegrationTest {
|
|||
|
||||
List<Department> resultList = query.getResultList();
|
||||
|
||||
assertThat(resultList).hasSize(3);
|
||||
assertThat(resultList).hasSize(2);
|
||||
assertThat(resultList).extracting("name")
|
||||
.containsOnly("Infra", "Accounting", "Accounting");
|
||||
}
|
||||
|
@ -41,7 +42,7 @@ public class JpaJoinsIntegrationTest {
|
|||
|
||||
List<Department> resultList = query.getResultList();
|
||||
|
||||
assertThat(resultList).hasSize(3);
|
||||
assertThat(resultList).hasSize(2);
|
||||
assertThat(resultList).extracting("name")
|
||||
.containsOnly("Infra", "Accounting", "Accounting");
|
||||
}
|
||||
|
@ -52,7 +53,7 @@ public class JpaJoinsIntegrationTest {
|
|||
|
||||
List<Department> resultList = query.getResultList();
|
||||
|
||||
assertThat(resultList).hasSize(3);
|
||||
assertThat(resultList).hasSize(2);
|
||||
assertThat(resultList).extracting("name")
|
||||
.containsOnly("Infra", "Accounting", "Accounting");
|
||||
}
|
||||
|
@ -63,7 +64,7 @@ public class JpaJoinsIntegrationTest {
|
|||
|
||||
List<Department> resultList = query.getResultList();
|
||||
|
||||
assertThat(resultList).hasSize(3);
|
||||
assertThat(resultList).hasSize(2);
|
||||
assertThat(resultList).extracting("name")
|
||||
.containsOnly("Infra", "Accounting", "Accounting");
|
||||
}
|
||||
|
@ -74,11 +75,12 @@ public class JpaJoinsIntegrationTest {
|
|||
|
||||
List<Department> resultList = query.getResultList();
|
||||
|
||||
assertThat(resultList).hasSize(9);
|
||||
assertThat(resultList).hasSize(3);
|
||||
assertThat(resultList).extracting("name")
|
||||
.containsOnly("Infra", "Accounting", "Management", "Infra", "Accounting", "Management", "Infra", "Accounting", "Management");
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void whenCollectionValuedAssociationIsJoined_ThenCanSelect() {
|
||||
TypedQuery<Phone> query = entityManager.createQuery("SELECT ph FROM Employee e JOIN e.phones ph WHERE ph LIKE '1%'", Phone.class);
|
||||
|
@ -116,7 +118,7 @@ public class JpaJoinsIntegrationTest {
|
|||
|
||||
List<Department> resultList = query.getResultList();
|
||||
|
||||
assertThat(resultList).hasSize(3);
|
||||
assertThat(resultList).hasSize(2);
|
||||
assertThat(resultList).extracting("name")
|
||||
.containsOnly("Infra", "Accounting", "Accounting");
|
||||
}
|
||||
|
@ -127,7 +129,7 @@ public class JpaJoinsIntegrationTest {
|
|||
|
||||
List<Department> resultList = query.getResultList();
|
||||
|
||||
assertThat(resultList).hasSize(4);
|
||||
assertThat(resultList).hasSize(3);
|
||||
assertThat(resultList).extracting("name")
|
||||
.containsOnly("Infra", "Accounting", "Accounting", "Management");
|
||||
}
|
||||
|
|
|
@ -101,7 +101,7 @@
|
|||
<org.springframework.data.version>3.1.3</org.springframework.data.version>
|
||||
<org.springframework.security.version>6.1.3</org.springframework.security.version>
|
||||
<!-- persistence -->
|
||||
<hibernate.version>6.2.8.Final</hibernate.version>
|
||||
<hibernate.version>6.4.2.Final</hibernate.version>
|
||||
<mysql-connector-java.version>8.2.0</mysql-connector-java.version>
|
||||
<tomcat-dbcp.version>9.0.80</tomcat-dbcp.version>
|
||||
</properties>
|
||||
|
|
|
@ -61,7 +61,7 @@
|
|||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
<version>${hibernate-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
|
@ -127,9 +127,9 @@
|
|||
<spring-boot.version>3.1.0</spring-boot.version>
|
||||
<!-- persistence -->
|
||||
<tomcat-dbcp.version>10.1.9</tomcat-dbcp.version>
|
||||
<hibernate-core.version>6.4.2.Final</hibernate-core.version>
|
||||
<jakson-databind.version>2.16.1</jakson-databind.version>
|
||||
<jakson-core.version>2.16.1</jakson-core.version>
|
||||
|
||||
<jjwt.version>0.12.3</jjwt.version>
|
||||
<logback.version>1.4.14</logback.version>
|
||||
</properties>
|
||||
|
|
|
@ -41,7 +41,7 @@
|
|||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<version>${hibernate.version}</version>
|
||||
<version>${hibernate-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xml-apis</groupId>
|
||||
|
@ -127,6 +127,7 @@
|
|||
<h2.version>2.1.214</h2.version>
|
||||
<expressly-language.version>5.0.0</expressly-language.version>
|
||||
<jakarta-servlet-jpa-jstl-api.version>3.0.0</jakarta-servlet-jpa-jstl-api.version>
|
||||
<hibernate-core.version>6.4.2.Final</hibernate-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
8
pom.xml
8
pom.xml
|
@ -733,7 +733,7 @@
|
|||
<module>jackson-simple</module>
|
||||
<module>java-blockchain</module>
|
||||
<module>java-jdi</module>
|
||||
<module>java-panama</module>
|
||||
<!--<module>java-panama</module>--> <!--JAVA-27339-->
|
||||
<module>javafx</module>
|
||||
<module>javax-sound</module>
|
||||
<module>javaxval-2</module>
|
||||
|
@ -817,6 +817,7 @@
|
|||
<module>spring-5-webflux-2</module>
|
||||
<module>spring-5-webflux</module>
|
||||
<module>spring-5</module>
|
||||
<module>spring-6-rsocket</module>
|
||||
<module>spring-activiti</module>
|
||||
<module>spring-actuator</module>
|
||||
<module>spring-ai</module>
|
||||
|
@ -973,7 +974,7 @@
|
|||
<module>jackson-simple</module>
|
||||
<module>java-blockchain</module>
|
||||
<module>java-jdi</module>
|
||||
<module>java-panama</module>
|
||||
<!--<module>java-panama</module>--> <!--JAVA-27339-->
|
||||
<module>javafx</module>
|
||||
<module>javax-sound</module>
|
||||
<module>javaxval-2</module>
|
||||
|
@ -1041,6 +1042,7 @@
|
|||
<module>patterns-modules</module>
|
||||
<module>performance-tests</module>
|
||||
<module>persistence-modules</module>
|
||||
<!--<module>persistence-modules/java-harperdb</module>--> <!-- This module requires a library to download manually -->
|
||||
<module>persistence-modules/spring-data-neo4j</module>
|
||||
<module>protobuffer</module>
|
||||
<module>quarkus-modules</module>
|
||||
|
@ -1056,6 +1058,7 @@
|
|||
<module>spring-5-webflux-2</module>
|
||||
<module>spring-5-webflux</module>
|
||||
<module>spring-5</module>
|
||||
<module>spring-6-rsocket</module>
|
||||
<module>spring-activiti</module>
|
||||
<module>spring-actuator</module>
|
||||
<module>spring-ai</module>
|
||||
|
@ -1092,6 +1095,7 @@
|
|||
<module>spring-jersey</module>
|
||||
<module>spring-jinq</module>
|
||||
<module>spring-kafka-2</module>
|
||||
<module>spring-kafka-3</module>
|
||||
<module>spring-kafka</module>
|
||||
<module>spring-katharsis</module>
|
||||
<module>spring-mobile</module>
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
<module>quarkus-jandex</module>
|
||||
<module>quarkus-vs-springboot</module>
|
||||
<module>quarkus-funqy</module>
|
||||
<!-- requires Java 21 <module>quarkus-virtual-threads</module> -->
|
||||
</modules>
|
||||
|
||||
</project>
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue