【问题标题】:ClassCastException while retrieving list from database using Hibernate使用 Hibernate 从数据库中检索列表时发生 ClassCastException
【发布时间】:2019-01-14 08:52:35
【问题描述】:
@Id
@Column(name="Item", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int itemId;

@Column(name="ItemName")
private String itemName;

@Column(name="ItemPrice")
private double itemPrice;

@Column(name="status")
private String status;

@Column(name="image")
private String image;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "RestaurantId", nullable = false)
private Restaurant restaurant;

这是我的实体类,

public List<FoodItem> getFoodItems(Restaurant restaurant) {
    Session session=getSession();
    List<FoodItem> list=null;
    NativeQuery<?> query = session.createNativeQuery("SELECT " + 
            "   \"Item\"," + 
            "   \"ItemName\"," + 
            "   \"ItemPrice\"," + 
            "   \"RestaurantId\"," + 
            "   \"status\"," + 
            "   \"image\" " + 
            "FROM \"SYSTEM\".\"FoodItem\" where \"RestaurantId\"="+restaurant.getRestaurantId());
    list = (List<FoodItem>) query.getResultList();
    return  list;
}

当我运行这个方法时,它不会像这样返回List&lt;FoodItem&gt;,而是返回List&lt;Array&gt;

[
[
    1,
    "Pasta",
    55,
    14,
    "Veg",
    null
],
[
    2,
    "Burger",
    35,
    14,
    "Veg",
    null
]
]

如果我尝试在列表中的每个对象中将餐厅对象设置为 null,

for(int index=0 ;index< list.size();index++)
        list.get(index).setRestaurant(null);

我得到了 ClassCastException。 我需要键中的响应:根据我的实体类的值对 谁能帮我解决这个问题。 谢谢。 [更新] 解决了!

【问题讨论】:

  • 您正在尝试使用本机查询获取对象列表。我想删除本机查询会起作用。你得到的记录是元组格式的

标签: java spring hibernate rest


【解决方案1】:

您假设从本机查询中获取对象数组列表。它不会为您构造开箱即用的类型对象。如果你想输入列表,你需要做JPQL而不是原生查询。

List<Object[]> listResults = query.getResultList();

遍历列表并构造类型化对象列表 -

List<FoodItem> foodItems = new ArrayList();

for (Object[] record : listResults) {

       FoodItem item = new FoodItem();
       // set values from record, do necessary casts as well.

       foodItems.add(item);

  }

【讨论】:

  • 这行得通,我会看看 JPQL 方法。谢谢。
【解决方案2】:

NativeQuery 有一个有界类型参数。使用它并传递预期的resultClass 作为第二个参数以获得适当的通用分辨率,您将获得预期的结果。

NativeQuery<FoodItem> query = session.createNativeQuery("SELECT " + 
            "   \"Item\"," + 
            "   \"ItemName\"," + 
            "   \"ItemPrice\"," + 
            "   \"RestaurantId\"," + 
            "   \"status\"," + 
            "   \"image\" " + 
            "FROM \"SYSTEM\".\"FoodItem\" where \"RestaurantId\"="+restaurant.getRestaurantId(), 
            FoodItem.class);
List<FoodItem> list = query.getResultList();
return  list;

【讨论】:

  • 还是一样,抛出异常【请求处理失败;嵌套异常是 java.lang.ClassCastException: [Ljava.lang.Object;无法转换为 com.xxx.foodorder.entity.FoodItem(由 org.apache.catalina.loader.ParallelWebappClassLoader@0x00000001003006e8 加载)] 根本原因 java.lang.ClassCastException: [Ljava.lang.Object;无法转换为 com.xxx.foodorder.entity.FoodItem(由 org.apache.catalina.loader.ParallelWebappClassLoader@0x00000001003006e8 加载)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多