Eclipselink无法转换oracle.sql.TIMESTAMPTZ

时间:2019-07-10 13:25:17

标签: eclipselink

当我使用Eclipselink作为持久性提供程序从数据库中获取值时,出现错误。 它没有将oracle.sql.TIMESTAMPTZ转换为java.sql.Timestamp或java.util.Date


Query q = em.createNativeQuery("SELECT * FROM MY_Schema.MyTable MT WHERE MT.START_DT = (SELECT MAX(START_DT) FROM MY_Schema.MyTable)",MyTable.class);

@Entity
@Table(name = "MyTable", schema = "MY_Schema")
public class MyTable implements Serializable {
    @EmbeddedId
    private MyTableId id;

@Embeddable
public class MyTableId implements Serializable {

  @Temporal(TemporalType.TIMESTAMP)
  @Column(name = "END_DT")
  private Calendar endTime;

  @Temporal(TemporalType.TIMESTAMP)
  @Column(name = "START_DT")
  private Calendar startTime;

例外

  

原因:异常[EclipseLink-3001](Eclipse Persistence Services- 2.6.8.WAS-v20181218-0accd7f):org.eclipse.persistence.exceptions.ConversionException异常描述:对象[oracle.sql.TIMESTAMPTZ@6156ebf7]无法将类别[class oracle.sql.TIMESTAMPTZ]的转换为[class java.sql.Timestamp]。 org.eclipse.persistence.internal处的org.eclipse.persistence.exceptions.ConversionException.couldNotBeConverted(ConversionException.java:78)处的org.eclipse.persistence.internal.helper.ConversionManager.convertObjectToTimest mp(ConversionManager.java:751)处的mp。 helper.ConversionManager.convertObject(ConversionManager.java:112)



我发现的东西

  

在调试时,我在eclipselink ConversionManager类中发现了这一点   oracle.sql.TIMESTAMPTZ没有任何处理。直接在此方法中引发异常。



/**
     * INTERNAL:
     * Build a valid instance of java.sql.Timestamp from the given source object.
     * @param sourceObject    Valid object of class java.sql.Timestamp, String, java.util.Date, or Long
     */
    protected java.sql.Timestamp convertObjectToTimestamp(Object sourceObject) throws ConversionException {
        java.sql.Timestamp timestamp = null;

        if (sourceObject instanceof java.sql.Timestamp) {
            return (java.sql.Timestamp)sourceObject;// Helper timestamp is not caught on class check.
        }

        if (sourceObject instanceof String) {
            timestamp = Helper.timestampFromString((String)sourceObject);
        } else if (sourceObject instanceof java.util.Date) {// This handles all date and subclasses, sql.Date, sql.Time conversions.
            timestamp = Helper.timestampFromDate((java.util.Date)sourceObject);
        } else if (sourceObject instanceof Calendar) {
            return Helper.timestampFromCalendar((Calendar)sourceObject);
        } else if (sourceObject instanceof Long) {
            timestamp = Helper.timestampFromLong((Long)sourceObject);
        } else {
            throw ConversionException.couldNotBeConverted(sourceObject, ClassConstants.TIMESTAMP);
        }
        return timestamp;
    }

1 个答案:

答案 0 :(得分:0)

oracle.sql.TIMESTAMPTZ处理是特定于数据库的,由Oracle9Platform的子类完成。确保使用'target-database'持久性属性指定了与数据库匹配的正确目标数据库平台类