天天看点

hibernate 使用org.hibernate.criterion.Example 查询无效

今天使用example查询,结果发现设置的条件无效.

查询代码:

hibernate 使用org.hibernate.criterion.Example 查询无效

/*** 

     *  

     * @param obj 

     * @param includezeros 

     * @return 

     */  

    private criteria getcriteria( object obj, boolean includezeros) {  

        if (obj == null) {  

            return this.sessionfactory.getcurrentsession().createcriteria(clz);  

        } else {  

            example example = example.create(obj);  

            if (!includezeros) {  

                example = example.excludezeroes();  

            }  

            criteria criteria = this.sessionfactory.getcurrentsession()  

                    .createcriteria(clz);  

            criteria.add(example);  

            return criteria;  

        }  

    }  

 测试

hibernate 使用org.hibernate.criterion.Example 查询无效

ordersdao ordersdao=(ordersdao)ctx.getbean("ordersdao");  

orders orders=new orders();  

        orders.setbarcode("00001925");  

list list=new arraylist();  

//      long count=ostypedao.listbypage(ostype, false, list, 0, 5);  

        long count=ordersdao.listbypage(orders, false, list, 0, 5);  

        system.out.println("count:"+count);  

 但是执行时,发现where后面是1=1,即设置的查询条件(orders.setbarcode("00001925"))没有起作用.

可能是代码的问题

orders 结构如下:

hibernate 使用org.hibernate.criterion.Example 查询无效

 * note:不允许修改表结构<br />table:orders<br /> 

 * 订单类 

 * @author huangwei 

 * @since 2014-09-02 

 */  

@entity  

@table(name = "orders")  

public class orders {  

    /*** 

     * 条码,订单号 

    private string barcode;  

     * 本订单返工次数,新单为0,返工累加∑1-n 

    private int serial;  

    @id  

    @column(name="order_id",unique=true)  

    public string getbarcode() {  

        return barcode;  

    public void setbarcode(string barcode) {  

        this.barcode = barcode;  

}  

 后来发现突然发现其他字段没问题,那么barcode有什么特殊的地方吗?

再一看表,原来它是主键.