poster214
poster214
2009-11-23 16:32
浏览 328
已采纳

hibernate 查詢字段(SET)為null 的數據

我想要某個對象下某個集合屬性為空的搜索結果,在寫hql語句的時候測試了N次一直不成功:select c from Category c where c.childCategory is null
childCategory 是一個 集合,報
Caused by: java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
錯誤
請問應該怎樣修改
[b]问题补充:[/b]
這是一個映射自身的類,為一對多
一個類別可以有多個子類別:childCategory
【code="java"]
package com.bookseat.entitys;

import java.util.HashSet;
import java.util.Set;

public class Category {
private Integer categoryId;
private String categoryName;

public Integer getCategoryId() {
    return categoryId;
}

public void setCategoryId(Integer categoryId) {
    this.categoryId = categoryId;
}

private Category fatherCategory;
private Set<Category> childCategory = new HashSet<Category>();
private Set<Dishware> dishwaresByCategory = new HashSet<Dishware>();

public String getCategoryName() {
    return categoryName;
}

public void setCategoryName(String categoryName) {
    this.categoryName = categoryName;
}

public Set<Category> getChildCategory() {
    return childCategory;
}

public void setChildCategory(Set<Category> childCategory) {
    this.childCategory = childCategory;
}

public Set<Dishware> getDishwaresByCategory() {
    return dishwaresByCategory;
}

public void setDishwaresByCategory(Set<Dishware> dishwaresByCategory) {
    this.dishwaresByCategory = dishwaresByCategory;
}

public Category(String categoryName, Category fatherCategory,
        Set<Category> childCategory, Set<Dishware> dishwaresByCategory) {
    super();
    this.categoryName = categoryName;
    this.fatherCategory = fatherCategory;
    this.childCategory = childCategory;
    this.dishwaresByCategory = dishwaresByCategory;
}

public Category getFatherCategory() {
    return fatherCategory;
}

public void setFatherCategory(Category fatherCategory) {
    this.fatherCategory = fatherCategory;
}

public Category() {

}

public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((categoryId == null) ? 0 : categoryId.hashCode());
    result = prime * result
            + ((categoryName == null) ? 0 : categoryName.hashCode());
    return result;
}

public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    final Category other = (Category) obj;
    if (categoryId == null) {
        if (other.categoryId != null)
            return false;
    } else if (!categoryId.equals(other.categoryId))
        return false;
    if (categoryName == null) {
        if (other.categoryName != null)
            return false;
    } else if (!categoryName.equals(other.categoryName))
        return false;
    return true;
}

}
[/code]
配置文件:
[code="xml"]
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">





column="Category_Name" />
cascade="save-update" lazy="false" >



cascade="save-update">










[/code]
按照yayg 兄的寫了搜索,不報錯但是仍然為空

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • javabull
    javabull 2009-11-23 20:37
    已采纳

    from Category as c where c.childCategory is empty
    集合用is empty普通属性用is null

    点赞 评论
  • yayg2008
    yayg2008 2009-11-23 16:41

    你的表是怎么个情况?主从表,一对多?
    你的hibernate是哪个版本的?如果是3.1以上的好像主从表要加inner join。
    from Category as c inner join c.childCategory as cc where cc is null

    点赞 评论
  • wanghaolovezlq
    wanghaolovezlq 2009-11-23 18:37

    你写的hql语句有问题嘛

    你查询的代码粘出来分析

    点赞 评论

相关推荐