首页 > 解决方案 > 错误 - 处理了新的全局异常!Message = java.util.LinkedHashMap 不能转换为 java.math.BigInteger

问题描述

PatientId 的 DataType 是实体对象中的 BigInteger

private BigInteger patientId;

代码:

    @Override
    public List<ChatRoomHistory> getLastChatDetails(List<String> patientIds) {
    String queryStr = "FROM ChatRoomHistory where type = 'NO' and patientId in :patientIds ORDER BY chatCloseTime DESC";
    TypedQuery<ChatRoomHistory> query = sessionFactory.getObject().getCurrentSession().createQuery(queryStr, ChatRoomHistory.class);
    query.setParameter("patientIds", patientIds);
    return query.getResultList();
}

错误

    2020-08-16 19:52:27,939 [http-nio-8080-exec-5:] c.t.c.u.e.GlobalExceptionHandler.handleException:243
                ERROR - new Global exception handled!    Message = java.util.LinkedHashMap cannot be cast to java.math.BigInteger
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.math.BigInteger
    at org.hibernate.type.descriptor.java.BigIntegerTypeDescriptor.unwrap(BigIntegerTypeDescriptor.java:19)
    at org.hibernate.type.descriptor.sql.DecimalTypeDescriptor$1.doBind(DecimalTypeDescriptor.java:47)
    at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:74)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280)

标签: spring-boothibernatespring-data-jpahqljpql

解决方案


您可以通过以下方式更正您的方法:

@Override
public List<ChatRoomHistory> getLastChatDetails(List<BigInteger> patientIds)
{
    String hql = "select c from ChatRoomHistory c where c.type = 'NO' and c.patientId in :patientIds ORDER BY c.chatCloseTime DESC";
    TypedQuery<ChatRoomHistory> query = sessionFactory.getCurrentSession().createQuery(hql, ChatRoomHistory.class);
    query.setParameter("patientIds", patientIds);
    return query.getResultList();
}

注释:

  1. 尽管 HQL 不需要存在 a select_clause,但通常最好包含一个。

  2. 值列表不能为空;它必须至少包含一个值。(见此


推荐阅读