Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8053946

JDBC should support writing/reading java.util.Optional to/from a database.

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P5 P5
    • None
    • 8u11
    • core-libs
    • x86
    • other

      A DESCRIPTION OF THE REQUEST :
      Nullable columns are a common use case when reading or writing data from or to a database. But on the object level nullable attributes are often considered bad smells. Java 8 introduces java.util.Optional to represent values which might not be present at runtime and help to avoid null references in your code.
      When inserting an optional value into a database, the user currently has to provide the mapping from Optional.of(<value>) to <value> and from Optional.empty() to null by herself (and vice versa for reading an Optional from a database). While this is not very hard to accomplish for a single Optional value it tends to get quite clumsy for more complex data hierarchies.

      JUSTIFICATION :
      Optional is an essential data structure for sanitising objects by removing nullable fields from them and should be supported by JDBC without any help from the user.

      ACTUAL -
      A java.io.NotSerializableException is thrown when trying to insert an Optional values into a database.

      CUSTOMER SUBMITTED WORKAROUND :
      When using JDBI on top of JDBC, the Dropwizard packages provide automatic mappings for the Optional data type of Google's Guava library. The same techniques can be used to make java.util.Optional work with JDBI, though it currently is more convenient to use Guava's Optional in the first place.

            lancea Lance Andersen
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: