Organiccat
Organiccat

Reputation: 5651

Generic object returned instead of class entity in hibernate query

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.

Upvotes: 1

Views: 2941

Answers (2)

Bozho
Bozho

Reputation: 597382

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

Upvotes: 1

matt b
matt b

Reputation: 140061

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.

Upvotes: 4

Related Questions