java.lang.ClassCastException:java.math.BigInteger无法强制转换为model.APRecord

时间:2015-08-13 18:17:59

标签: java sql hibernate

我的模特课:

    public class APRecord extends AbstractAPRecord implements ARecord {

    private BigInteger autoID;
    private String agentG;
    private AMMRecord mAId;
    private String aId = null;
    //getters and setters

我有一个DAO方法:

    public ScrollableResults getPRecord(StatelessSession statelessSession throws UnsupportedEncodingException{
   Criteria crit = statelessSession.createCriteria(APRecord.class, "apr");
   crit.createAlias("mAId", "aID", Criteria.INNER_JOIN);

   ProjectionList projList = Projections.projectionList();
    projList.add(Projections.property("aID.id"));
    projList.add(Projections.property("pName"));
    projList.add(Projections.property("kNum"));

     crit.setProjection(Projections.distinct(projList));


    return crit.scroll(ScrollMode.FORWARD_ONLY);
}

这会出错:

    java.lang.ClassCastException: java.math.BigInteger cannot be cast to    mil.dod.netops.hbss.aps.model.APRecord 
    at          mil.dod.netops.hbss.aps.persist.hibernate.ApDAOTest.testFilteredPRecords(ApDAOTest.java:347) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at junit.framework.TestCase.runTest(TestCase.java:168) 
    at org.unitils.UnitilsJUnit3.runTest(UnitilsJUnit3.java:111) 
    at junit.framework.TestCase.runBare(TestCase.java:134) 
    at org.unitils.UnitilsJUnit3.runBare(UnitilsJUnit3.java:76) 
    at junit.framework.TestResult$1.protect(TestResult.java:110) 
    at junit.framework.TestResult.runProtected(TestResult.java:128) 
    at junit.framework.TestResult.run(TestResult.java:113) 
    at junit.framework.TestCase.run(TestCase.java:124) 
    at junit.framework.TestSuite.runTest(TestSuite.java:232) 
    at junit.framework.TestSuite.run(TestSuite.java:227) 
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at         org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

我想要可滚动的结果。当我有addOrder时它工作正常,但它没有返回明显的结果,因此是不同的查询。有人可以帮我解决这个错误吗?

更新:我的测试

  ScrollableResults results = amDAO.getPRecords(statelessSession);

    List<APRecord> pList = new ArrayList<APRecord>();
    while (results.next()){
        APRecord aPRecord = (APRecord) results.get(0); //error occurs here
        pList.add(aPRecord);
    }

1 个答案:

答案 0 :(得分:0)

异常是由行

引起的
APRecord aPRecord = (APRecord) results.get(0);

您将结果集当前行的第一个元素转换为APRecord,它不是APRecord,而是BigInteger,如异常所示。这是正常的,因为您的查询由于投影而返回包含三个对象的行:

  1. 与APRecord相关联的AMMRecord的ID
  2. APRecord的pName
  3. APRecord的kNum
  4. 如果查询的目标是返回APRecord的实例,则它不应该有投影。