Java中反对类强制转换异常

Java中反对类强制转换异常,java,hibernate,intellij-idea,Java,Hibernate,Intellij Idea,我有个问题让我有点发疯。下面是Java方法 public List<FtpActiveMerchantDTO> getFtpActiveMerchants() { String sql = "select m.merchantId, ma.merchantAcctId, m.domain, f.fetchUrl, ma.acctActive, " + "f.fieldDelimiter, f.feedType " + "fro

我有个问题让我有点发疯。下面是Java方法

public List<FtpActiveMerchantDTO> getFtpActiveMerchants() {

    String sql = "select m.merchantId, ma.merchantAcctId, m.domain, f.fetchUrl, ma.acctActive, " +
            "f.fieldDelimiter, f.feedType " +
            "from merchant_account ma " +
            "join merchant_ftp_account f on f.merchantAcctId = ma.merchantAcctId " +
            "join merchant m on m.merchantAcctId = ma.merchantAcctId " +
            "where f.fetchUrl is not null and ma.acctActive = 1";

    Query query = currentSession().createSQLQuery(sql);

    List<FtpActiveMerchantDTO> ftpActiveMerchantDTOList = new ArrayList<FtpActiveMerchantDTO>();
    int merchantId, merchantAcctId;
    byte acctActive;
    for (Object rowObject : query.list()) {
        Object[] row = (Object []) rowObject;
        merchantId = ((BigDecimal) row[0]).intValue();
        merchantAcctId = ((BigDecimal) row[1]).intValue();
        acctActive = ((BigDecimal) row[4]).byteValue();
        ftpActiveMerchantDTOList.add(new FtpActiveMerchantDTOBuilder().withMerchantId(merchantId)
                .withMerchantAcctId(merchantAcctId).withDomain((String) row[2])
                .withFetchUrl((String) row[3]).withAcctActive(acctActive > 0)
                .withFieldDelimiter(row[5].toString()).withFeedType((String) row[6]).build());
    }

    return ftpActiveMerchantDTOList;
}
错误发生在分配了
acctActive
的行。当我将该行修改为:

acctActive = (Byte) row[4];
然后,服务按预期工作。但是我的集成测试(从IntelliJ内部运行)

失败,出现以下错误:

java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Byte
at com.pronto.mpds.dal.MerchantDAOImpl.getFtpActiveMerchants(MerchantDAOImpl.java:143)
at com.pronto.mpds.dal.MerchantDAOIT.whenFetchingFtpActiveMerchants(MerchantDAOIT.java:96)
at com.pronto.mpds.dal.MerchantDAOIT.testFtpActiveMerchants(MerchantDAOIT.java:44)
...

db表中的字段是一个tinyint(4)。为什么db查询的结果“期望”为BigDecimal?是否存在某种默认数据类型?我知道我没有在任何地方配置数据库。

乍一看,集成测试的数据库架构与生产数据库架构不同,因此类型不匹配。

在集成测试中是否使用相同的数据库?如果是,则看起来它们有不同的模式。您需要使用标量来显式指定类型。如果这听起来很愚蠢,很抱歉,但是您是否100%确定您的单元测试没有以某种方式使用旧代码(例如,类路径中的旧jar)?@Nambari-您会在代码中的何处执行此操作?这发现了两个数据库之间的其他一些不一致,这并不奇怪,因此需要一段时间来解决,但这是问题的原因,修复数据库不一致解决了问题。集成测试的Yay:)
private void whenFetchingFtpActiveMerchants() {
    openAndBindSession();
    ftpActiveMerchantDTOList = merchantDAO.getFtpActiveMerchants();
    flushAndCloseSession();
}
java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Byte
at com.pronto.mpds.dal.MerchantDAOImpl.getFtpActiveMerchants(MerchantDAOImpl.java:143)
at com.pronto.mpds.dal.MerchantDAOIT.whenFetchingFtpActiveMerchants(MerchantDAOIT.java:96)
at com.pronto.mpds.dal.MerchantDAOIT.testFtpActiveMerchants(MerchantDAOIT.java:44)
...