aduygau
2017-02-07 02:51
采纳率: 50%
浏览 1.5k
已采纳

hibernate三个表联合查询不出结果

求助

//根据分类id查询商品个数
public int findCountCid(Integer cid) {
String hql = "select count(*) from Product p join p.categorySecond.category c where c.cid=?";
List list = (List) this.getHibernateTemplate().find(hql,cid);
if(list != null && list.size() > 0){
return list.get(0).intValue();
}
return 0;
}

category表

public class Category {
private Integer cid;
private String cname;
private Set categorySeconds=new HashSet();
public Set getCategorySeconds() {
return categorySeconds;
}

categorysecond表

public class CategorySecond {
private Integer csid;
private String csname;
private Category category;
private Set products=new HashSet();
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public Set getProducts() {
return products;
}
public void setProducts(Set products) {
this.products = products;
}

product表

public class Product {
private Integer pid;
private String pname;
private Double market_price;
private Double shop_price;
private String image;
private String pdesc;
private Integer is_hot;
private Date pdate;
//二级分类的外键,使用二级分类的对象
private CategorySecond categorySecond;
public CategorySecond getCategorySecond() {
return categorySecond;
}
public void setCategorySecond(CategorySecond categorySecond) {
this.categorySecond = categorySecond;
}

映射关系如下

<many-to-one name="category" lazy="false" class="cn.itcast.shop.category.vo.Category" column="cid"></many-to-one>
    <!-- 二级分类与商品的关联 -->
    <set lazy="false" name="products">
        <key column="csid"></key>
        <one-to-many class="cn.itcast.shop.product.vo.Product"/>
    </set>


    <many-to-one name="categorySecond" lazy="false" class="cn.itcast.shop.categorysecond.vo.CategorySecond"></many-to-one>


        <set order-by="csid" name="categorySeconds" lazy="false">
        <key column="cid"></key>
        <one-to-many class="cn.itcast.shop.categorysecond.vo.CategorySecond"/>
    </set>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 子 不语 2017-02-07 06:19
    已采纳

    感觉是你sql有问题,你先在数据把sql写出来看能不能查询出数据,再修改成对应的hql。

    或者直接写出sql用HibernateSessionFactory.getSession().createSQLQuery(sql);

    已采纳该答案
    打赏 评论
  • aduygau 2017-02-07 03:08

    hibernate 执行的sql语句如下,感觉少了点什么

    Hibernate:
    select
    count(*) as col_0_0_
    from
    product product0_ cross
    join
    categorysecond categoryse1_
    where
    product0_.categorySecond(感觉因为少了什么,这缺少了csid吗?)=categoryse1_.csid
    and categoryse1_.cid=?

    打赏 评论

相关推荐 更多相似问题