我尝试了一个hibernate第17章SQL native的例子总结一下,我将使用Hibernate返回一个带有SQL查询的对象XXX ..这是我的例子
@Override
public Composant FindComposantFRomXls(String comp, String fab) {
return (Composant) getSessionFactory().getCurrentSession().createSQLQuery("select * from composant where ref_composant='"+comp+" ' and fabricant =(select id_fabricant from fabricant where nom_fabricant ='"+fab+"')").addEntity(Composant.class);
}
这是我的错误信息
. lang。ClassCastException: org.hibernate.internal.SQLQueryImpl不能强制转换为com.JEE.model.Composant
return (Composant) getSessionFactory().getCurrentSession().
createSQLQuery("select * from composant c where c.ref_composant=:param
and c.fabricant = (select id_fabricant from fabricant f where f.nom_fabricant =:param2)")
.addEntity(Composant.class)
.setParameter("param", comp).setParameter("param2", fab).uniqueResult();
它工作良好....由于mkyong