1

I have a DAO class where I use HQL to get data from my database

public List<Booking> getInventory(){
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        Query query = session.createQuery("select booking.bookingId, booking.customerId, booking.vehicleId, booking.section, booking.isle, booking.row from Booking booking");

        List<Booking> invList = (List<Booking>) query.getResultList();

        return invList;
    }

I am trying to access the objects that are being returned here

@RequestMapping("/")
    public String home(Model model){
        DAO dao = new DAO();
        List<Booking> invList = (List<Booking>) dao.getInventory();

        for(Booking booking : invList){
            System.out.println(booking.getBookingId());
        }
        return "home";

    }

But I am getting this error

Request processing failed; nested exception is java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.algonquinstorage.beans.Booking

I have no clue why I am getting this error, can someone help me out?

EDIT:

This is my booking class

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Booking implements Serializable {

    @Id
    int bookingId;
    int vehicleId;
    int customerId;
    String section;
    int isle;
    int row;

    public int getBookingId() {
        return bookingId;
    }

    public void setBookingId(int bookingId) {
        this.bookingId = bookingId;
    }

    public int getVehicleId() {
        return vehicleId;
    }

    public void setVehicleId(int vehicleId) {
        this.vehicleId = vehicleId;
    }

    public int getCustomerId() {
        return customerId;
    }

    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public int getIsle() {
        return isle;
    }

    public void setIsle(int isle) {
        this.isle = isle;
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public Booking() {
        super();
    }

    public Booking(int bookingId, int customerId, int vehicleId, String section, int isle, int row) {
        super();
        this.bookingId = bookingId;
        this.customerId = customerId;
        this.vehicleId = vehicleId;
        this.section = section;
        this.isle = isle;
        this.row = row;
    }

}
Craig
  • 364
  • 1
  • 3
  • 25
  • Possible duplicate of http://stackoverflow.com/questions/7952064/mapping-list-from-jpa-query-getresultlist-to-a-customized-to – digidude Sep 20 '16 at 21:39
  • as explained in the link above, you can make the HQL return a List using an AliasToBeanResultTransformer – digidude Sep 20 '16 at 21:44
  • I tried this. It got rid of my error but I am getting the wrong results. – Craig Sep 20 '16 at 22:32

1 Answers1

1

You have to change your DAO function to:

public List<Booking> getInventory(){
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    Query query = session.createQuery("select booking from Booking booking");

    List<Booking> invList = (List<Booking>) query.getResultList();

    return invList;
}

The query

select booking.bookingId, booking.customerId, booking.vehicleId, booking.section, booking.isle, booking.row from Booking booking

won't return List<Booking>, but List<Object[]>, where each Object[] is an array containing [bookingId, customerId, vehicleId, section, isle, row]

mateuszlo
  • 1,309
  • 1
  • 11
  • 10
  • It worked but printed out 0, 0. When I look at my tables in MySQL Workbench I can see that the values that should be printing are 1 and . So I am not getting the error but it didn't print the proper results. – Craig Sep 20 '16 at 21:52
  • You have to provide some additional info. How your Booking class looks like? – mateuszlo Sep 20 '16 at 21:56
  • Booking class looks fine. Can you check what your original query actually returned - knowing that its returning type is `List` – mateuszlo Sep 20 '16 at 22:09
  • How can I check? It looks like it returns a booking object. – Craig Sep 20 '16 at 22:15
  • I mean go back to `select booking.bookingId, booking.customerId ...` query, cast result to list of Object[] - `List invList = (List) query.getResultList();` and print results. – mateuszlo Sep 20 '16 at 22:19
  • See if ids are null there. – mateuszlo Sep 20 '16 at 22:19
  • I cannot print anything from the results.I just did toString and this is what I am getting [Ljava.lang.Object;@313c852e [Ljava.lang.Object;@6460a264 – Craig Sep 20 '16 at 22:29
  • Try printing results with this `invList.forEach(x -> System.out.println(Arrays.toString(x)));` – mateuszlo Sep 20 '16 at 22:34
  • Close. I got [0, 0, 0, A, 1, 1] [0, 0, 0, B, 1, 1] when it should be [1, 1, 1, A, 1, 1] [2, 2, 2, B, 1, 1] – Craig Sep 20 '16 at 22:40
  • That looks quite strange. I have 2 more questions. How your database table looks like - what are column names. And how did you insert data to this databse - thorugh hibernate or just plain sql directly on database? – mateuszlo Sep 20 '16 at 22:51
  • I am thinking that it has to do with the database. Do the columns have to be the same name as the attributes as the class? Because the section isle and row are the same as the column names and they work. – Craig Sep 20 '16 at 23:11
  • That was the issue, it's working now. thank you for your help. – Craig Sep 20 '16 at 23:37