Source:
<code>public EventBooking add(final String eventId, final Long userId, final Long reserveById, final BookingStatus status, Map<String, String> additionalEventInformation) throws SegueDatabaseException { PreparedStatement pst; if (null == additionalEventInformation) { additionalEventInformation = Maps.newHashMap(); } try (Connection conn = ds.getDatabaseConnection()) { Date creationDate = new Date(); pst = conn.prepareStatement( "INSERT INTO event_bookings (id, user_id, reserved_by, event_id, status, created, updated, additional_booking_information) VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?::text::jsonb)", Statement.RETURN_GENERATED_KEYS); pst.setLong(1, userId); try { pst.setLong(2, reserveById); <START> } catch (NullPointerException e) { pst.setNull(2, Types.INTEGER); <END> } pst.setString(3, eventId); pst.setString(4, status.name()); pst.setTimestamp(5, new java.sql.Timestamp(creationDate.getTime())); pst.setTimestamp(6, new java.sql.Timestamp(creationDate.getTime())); pst.setString(7, objectMapper.writeValueAsString(additionalEventInformation)); if (pst.executeUpdate() == 0) { throw new SegueDatabaseException("Unable to save event booking."); } try (ResultSet generatedKeys = pst.getGeneratedKeys()) { if (generatedKeys.next()) { Long id = generatedKeys.getLong(1); return new PgEventBooking(ds, id, userId, reserveById, eventId, status, creationDate, creationDate, additionalEventInformation); } else { throw new SQLException("Creating event booking failed, no ID obtained."); } } } catch (SQLException e) { throw new SegueDatabaseException("Postgres exception", e); } catch (JsonProcessingException e) { throw new SegueDatabaseException("Unable to convert json to string for persistence.", e); } }</code><technical_language>If a foreign key populated user booking / reservation need nasty catches for null pointers</technical_language>

Target:
public EventBooking add(final String eventId, final Long userId, final Long reserveById, final BookingStatus status, Map<String, String> additionalEventInformation) throws SegueDatabaseException { PreparedStatement pst; if (null == additionalEventInformation) { additionalEventInformation = Maps.newHashMap(); } try (Connection conn = ds.getDatabaseConnection()) { Date creationDate = new Date(); pst = conn.prepareStatement( "INSERT INTO event_bookings (id, user_id, reserved_by, event_id, status, created, updated, additional_booking_information) VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?::text::jsonb)", Statement.RETURN_GENERATED_KEYS); pst.setLong(1, userId); if (reserveById == null) { pst.setNull(2, Types.INTEGER); } else { pst.setLong(2, reserveById); } pst.setString(3, eventId); pst.setString(4, status.name()); pst.setTimestamp(5, new java.sql.Timestamp(creationDate.getTime())); pst.setTimestamp(6, new java.sql.Timestamp(creationDate.getTime())); pst.setString(7, objectMapper.writeValueAsString(additionalEventInformation)); if (pst.executeUpdate() == 0) { throw new SegueDatabaseException("Unable to save event booking."); } try (ResultSet generatedKeys = pst.getGeneratedKeys()) { if (generatedKeys.next()) { Long id = generatedKeys.getLong(1); return new PgEventBooking(ds, id, userId, reserveById, eventId, status, creationDate, creationDate, additionalEventInformation); } else { throw new SQLException("Creating event booking failed, no ID obtained."); } } } catch (SQLException e) { throw new SegueDatabaseException("Postgres exception", e); } catch (JsonProcessingException e) { throw new SegueDatabaseException("Unable to convert json to string for persistence.", e); } }