poster214
poster214
2009-11-23 16:32

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

  • hibernate

我想要某個對象下某個集合屬性為空的搜索結果,在寫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条回答