File tree Expand file tree Collapse file tree
core-java-modules/core-java-8-2
java/com/baeldung/optionalReturnType Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2222 <maven .compiler.source>1.8</maven .compiler.source>
2323 <maven .compiler.target>1.8</maven .compiler.target>
2424 <icu .version>64.2</icu .version>
25+ <hibernate .core.version>5.4.0.Final</hibernate .core.version>
26+ <h2database .version>1.4.197</h2database .version>
27+ <jackson .databind.version>2.9.8</jackson .databind.version>
2528 </properties >
2629
2730 <dependencies >
3033 <artifactId >icu4j</artifactId >
3134 <version >${icu.version} </version >
3235 </dependency >
33-
36+ <dependency >
37+ <groupId >org.hibernate</groupId >
38+ <artifactId >hibernate-core</artifactId >
39+ <version >${hibernate.core.version} </version >
40+ </dependency >
41+ <dependency >
42+ <groupId >com.h2database</groupId >
43+ <artifactId >h2</artifactId >
44+ <version >${h2database.version} </version >
45+ </dependency >
46+ <dependency >
47+ <groupId >com.fasterxml.jackson.core</groupId >
48+ <artifactId >jackson-databind</artifactId >
49+ <version >${jackson.databind.version} </version >
50+ </dependency >
3451 </dependencies >
3552
3653 <build >
Original file line number Diff line number Diff line change 1+ package com .baeldung .optionalReturnType ;
2+
3+ import java .util .HashMap ;
4+ import java .util .Map ;
5+ import java .util .Optional ;
6+
7+ public class HandleOptionalTypeExample {
8+ static Map <String , User > usersByName = new HashMap ();
9+ static {
10+ User user1 = new User ();
11+ user1 .setUserId (1l );
12+ user1 .setFirstName ("baeldung" );
13+ usersByName .put ("baeldung" , user1 );
14+ }
15+
16+ public static void main (String [] args ) {
17+ changeUserName ("baeldung" , "baeldung-new" );
18+ changeUserName ("user" , "user-new" );
19+ }
20+
21+ public static void changeUserName (String oldFirstName , String newFirstName ) {
22+ Optional <User > userOpt = findUserByName (oldFirstName );
23+ if (userOpt .isPresent ()) {
24+ User user = userOpt .get ();
25+ user .setFirstName (newFirstName );
26+
27+ System .out .println ("user with name " + oldFirstName + " is changed to " + user .getFirstName ());
28+ } else {
29+ // user is missing
30+ System .out .println ("user with name " + oldFirstName + " is not found." );
31+ }
32+ }
33+
34+ public static Optional <User > findUserByName (String name ) {
35+ // look up the user in the database, the user object below could be null
36+ User user = usersByName .get (name );
37+ Optional <User > opt = Optional .ofNullable (user );
38+
39+ return opt ;
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .optionalReturnType ;
2+
3+ import com .fasterxml .jackson .core .JsonProcessingException ;
4+ import com .fasterxml .jackson .databind .ObjectMapper ;
5+
6+ public class OptionalToJsonExample {
7+ public static void main (String [] args ) {
8+ UserOptional user = new UserOptional ();
9+ user .setUserId (1l );
10+ user .setFirstName ("Bael Dung" );
11+
12+ ObjectMapper om = new ObjectMapper ();
13+ try {
14+ System .out .print ("user in json is:" + om .writeValueAsString (user ));
15+ } catch (JsonProcessingException e ) {
16+ e .printStackTrace ();
17+ }
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .optionalReturnType ;
2+
3+ import java .util .Optional ;
4+
5+ import javax .persistence .EntityManager ;
6+ import javax .persistence .EntityManagerFactory ;
7+ import javax .persistence .Persistence ;
8+
9+ public class PersistOptionalTypeExample {
10+ static String persistenceUnit = "com.baeldung.optionalReturnType" ;
11+ static EntityManagerFactory emf = Persistence .createEntityManagerFactory (persistenceUnit );
12+
13+ static EntityManager entityManager = emf .createEntityManager ();
14+
15+ // to run this app, uncomment the follow line in META-INF/persistence.xml
16+ // <class>com.baeldung.optionalReturnType.UserOptionalField</class>
17+ public static void main (String [] args ) {
18+ UserOptionalField user1 = new UserOptionalField ();
19+ user1 .setUserId (1l );
20+ user1 .setFirstName (Optional .of ("Bael Dung" ));
21+ entityManager .persist (user1 );
22+
23+ UserOptional user2 = entityManager .find (UserOptional .class , 1l );
24+ System .out .print ("User2.firstName:" + user2 .getFirstName ());
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .optionalReturnType ;
2+
3+ import javax .persistence .EntityManager ;
4+ import javax .persistence .EntityManagerFactory ;
5+ import javax .persistence .Persistence ;
6+
7+ public class PersistOptionalTypeExample2 {
8+ static String persistenceUnit = "com.baeldung.optionalReturnType" ;
9+ static EntityManagerFactory emf = Persistence .createEntityManagerFactory (persistenceUnit );
10+
11+ static EntityManager em = emf .createEntityManager ();
12+
13+ public static void main (String [] args ) {
14+ UserOptional user1 = new UserOptional ();
15+ user1 .setUserId (1l );
16+ user1 .setFirstName ("Bael Dung" );
17+ em .persist (user1 );
18+
19+ UserOptional user2 = em .find (UserOptional .class , 1l );
20+ System .out .print ("User2.firstName:" + user2 .getFirstName ());
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .optionalReturnType ;
2+
3+ import javax .persistence .EntityManager ;
4+ import javax .persistence .EntityManagerFactory ;
5+ import javax .persistence .Persistence ;
6+
7+ public class PersistUserExample {
8+ static String persistenceUnit = "com.baeldung.optionalReturnType" ;
9+ static EntityManagerFactory emf = Persistence .createEntityManagerFactory (persistenceUnit );
10+
11+ static EntityManager em = emf .createEntityManager ();
12+
13+ public static void main (String [] args ) {
14+ User user1 = new User ();
15+ user1 .setUserId (1l );
16+ user1 .setFirstName ("Bael Dung" );
17+ em .persist (user1 );
18+
19+ User user2 = em .find (User .class , 1l );
20+ System .out .print ("User2.firstName:" + user2 .getFirstName ());
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .optionalReturnType ;
2+
3+ import java .io .FileOutputStream ;
4+ import java .io .IOException ;
5+ import java .io .ObjectOutputStream ;
6+ import java .util .Optional ;
7+
8+ public class SerializeOptionalTypeExample {
9+ public static void main (String [] args ) {
10+ User user1 = new User ();
11+ user1 .setUserId (1l );
12+ user1 .setFirstName ("baeldung" );
13+
14+ serializeObject (user1 , "user1.ser" );
15+
16+ UserOptionalField user2 = new UserOptionalField ();
17+ user2 .setUserId (1l );
18+ user2 .setFirstName (Optional .of ("baeldung" ));
19+
20+ serializeObject (user2 , "user2.ser" );
21+
22+ }
23+
24+ public static void serializeObject (Object object , String fileName ) {
25+ // Serialization
26+ try {
27+ FileOutputStream file = new FileOutputStream (fileName );
28+ ObjectOutputStream out = new ObjectOutputStream (file );
29+
30+ out .writeObject (object );
31+
32+ out .close ();
33+ file .close ();
34+
35+ System .out .println ("Object " + object .toString () + " has been serialized to file " + fileName );
36+
37+ } catch (IOException e ) {
38+ e .printStackTrace ();
39+ }
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .optionalReturnType ;
2+
3+ import java .io .Serializable ;
4+
5+ import javax .persistence .Entity ;
6+ import javax .persistence .Id ;
7+
8+ @ Entity
9+ public class User implements Serializable {
10+ @ Id
11+ private long userId ;
12+
13+ private String firstName ;
14+
15+ public long getUserId () {
16+ return userId ;
17+ }
18+
19+ public void setUserId (long userId ) {
20+ this .userId = userId ;
21+ }
22+
23+ public String getFirstName () {
24+ return firstName ;
25+ }
26+
27+ public void setFirstName (String firstName ) {
28+ this .firstName = firstName ;
29+ }
30+
31+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .optionalReturnType ;
2+
3+ import java .io .Serializable ;
4+ import java .util .Optional ;
5+
6+ import javax .persistence .Column ;
7+ import javax .persistence .Entity ;
8+ import javax .persistence .Id ;
9+
10+ @ Entity
11+ public class UserOptional implements Serializable {
12+ @ Id
13+ private long userId ;
14+
15+ @ Column (nullable = true )
16+ private String firstName ;
17+
18+ public long getUserId () {
19+ return userId ;
20+ }
21+
22+ public void setUserId (long userId ) {
23+ this .userId = userId ;
24+ }
25+
26+ public Optional <String > getFirstName () {
27+ return Optional .ofNullable (firstName );
28+ }
29+
30+ public void setFirstName (String firstName ) {
31+ this .firstName = firstName ;
32+ Optional .ofNullable (firstName );
33+ }
34+
35+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .optionalReturnType ;
2+
3+ import java .io .Serializable ;
4+ import java .util .Optional ;
5+
6+ import javax .persistence .Entity ;
7+ import javax .persistence .Id ;
8+
9+ @ Entity
10+ public class UserOptionalField implements Serializable {
11+ @ Id
12+ private long userId ;
13+
14+ private Optional <String > firstName ;
15+
16+ public long getUserId () {
17+ return userId ;
18+ }
19+
20+ public void setUserId (long userId ) {
21+ this .userId = userId ;
22+ }
23+
24+ public Optional <String > getFirstName () {
25+ return firstName ;
26+ }
27+
28+ public void setFirstName (Optional <String > firstName ) {
29+ this .firstName = firstName ;
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments