gpt4 book ai didi

Java 类转换异常,涉及 Hibernate 返回的 List

转载 作者:行者123 更新时间:2023-11-30 05:00:58 24 4
gpt4 key购买 nike

首先,我正在维护一些我没有编写的代码,而且我对 Java 和 Hibernate 相当陌生。我最近调整了一个在 Hibernate session 中运行的查询。返回类型没有更改(它仍然是列表),但我将查询从 session.createquery 更改为 session.createSQLquery。由于某种原因,这破坏了使用返回列表的代码。

消费方法如下:

public void getStuff(CompanyDAO company, ProductDAO dao, Long listingId, Long year, Long prodId, String startTag, String endAdFileTag, String letter) throws DAOException {
List<Object[]> Products = dao.loadStuff(year, prodId, listingId, letter);

if (Products != null && Products.size() > 0) {
for (Object[] p : Products) {
if (p != null) {
ProductListing parentChild = (ProductListing) p[0];
Long childCountObj = (Long) p[1];
int childCount = childCountObj.intValue();

if (parentChild != null) {
parentChild.setNextLevelChildrenCount(childCount);
}
}
}
}
}

增强的 for 循环 ( for (Object[] p : Products) { ) 导致了异常。

这里是异常:java.lang.ClassCastException:ProductListing无法转换为[Ljava.lang.Object;

我不明白为什么更改查询会破坏此代码。该查询返回一个对象数组,就像调整之前一样。

如果您能对这个难题有任何见解,我将不胜感激。

谢谢。

之前的 DAO:
public List loadStuff(Longyear,LongprodId,LonglistingId,String letter) throws DAOException { session session = HibernateUtil.getSession(HibernateUtil.keys[sessionId]); StringBuffer strQuery = new StringBuffer(); if (year == null || prodId == null || ListingId == null ) return null;

    logger.debug("prodCode = " + prodId);
logger.debug("year = " + year);


strQuery.append("select pl, (select count(*) from ProductListing pppl where pppl.listingId = :listingid ");
strQuery.append(" and pppl.responseYear = :year and pppl.parentProductId = pl.productId) as CHILD_COUNT ");
strQuery.append(" from ProductListing pl ");
strQuery.append(" where pl.parentProductId = :prodid ");
strQuery.append(" and pl.responseYear = :year ");
strQuery.append(" and pl.listingId = :listingid ");

strQuery.append(" and (exists(select 0 from ProductListingMain plm, PdMain pm ");
strQuery.append(" where plm.productListingId = pl.id ");
strQuery.append(" and pm.currentVersionId = plm.versionId ");
strQuery.append(" and pm.responded = 'Y' ");
strQuery.append(" and pm.deleteFlag = 'N' ");
strQuery.append(" and pm.onlineFlag = 'Y') ");
strQuery.append(" or exists(select 0 from ProductListing ppl ");
strQuery.append(" where ppl.listingId = :listingid ");
strQuery.append(" and ppl.responseYear = :year ");
strQuery.append(" and ppl.parentProductId = pl.productId)) ");

//todo iclude the param letter in the query ... please test it before implementing
if (letter != null) strQuery.append(" and upper (pl.onlineDescription) like '" + letter + "%' ");

Query query = session.createQuery(strQuery.toString())
.setLong("year", year)
.setLong("prodid", prodId)
.setLong("listingid", listingId);

List list = query.list();
return list;
}
DAO after:

public List loadStuff(Longyear,LongprodId,LonglistingId,String letter) throws DAOException { session session = HibernateUtil.getSession(HibernateUtil.keys[sessionId]); StringBuffer strQuery = new StringBuffer(); if (year == null || prodId == null || ListingId == null ) return null;

   String letterClause;
if (letter != null)
letterClause = " and upper (pl.onlineDescription) like '" + letter + "%' \n";
else
letterClause = " ";

strQuery.append(
" select pl.* \n" +
" from PRODUCT_LINE p, PRODUCT_LINE_X_LISTING pll, \n" +
" PRODUCT_X_LISTING pl, CURRENT_YEAR cy, \n" +
" LISTING l \n" +
" where p.id = pll.PRODUCT_LINE_ID \n" +
" and p.prod_line_no = cy.PROD_LINE_MASTER \n" +
" and pl.RESPONSE_YEAR = cy.ONLINE_YEAR \n" +
" and pl.LISTING_ID = cy.LISTING_ID \n" +
" and pll.LISTING_ID = l.id \n" +
" and cy.LISTING_ID = l.id \n" +
" and pll.YEAR = cy.ONLINE_YEAR \n" +
" and p.Id = pll.product_line_id \n" +
" and pl.PARENT_PRODUCT_ID = " + prodId +
" and pl.LISTING_ID = " + listingId +
" and exists \n" +
" (select 'x' \n" +
" from pd_product_listing_mv mv \n" +
" where mv.listing_id = pl.listing_id \n" +
" and mv.id = pl.id) \n" +
letterClause +
" order by upper(pl.ONLINE_DESCRIPTION) ");

Query query = session.createSQLQuery(strQuery.toString())
// return all columns in the ProductListing table, i.e. the entire class
.addEntity("pl", com.acme.run.model.directories.ProductListing.class);
List list = query.list();

return list;
}

抱歉,这没有更好的格式。这是我的第一篇文章,

最佳答案

您仅选择 pl.*并且您正在使用 addEntity("pl", ProductListing.class) ,因此返回的 List 是 ProductListing 类型的对象。旧查询选择 CHILD_COUNT也有,但不再是了。我猜您忘记了新查询中的 COUNT 值。

关于Java 类转换异常,涉及 Hibernate 返回的 List<Object[]>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6736561/

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