You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/08/26 11:14:46 UTC

[GitHub] [shardingsphere] irwinai opened a new issue, #20568: OffsetDateTime type in model got ClassCastException

irwinai opened a new issue, #20568:
URL: https://github.com/apache/shardingsphere/issues/20568

   ### Which version of ShardingSphere did you use?
   
   5.1.2
   
   ### Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?
   
   ShardingSphere-JDBC
   
   ### Expected behavior
   
   when I use the OffsetDateTime in model, I hope that query can works
   
   ```java
   public class Order {
       private Long orderId;
       private Long userId;
       private Integer status;
       private OffsetDateTime createTime;
   }
   ```
   
   ### Actual behavior
   
   Got exception:
   
   ```java
   Caused by: java.lang.ClassCastException: class java.sql.Timestamp cannot be cast to class java.time.OffsetDateTime (java.sql.Timestamp is in module java.sql of loader 'platform'; java.time.OffsetDateTime is in module java.base of loader 'bootstrap')
   	at org.apache.ibatis.type.OffsetDateTimeTypeHandler.getNullableResult(OffsetDateTimeTypeHandler.java:38)
   	at org.apache.ibatis.type.OffsetDateTimeTypeHandler.getNullableResult(OffsetDateTimeTypeHandler.java:28)
   	at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:85)
   	... 99 more
   ```
   
   ### Reason analyze (If you can)
   
   ShardingSphereResultSet handle the type
   
   ```java
   @Override
       public <T> T getObject(final int columnIndex, final Class<T> type) throws SQLException {
           if (BigInteger.class.equals(type)) {
               return (T) BigInteger.valueOf(getLong(columnIndex));
           } else if (Blob.class.equals(type)) {
               return (T) getBlob(columnIndex);
           } else if (Clob.class.equals(type)) {
               return (T) getClob(columnIndex);
           } else if (LocalDateTime.class.equals(type) || LocalDate.class.equals(type) || LocalTime.class.equals(type) || OffsetDateTime.class.equals(type)) { // to handle the OffsetDateTime type
               return (T) ResultSetUtil.convertValue(mergeResultSet.getValue(columnIndex, Timestamp.class), type);
           } else {
               return (T) ResultSetUtil.convertValue(mergeResultSet.getValue(columnIndex, type), type);
           }
       }
   ```
   
   ResultSetUtil also do this
   
   ```java
     private static Object convertTimestampValue(final Object value, final Class<?> convertType) {
           Timestamp timestamp = (Timestamp) value;
           if (LocalDateTime.class.equals(convertType)) {
               return timestamp.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
           }
           if (LocalDate.class.equals(convertType)) {
               return timestamp.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
           }
           if (LocalTime.class.equals(convertType)) {
               return timestamp.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
           }
          // need to handle the OffsetDateTime
           if (OffsetDateTime.class.equals(convertType)) {
               return timestamp.toInstant().atZone(ZoneId.systemDefault()).toOffsetDateTime();
           }
           return value;
       }
   ```
   
   ### Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.
   
   1. define your model with date type OffsetDateTime
   
      ```java
      public class Order {
          private Long orderId;
      
          private Long userId;
      
          private Integer status;
      
          private OffsetDateTime createTime;
      }
      ```
   
   2. just use mybatis and execute query like:`select * from order where order_id=xx`
   
   3. then you will be got the exception
   
      
   
   ### Example codes for reproduce this issue (such as a github link).
   
   I had fixed the bug in my project.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [shardingsphere] strongduanmu closed issue #20568: OffsetDateTime type in model got ClassCastException

Posted by GitBox <gi...@apache.org>.
strongduanmu closed issue #20568: OffsetDateTime type in model got ClassCastException
URL: https://github.com/apache/shardingsphere/issues/20568


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org