关于Hibernate在使用原生SQL语句多表查询所遇到的问题

来源:互联网 发布:html5 720度全景源码 编辑:程序博客网 时间:2024/05/16 00:41

今天早上想实现商品的分类功能时,使用的是原生SQL语句中的多表查询语法,遇到的问题:

  • 1、写好SQL语句后,调用createQuery(hql)方法是出现 org.hibernate.hql.internal.ast.QuerySyntaxException异常,该异常信息是映射文件的字段与数据库字段不一致,或者名称不一致导致;
public List<ClothesType> findClothesType(int categoryId) {        String hql="select c.id,c.nameType from ClothesType as c         join RelationPro as r on c.id=r.clotherstypeId where         r.categoryId=1";        Query query=getSession().createQuery(hql);        //query.setInteger(0, categoryId);        @SuppressWarnings("unchecked")        List<ClothesType> list = query.list();        System.out.println(list.get(0).getNameType());        System.out.println("hhhhhhhhhh"+list.size());        return list;    }

最后修改为createSQLQuery(hql)才成功;

public List<ClothesType> findClothesType(int categoryId) {        String hql="select c.id,c.nameType from ClothesType as c join RelationPro as r on c.id=r.clotherstypeId where r.categoryId=1";        Query query=getSession().createSQLQuery(hql).addEntity(ClothesType.class);        //query.setInteger(0, categoryId);        @SuppressWarnings("unchecked")        List<ClothesType> list = query.list();        System.out.println(list.get(0).getNameType());        System.out.println("hhhhhhhhhh"+list.size());        return list;    }

备注:如果没有在createSQLQuery()后调用addEntity(ClothesType.class);方法,查出来的结果集与对象字段不对应


0 0
原创粉丝点击