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

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 2009-11-23 20:37
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)