1

I am running a hibernate query which is returning an Object instead of the correct entity object. The statement is as follows:

public List getPriorBids(String rfpCD, String carrierCode) {
    String sqlString =  " SELECT inland_cd, " +
                                "rfp_cd, " +
                                "carrier_cd, " +
                                "port_cd, " +
                                "max(iteration_nb) as iteration_nb " +
                        " FROM care.inland_bid " +
                        " WHERE rfp_cd = :rfpCode " +
                        "   AND carrier_cd = :carrierCode " +
                        " GROUP BY inland_cd, rfp_cd, carrier_cd, port_cd " +
                        " ORDER BY inland_cd, carrier_cd"; 
    SQLQuery sqlQuery = session.createSQLQuery(sqlString);
    sqlQuery.setString("carrierCode",carrierCode);
    sqlQuery.setString("rfpCode", rfpCD);
    sqlQuery.addEntity(Bid.class);
    List bids = sqlQuery.list();
    return bids;
   }

Clearly I am trying to store it as a Bid object. I have another object as well which I should be storing it as but I get an error that the column name cannot be found, despite double and triple checking column names to make sure they match.

0

2 Answers 2

4

Query.list() returns a raw List object, not a generic List.

You should just be able to cast it to List<Bid>, and ignore the unsafe cast warning.

I think you are confusing a method which returns a raw List with the actual run-time type of the List elements being Object. Just because the compile-time signature of List.get(i) returns an Object does not mean that the actual type of the element returned is Object at run-time.

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

1 Comment

I thought addEntity(class) changed the return type of the object to the specified class type if it was able to fit. I have it returning a list of objects (took out the addEntity) but casting them into List<Bid> later doesn't appear to change the object (at least as far as the debugger shows).
1

If using JPA 2, you can create a TypedQuery which supports generics

Comments

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.