有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

hibernate中的java对象不会强制转换

我有一个DAO类,使用HQL从数据库中获取数据

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

我正在尝试访问此处返回的对象

@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";

    }

但我得到了这个错误

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

我不知道我为什么会犯这个错误,有人能帮我吗

编辑:

这是我的订票课

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

}

共 (1) 个答案

  1. # 1 楼答案

    您必须将DAO功能更改为:

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

    问题

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

    不会返回List<Booking>,而是List<Object[]>,其中每个Object[]是一个数组,包含[bookingId、customerId、vehicleId、section、isle、row]