gpt4 book ai didi

java - hibernate 中的对象不会转换

转载 作者:行者123 更新时间:2023-11-30 07:08:38 27 4
gpt4 key购买 nike

我有一个 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;
}

}

最佳答案

您必须将 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]的数组

关于java - hibernate 中的对象不会转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39604095/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com