首先,我正在维护一些我没有编写的代码,而且我对Java和Hibernate还不熟悉。我最近调整了一个在Hibernate会话中运行的查询。返回类型没有改变(它仍然是List)但我将查询从session.createquery更改为session.createSQLquery。出于某种原因,这破坏了使用返回List的代码。
以下是消费方法:
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循环(对于(Object [] p:Products){)导致异常。
以下是异常:java.lang.ClassCastException:ProductListing无法强制转换为[Ljava.lang.Object;
我不明白为什么更改查询会破坏此代码。查询返回一个对象数组,就像在调整之前一样。
我很欣赏这种困惑的任何见解。
感谢。
之前的DAO: 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(Long year,Long prodId,Long listingId,String letter)抛出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;
}
抱歉,格式化程度不高。这是我的第一篇文章和
答案 0 :(得分:2)
您只选择了pl.*
并且您使用的是addEntity("pl", ProductListing.class)
,因此返回的列表是ProductListing
类型的对象。旧查询也选择了CHILD_COUNT
,但现在不再选择了{{1}}。我假设您忘记了新查询中的COUNT值。
答案 1 :(得分:0)
看起来将查询从“createquery”更改为createsqlquery“导致Hiberate返回一个ProductListing对象列表而不是List
很难说不看ProductDAO - 旧版本和新版本。