diff --git a/core-java-modules/core-java-collections-maps-6/pom.xml b/core-java-modules/core-java-collections-maps-6/pom.xml
new file mode 100644
index 0000000000..9910d08691
--- /dev/null
+++ b/core-java-modules/core-java-collections-maps-6/pom.xml
@@ -0,0 +1,20 @@
+
+
+ core-java-collections-maps-6
+ 0.1.0-SNAPSHOT
+ core-java-collections-maps-6
+ jar
+
+ core-java-modules
+ com.baeldung.core-java-modules
+ 0.0.1-SNAPSHOT
+
+ 4.0.0
+
+
+ 5.2.5.RELEASE
+
+
+
\ No newline at end of file
diff --git a/core-java-modules/core-java-collections-maps-6/src/main/java/com/baeldung/map/hashmapcopy/CopyingAHashMapToAnother.java b/core-java-modules/core-java-collections-maps-6/src/main/java/com/baeldung/map/hashmapcopy/CopyingAHashMapToAnother.java
new file mode 100644
index 0000000000..77a6402a75
--- /dev/null
+++ b/core-java-modules/core-java-collections-maps-6/src/main/java/com/baeldung/map/hashmapcopy/CopyingAHashMapToAnother.java
@@ -0,0 +1,47 @@
+package com.baeldung.map.hashmapcopy;
+
+import java.util.Map;
+
+import com.google.common.collect.MapDifference;
+import com.google.common.collect.Maps;
+
+public class CopyingAHashMapToAnother {
+ public Map copyByIteration(Map sourceMap, Map targetMap) {
+ for (Map.Entry entry : sourceMap.entrySet()) {
+ if (!targetMap.containsKey(entry.getKey())) {
+ targetMap.put(entry.getKey(), entry.getValue());
+ }
+ }
+ return targetMap;
+ }
+
+ public Map copyUsingPutAll(Map sourceMap, Map targetMap) {
+ sourceMap.keySet()
+ .removeAll(targetMap.keySet());
+ targetMap.putAll(sourceMap);
+ return targetMap;
+ }
+
+ public Map copyUsingPutIfAbsent(Map sourceMap, Map targetMap) {
+ for (Map.Entry entry : sourceMap.entrySet()) {
+ targetMap.putIfAbsent(entry.getKey(), entry.getValue());
+ }
+ return targetMap;
+ }
+
+ public Map copyUsingPutIfAbsentForEach(Map sourceMap, Map targetMap) {
+ sourceMap.forEach(targetMap::putIfAbsent);
+ return targetMap;
+ }
+
+ public Map copyUsingMapMerge(Map sourceMap, Map targetMap) {
+ sourceMap.forEach((key, value) -> targetMap.merge(key, value, (oldVal, newVal) -> oldVal));
+ return targetMap;
+ }
+
+ public Map copyUsingGuavaMapDifference(Map sourceMap, Map targetMap) {
+ MapDifference differenceMap = Maps.difference(sourceMap, targetMap);
+ targetMap.putAll(differenceMap.entriesOnlyOnLeft());
+ return targetMap;
+ }
+}
diff --git a/core-java-modules/core-java-collections-maps-6/src/test/java/com/baeldung/map/hashmapcopy/CopyHashMapIntoAnotherUnitTest.java b/core-java-modules/core-java-collections-maps-6/src/test/java/com/baeldung/map/hashmapcopy/CopyHashMapIntoAnotherUnitTest.java
new file mode 100644
index 0000000000..b11f470eb3
--- /dev/null
+++ b/core-java-modules/core-java-collections-maps-6/src/test/java/com/baeldung/map/hashmapcopy/CopyHashMapIntoAnotherUnitTest.java
@@ -0,0 +1,68 @@
+package com.baeldung.map.hashmapcopy;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.baeldung.map.hashmapcopy.CopyingAHashMapToAnother;
+
+public class CopyHashMapIntoAnotherUnitTest {
+ @Test
+ public void givenSourceAndTargetMapsWhenIteratedOverThenCopyingSuccess(){
+ CopyingAHashMapToAnother obj = new CopyingAHashMapToAnother();
+ Assert.assertEquals(generateExpectedResultMap(), obj.copyByIteration(generateSourceMap(), generateTargetMap()));
+ }
+
+ @Test
+ public void givenSourceAndTargetMapsWhenUsedPutAllThenCopyingSuccess(){
+ CopyingAHashMapToAnother obj = new CopyingAHashMapToAnother();
+ Assert.assertEquals(generateExpectedResultMap(), obj.copyUsingPutAll(generateSourceMap(), generateTargetMap()));
+ }
+
+ @Test
+ public void givenSourceAndTargetMapsWhenUsedPutIfAbsentThenCopyingSuccess(){
+ CopyingAHashMapToAnother obj = new CopyingAHashMapToAnother();
+ Assert.assertEquals(generateExpectedResultMap(), obj.copyUsingPutIfAbsent(generateSourceMap(), generateTargetMap()));
+ Assert.assertEquals(generateExpectedResultMap(), obj.copyUsingPutIfAbsentForEach(generateSourceMap(), generateTargetMap()));
+ }
+
+ @Test
+ public void givenSourceAndTargetMapsWhenUsedMapMergeThenCopyingSuccess(){
+ CopyingAHashMapToAnother obj = new CopyingAHashMapToAnother();
+ Assert.assertEquals(generateExpectedResultMap(), obj.copyUsingMapMerge(generateSourceMap(), generateTargetMap()));
+ }
+
+ @Test
+ public void givenSourceAndTargetMapsWhenMapDifferenceUsedThenCopyingSuccess(){
+ CopyingAHashMapToAnother obj = new CopyingAHashMapToAnother();
+ Assert.assertEquals(generateExpectedResultMap(), obj.copyUsingGuavaMapDifference(generateSourceMap(), generateTargetMap()));
+ }
+
+ private Map generateSourceMap(){
+ Map sourceMap = new HashMap<>();
+ sourceMap.put("India", "Delhi");
+ sourceMap.put("United States", "Washington D.C.");
+ sourceMap.put("United Kingdom", "London DC");
+ return sourceMap;
+ }
+
+ private Map generateTargetMap(){
+ Map targetMap = new HashMap<>();
+ targetMap.put("Zimbabwe", "Harare");
+ targetMap.put("Norway", "Oslo");
+ targetMap.put("United Kingdom", "London");
+ return targetMap;
+ }
+
+ private Map generateExpectedResultMap(){
+ Map resultMap = new HashMap<>();
+ resultMap.put("India", "Delhi");
+ resultMap.put("United States", "Washington D.C.");
+ resultMap.put("United Kingdom", "London");
+ resultMap.put("Zimbabwe", "Harare");
+ resultMap.put("Norway", "Oslo");
+ return resultMap;
+ }
+}
diff --git a/core-java-modules/core-java-security-3/src/main/java/com/baeldung/crypto/exception/BadPaddingExamples.java b/core-java-modules/core-java-security-3/src/main/java/com/baeldung/crypto/exception/BadPaddingExamples.java
index 3fff4410e3..135510785f 100644
--- a/core-java-modules/core-java-security-3/src/main/java/com/baeldung/crypto/exception/BadPaddingExamples.java
+++ b/core-java-modules/core-java-security-3/src/main/java/com/baeldung/crypto/exception/BadPaddingExamples.java
@@ -16,7 +16,7 @@ public class BadPaddingExamples {
SecretKey encryptionKey = CryptoUtils.getKeyForText("BaeldungIsASuperCoolSite");
SecretKey differentKey = CryptoUtils.getKeyForText("ThisGivesUsAnAlternative");
- Cipher cipher = Cipher.getInstance("AES/ECB/ISO10126Padding");
+ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, encryptionKey);
byte[] cipherTextBytes = cipher.doFinal(plainTextBytes);
@@ -28,12 +28,12 @@ public class BadPaddingExamples {
public static byte[] encryptAndDecryptUsingDifferentAlgorithms(SecretKey key, IvParameterSpec ivParameterSpec,
byte[] plainTextBytes) throws InvalidKeyException, GeneralSecurityException {
- Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding");
+ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
byte[] cipherTextBytes = cipher.doFinal(plainTextBytes);
- cipher = Cipher.getInstance("AES/ECB/ISO10126Padding");
+ cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml
index 612e607a38..1033213cd1 100644
--- a/core-java-modules/pom.xml
+++ b/core-java-modules/pom.xml
@@ -132,6 +132,7 @@
core-java-regex-2
core-java-uuid
pre-jpms
+ core-java-collections-maps-6
diff --git a/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/config/MongoClientConfig.java b/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/config/MongoClientConfig.java
index e63034a5b5..0dff1ec86d 100644
--- a/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/config/MongoClientConfig.java
+++ b/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/config/MongoClientConfig.java
@@ -14,9 +14,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
-import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
-import com.baeldung.boot.csfle.config.converter.BinaryConverter;
import com.mongodb.AutoEncryptionSettings;
import com.mongodb.ClientEncryptionSettings;
import com.mongodb.ConnectionString;
@@ -50,11 +48,6 @@ public class MongoClientConfig extends AbstractMongoClientConfiguration {
return db;
}
- @Override
- public MongoCustomConversions customConversions() {
- return new MongoCustomConversions(Arrays.asList(new BinaryConverter()));
- }
-
@Bean
@Override
public MongoClient mongoClient() {
diff --git a/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/config/converter/BinaryConverter.java b/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/config/converter/BinaryConverter.java
deleted file mode 100644
index 15231551fc..0000000000
--- a/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/config/converter/BinaryConverter.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package com.baeldung.boot.csfle.config.converter;
-
-import org.bson.BsonBinary;
-import org.bson.types.Binary;
-import org.springframework.core.convert.converter.Converter;
-
-public class BinaryConverter implements Converter {
-
- @Override
- public BsonBinary convert(Binary source) {
- return new BsonBinary(source.getType(), source.getData());
- }
-}
diff --git a/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/data/Citizen.java b/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/data/Citizen.java
index 9d6496a17b..11e776123a 100644
--- a/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/data/Citizen.java
+++ b/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/data/Citizen.java
@@ -13,7 +13,9 @@ public class Citizen {
}
public Citizen(EncryptedCitizen encryptedCitizen) {
- this.name = encryptedCitizen.getName();
+ if (encryptedCitizen != null) {
+ this.name = encryptedCitizen.getName();
+ }
}
public String getName() {
diff --git a/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/data/EncryptedCitizen.java b/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/data/EncryptedCitizen.java
index 01c9245fbf..c7ca5566a9 100644
--- a/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/data/EncryptedCitizen.java
+++ b/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/data/EncryptedCitizen.java
@@ -1,14 +1,14 @@
package com.baeldung.boot.csfle.data;
-import org.bson.BsonBinary;
+import org.bson.types.Binary;
import org.springframework.data.mongodb.core.mapping.Document;
@Document("citizens")
public class EncryptedCitizen {
private String name;
- private BsonBinary email;
- private BsonBinary birthYear;
+ private Binary email;
+ private Binary birthYear;
public EncryptedCitizen() {
}
@@ -25,19 +25,19 @@ public class EncryptedCitizen {
this.name = name;
}
- public BsonBinary getEmail() {
+ public Binary getEmail() {
return email;
}
- public void setEmail(BsonBinary email) {
+ public void setEmail(Binary email) {
this.email = email;
}
- public BsonBinary getBirthYear() {
+ public Binary getBirthYear() {
return birthYear;
}
- public void setBirthYear(BsonBinary birthYear) {
+ public void setBirthYear(Binary birthYear) {
this.birthYear = birthYear;
}
diff --git a/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/service/CitizenService.java b/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/service/CitizenService.java
index 91b5940b25..6b3c463d0d 100644
--- a/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/service/CitizenService.java
+++ b/persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/service/CitizenService.java
@@ -7,6 +7,7 @@ import org.bson.BsonBinary;
import org.bson.BsonInt32;
import org.bson.BsonString;
import org.bson.BsonValue;
+import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
@@ -65,7 +66,7 @@ public class CitizenService {
}
}
- public BsonBinary encrypt(Object value, String algorithm) {
+ public Binary encrypt(Object value, String algorithm) {
if (value == null)
return null;
@@ -80,17 +81,22 @@ public class CitizenService {
EncryptOptions options = new EncryptOptions(algorithm);
options.keyId(encryptionConfig.getDataKeyId());
- return clientEncryption.encrypt(bsonValue, options);
+
+ BsonBinary encryptedValue = clientEncryption.encrypt(bsonValue, options);
+ return new Binary(encryptedValue.getType(), encryptedValue.getData());
}
- public BsonValue decryptProperty(BsonBinary value) {
+ public BsonValue decryptProperty(Binary value) {
if (value == null)
return null;
- return clientEncryption.decrypt(value);
+ return clientEncryption.decrypt(new BsonBinary(value.getType(), value.getData()));
}
private Citizen decrypt(EncryptedCitizen encrypted) {
+ if (encrypted == null)
+ return null;
+
Citizen citizen = new Citizen(encrypted);
BsonValue decryptedBirthYear = decryptProperty(encrypted.getBirthYear());
diff --git a/persistence-modules/spring-boot-persistence-mongodb-3/src/test/java/com/baeldung/boot/csfle/CitizenServiceLiveTest.java b/persistence-modules/spring-boot-persistence-mongodb-3/src/test/java/com/baeldung/boot/csfle/CitizenServiceLiveTest.java
index 5d0a931bb9..471cb2883a 100644
--- a/persistence-modules/spring-boot-persistence-mongodb-3/src/test/java/com/baeldung/boot/csfle/CitizenServiceLiveTest.java
+++ b/persistence-modules/spring-boot-persistence-mongodb-3/src/test/java/com/baeldung/boot/csfle/CitizenServiceLiveTest.java
@@ -1,8 +1,10 @@
package com.baeldung.boot.csfle;
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
-import org.bson.BsonBinary;
+import org.bson.types.Binary;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -36,7 +38,7 @@ public class CitizenServiceLiveTest {
citizen.setName("Foo");
citizen.setEmail("foo@citizen.com");
- BsonBinary encryptedEmail = service.encrypt(citizen.getEmail(), CitizenService.DETERMINISTIC_ALGORITHM);
+ Binary encryptedEmail = service.encrypt(citizen.getEmail(), CitizenService.DETERMINISTIC_ALGORITHM);
EncryptedCitizen saved = service.save(citizen);
assertEquals(encryptedEmail, saved.getEmail());
diff --git a/persistence-modules/spring-data-jpa-repo-2/src/main/resources/application.properties b/persistence-modules/spring-data-jpa-repo-2/src/main/resources/application.properties
index 3ca0cc1242..db4837d8d2 100644
--- a/persistence-modules/spring-data-jpa-repo-2/src/main/resources/application.properties
+++ b/persistence-modules/spring-data-jpa-repo-2/src/main/resources/application.properties
@@ -3,4 +3,9 @@ spring.datasource.username=sa
spring.datasource.password=sa
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
-logging.level.com.baeldung.spring.data.persistence.search=debug
\ No newline at end of file
+logging.level.com.baeldung.spring.data.persistence.search=debug
+
+spring.jpa.show-sql=true
+logging.level.org.hibernate.SQL=DEBUG
+logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
+spring.jpa.properties.hibernate.format_sql=true