title: Using JAVA to save data to Oracle table with TIMESTAMP WITH ZONED TIME column
body: <p>I am trying to find a Java Time Type that can be persisted to a column of type TIMESTAMP WITH TIME ZONE in the Oracle DB. The Java type needs to have the Time Zone as part of it. Because of the way the application was written, the Java Calendar type was used (also, the Java Calendar type has Time Zone that is part of it)</p>
  
  <p><strong>TRIAL ONE:</strong></p>
  
  <p>I did try to use the Calendar directly (with no serializer) with this code:</p>
  
  <p>dateStr was in a pattern of: <code>"MM-dd-yyyy hh:mm:ss a"</code>
  ZoneLoc was for the timezone: ex: <code>America/Chicago</code>, <code>US/Eastern</code>, etc.</p>
  
  <pre><code>public Calendar convDateStrWithZoneTOCalendar(String dateStr,
          String ZoneLoc) throws Exception {
  
      String string = dateStr;
      String pattern = this.getPattern();
      Date date = new SimpleDateFormat(pattern).parse(string);        
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      TimeZone tz = TimeZone.getTimeZone(ZoneLoc);
      calendar.setTimeZone( tz );
      return calendar;
  
  }
  </code></pre>
  
  <p>when using this, the correct Zone Location was not set. For example, I would try to set ZoneLoc to US/Hawaii and when data was saved to the DB, it would be something like
  23-SEP-19 10.03.11.000000 AM -05:00   And (-05:00) does not represent US/Hawaii</p>
  
  <p><strong>TRIAL TWO</strong></p>
  
  <p>It is for this reason why the attribute-conversion route was tried. In this case, there is an attempt to convert Calendar to ZoneDateTime. </p>
  
  <p>I had seen this in another message noting: </p>
  
  <blockquote>
    <p>Add @Convert(converter=OracleZonedDateTimeSeriliazer.class) on top of
    your createdOn attribute on ur Entity class.</p>
  </blockquote>
  
  <p>But, as mentioned above, Calendar is being used as part of the entity and not ZonedDateTime</p>
  
  <p><strong>the way the entity class is defined now. Before (when using the ( convDateStrWithZoneTOCalendar above), the "@Convert" items were not present</strong></p>
  
  <pre><code>@Entity
  @Table(name = "LAWNCORRJOB", schema = "ORAAPPS", uniqueConstraints = @UniqueConstraint(columnNames = {
          "TENANTID", "GENERATEDJOBNO" }))
  public class Lawncorrjob implements java.io.Serializable {
  
  
      private static final long serialVersionUID = -8207025830028821136L;
  
      ....
  
      @Convert( converter = OracleCalendarZoneDateTimeSerializer.class, disableConversion = false )
      private Calendar schedfirstdayts;
  
      @Convert( converter = OracleCalendarZoneDateTimeSerializer.class, disableConversion = false )
      private Calendar schedlastdayts;
  
      ...
  }
  </code></pre>
  
  <p><strong>the converter being used: converts Calendar to ZonedDateTime (under Oracle)</strong></p>
  
  <pre><code>@Converter(autoApply = true)
  public class OracleCalendarZoneDateTimeSerializer implements
          AttributeConverter&lt;Calendar, ZonedDateTime&gt; {
  
      @Override
      public ZonedDateTime convertToDatabaseColumn(Calendar attribute) {
  
          if (attribute == null)
              return null;
  
          // get the timezone
          TimeZone tz = attribute.getTimeZone();
  
          // use the timezone to specify the area location
          // ex: For example, TimeZone.getTimeZone("GMT-8").getID() returns
          // "GMT-08:00".
          ZonedDateTime xzdt = ZonedDateTime.of(attribute.get(Calendar.YEAR),
                  attribute.get(Calendar.MONTH),
                  attribute.get(Calendar.DAY_OF_MONTH) + 1,
                  attribute.get(Calendar.HOUR_OF_DAY),
                  attribute.get(Calendar.MINUTE), attribute.get(Calendar.SECOND),
                  attribute.get(Calendar.MILLISECOND), ZoneId.of(tz.getID()));
  
          return xzdt;
      }
  
      @Override
      public Calendar convertToEntityAttribute(ZonedDateTime dbData) {
  
          GregorianCalendar newcal = DateTimeUtils.toGregorianCalendar(dbData);
  
          return newcal;
      }
  
  }
  </code></pre>
  
  <p>But, when executing this code, I get an error:</p>
  
  <blockquote>
    <p>Error Message: Could not commit JPA transaction; nested exception is
    javax.persistence.RollbackException: Error while committing the
    transaction CAUSE : javax.persistence.RollbackException: Error while
    committing the transaction",</p>
  </blockquote>
  
  <p>So, is there any way I can fix either option to get this all working?</p>
  
  <p>Any help, hints or advice is appreciated.</p>
  
  <p>TIA</p>