0

My model class:

    public class APRecord extends AbstractAPRecord implements ARecord {

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

I have a DAO method:

    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);
}

This gives an error:

    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) 

I want scrollable Results back. It worked fine when I had addOrder but it didn't return me distinct results, hence the distinct query. Can someone help me fix this error?

Update: My test

  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);
    }
8
  • Why do you want to add property "aID.id" instead of just "aID"? I guess it will solve your problem. Commented Aug 13, 2015 at 18:22
  • Can you try with replacing BigInteger with Long? Commented Aug 13, 2015 at 18:38
  • @Pipes there's an inner join. id is in the class AMMRecord. That's not a solution. Commented Aug 13, 2015 at 18:38
  • @sunrise76 Still an error. Commented Aug 13, 2015 at 18:39
  • Show us the code causing the exception: ApDAOTest.java Commented Aug 13, 2015 at 19:03

1 Answer 1

0

The exception is caused by the line

APRecord aPRecord = (APRecord) results.get(0);

You're casting the first element of the current row of the resultset to APRecord, and it's not an APRecord, but a BigInteger, as the exception shows. And it's normal, since your query, due to the projections, returns rows containing three objects:

  1. the ID of the AMMRecord associated with the APRecord
  2. the pName of the the APRecord
  3. the kNum of the APRecord

If the goal of the query is to return instances of APRecord, it should not have projections.

Sign up to request clarification or add additional context in comments.

6 Comments

Before my DAO method had crit.addOrder( Order.asc("pName") );crit.addOrder( Order.asc("kNum") ); but it returned duplicated records. So I did a projection for which the query works in sql. Is there any way to get the actual scrollable distinct results without using projection?
If the entity only contains the fields you're showing us, then the query shouldn't return duplicate records. But whatever. You can use projections, but just realize that what you'll get is not instances of APRecord.
the query returns duplicate for the id. that's why I used projections, but I need actual scrollable results. I'm not sure how to go by doing that.
It's simple: results.get(0) is the AMMRecord ID, result.get(1) is the pName, results.get(2) is the kNum. What's the problem?
I did a return crit.setResultTransformer(Transformers.aliasToBean(APRecord.class)).scroll(ScrollMode.FORWARD_ONLY); for which my test returns null.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.