diff --git a/core-java-modules/core-java-8-2/pom.xml b/core-java-modules/core-java-8-2/pom.xml index fbaf795b95..ff2e290086 100644 --- a/core-java-modules/core-java-8-2/pom.xml +++ b/core-java-modules/core-java-8-2/pom.xml @@ -22,6 +22,9 @@ 1.8 1.8 64.2 + 5.4.0.Final + 1.4.197 + 2.9.8 @@ -30,7 +33,21 @@ icu4j ${icu.version} - + + org.hibernate + hibernate-core + ${hibernate.core.version} + + + com.h2database + h2 + ${h2database.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.databind.version} + diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/HandleOptionalTypeExample.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/HandleOptionalTypeExample.java new file mode 100644 index 0000000000..c472bab077 --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/HandleOptionalTypeExample.java @@ -0,0 +1,41 @@ +package com.baeldung.optionalReturnType; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +public class HandleOptionalTypeExample { + static Map usersByName = new HashMap(); + static { + User user1 = new User(); + user1.setUserId(1l); + user1.setFirstName("baeldung"); + usersByName.put("baeldung", user1); + } + + public static void main(String[] args) { + changeUserName("baeldung", "baeldung-new"); + changeUserName("user", "user-new"); + } + + public static void changeUserName(String oldFirstName, String newFirstName) { + Optional userOpt = findUserByName(oldFirstName); + if (userOpt.isPresent()) { + User user = userOpt.get(); + user.setFirstName(newFirstName); + + System.out.println("user with name " + oldFirstName + " is changed to " + user.getFirstName()); + } else { + // user is missing + System.out.println("user with name " + oldFirstName + " is not found."); + } + } + + public static Optional findUserByName(String name) { + // look up the user in the database, the user object below could be null + User user = usersByName.get(name); + Optional opt = Optional.ofNullable(user); + + return opt; + } +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/OptionalToJsonExample.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/OptionalToJsonExample.java new file mode 100644 index 0000000000..b44a35fae1 --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/OptionalToJsonExample.java @@ -0,0 +1,19 @@ +package com.baeldung.optionalReturnType; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class OptionalToJsonExample { + public static void main(String[] args) { + UserOptional user = new UserOptional(); + user.setUserId(1l); + user.setFirstName("Bael Dung"); + + ObjectMapper om = new ObjectMapper(); + try { + System.out.print("user in json is:" + om.writeValueAsString(user)); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + } +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample.java new file mode 100644 index 0000000000..85c96b9bc3 --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample.java @@ -0,0 +1,26 @@ +package com.baeldung.optionalReturnType; + +import java.util.Optional; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +public class PersistOptionalTypeExample { + static String persistenceUnit = "com.baeldung.optionalReturnType"; + static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit); + + static EntityManager entityManager = emf.createEntityManager(); + + // to run this app, uncomment the follow line in META-INF/persistence.xml + // com.baeldung.optionalReturnType.UserOptionalField + public static void main(String[] args) { + UserOptionalField user1 = new UserOptionalField(); + user1.setUserId(1l); + user1.setFirstName(Optional.of("Bael Dung")); + entityManager.persist(user1); + + UserOptional user2 = entityManager.find(UserOptional.class, 1l); + System.out.print("User2.firstName:" + user2.getFirstName()); + } +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample2.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample2.java new file mode 100644 index 0000000000..3114e7cefb --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistOptionalTypeExample2.java @@ -0,0 +1,22 @@ +package com.baeldung.optionalReturnType; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +public class PersistOptionalTypeExample2 { + static String persistenceUnit = "com.baeldung.optionalReturnType"; + static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit); + + static EntityManager em = emf.createEntityManager(); + + public static void main(String[] args) { + UserOptional user1 = new UserOptional(); + user1.setUserId(1l); + user1.setFirstName("Bael Dung"); + em.persist(user1); + + UserOptional user2 = em.find(UserOptional.class, 1l); + System.out.print("User2.firstName:" + user2.getFirstName()); + } +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistUserExample.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistUserExample.java new file mode 100644 index 0000000000..f1284958e7 --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/PersistUserExample.java @@ -0,0 +1,22 @@ +package com.baeldung.optionalReturnType; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +public class PersistUserExample { + static String persistenceUnit = "com.baeldung.optionalReturnType"; + static EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnit); + + static EntityManager em = emf.createEntityManager(); + + public static void main(String[] args) { + User user1 = new User(); + user1.setUserId(1l); + user1.setFirstName("Bael Dung"); + em.persist(user1); + + User user2 = em.find(User.class, 1l); + System.out.print("User2.firstName:" + user2.getFirstName()); + } +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/SerializeOptionalTypeExample.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/SerializeOptionalTypeExample.java new file mode 100644 index 0000000000..d67337ad98 --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/SerializeOptionalTypeExample.java @@ -0,0 +1,41 @@ +package com.baeldung.optionalReturnType; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.util.Optional; + +public class SerializeOptionalTypeExample { + public static void main(String[] args) { + User user1 = new User(); + user1.setUserId(1l); + user1.setFirstName("baeldung"); + + serializeObject(user1, "user1.ser"); + + UserOptionalField user2 = new UserOptionalField(); + user2.setUserId(1l); + user2.setFirstName(Optional.of("baeldung")); + + serializeObject(user2, "user2.ser"); + + } + + public static void serializeObject(Object object, String fileName) { + // Serialization + try { + FileOutputStream file = new FileOutputStream(fileName); + ObjectOutputStream out = new ObjectOutputStream(file); + + out.writeObject(object); + + out.close(); + file.close(); + + System.out.println("Object " + object.toString() + " has been serialized to file " + fileName); + + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/User.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/User.java new file mode 100644 index 0000000000..7aa11d78cb --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/User.java @@ -0,0 +1,31 @@ +package com.baeldung.optionalReturnType; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class User implements Serializable { + @Id + private long userId; + + private String firstName; + + public long getUserId() { + return userId; + } + + public void setUserId(long userId) { + this.userId = userId; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/UserOptional.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/UserOptional.java new file mode 100644 index 0000000000..0138a84ab9 --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/UserOptional.java @@ -0,0 +1,35 @@ +package com.baeldung.optionalReturnType; + +import java.io.Serializable; +import java.util.Optional; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class UserOptional implements Serializable { + @Id + private long userId; + + @Column(nullable = true) + private String firstName; + + public long getUserId() { + return userId; + } + + public void setUserId(long userId) { + this.userId = userId; + } + + public Optional getFirstName() { + return Optional.ofNullable(firstName); + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + Optional.ofNullable(firstName); + } + +} diff --git a/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/UserOptionalField.java b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/UserOptionalField.java new file mode 100644 index 0000000000..c02430b1ba --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/java/com/baeldung/optionalReturnType/UserOptionalField.java @@ -0,0 +1,31 @@ +package com.baeldung.optionalReturnType; + +import java.io.Serializable; +import java.util.Optional; + +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class UserOptionalField implements Serializable { + @Id + private long userId; + + private Optional firstName; + + public long getUserId() { + return userId; + } + + public void setUserId(long userId) { + this.userId = userId; + } + + public Optional getFirstName() { + return firstName; + } + + public void setFirstName(Optional firstName) { + this.firstName = firstName; + } +} diff --git a/core-java-modules/core-java-8-2/src/main/resources/META-INF/persistence.xml b/core-java-modules/core-java-8-2/src/main/resources/META-INF/persistence.xml new file mode 100644 index 0000000000..e8cd723ec2 --- /dev/null +++ b/core-java-modules/core-java-8-2/src/main/resources/META-INF/persistence.xml @@ -0,0 +1,34 @@ + + + + + Persist Optional Return Type Demo + org.hibernate.jpa.HibernatePersistenceProvider + com.baeldung.optionalReturnType.User + com.baeldung.optionalReturnType.UserOptional + + true + + + + + + + + + + + + + \ No newline at end of file