Hibernate不会检索所需的记录

时间:2013-10-02 02:07:04

标签: java hibernate hibernate-mapping

我无法从数据库中检索所需的记录,我的类如下,我需要知道哪些用户有非活动的篮子。

Class Person {

}

Class User extends Person{
   private Set<Basket> basketlist;
}

Class Prof extends User {

}

Class Basket{
  private String active;
}


           Criteria cre = session.createCriteria(User.class, "user")
                    .createAlias("user.basketlist", "basket");
            ProjectionList pl = Projections.projectionList();
            pl.add(Projections.property("user.id").as("id"));
            cre.setProjection(pl);
            cre.add(Restrictions.eq("basket.active", null));
            IDs = cre.list();

以上代码生成以下查询(我不确定为什么它包含此查询中的Prof以及为什么它不向非活动篮子的用户显示)

select this_.id as y0_ from User this_ inner join Person this_1_ on 
this_.id=this_1_.id left outer join Prof this_2_ on this_.id=this_2_.id inner join 
User_basketlist basketl3_ on this_.id=basketl3_.User_id inner join basketlist 
basketl1_ on basketl3_.basketlist_id=basketl1_.id where basketl1_.active=?

我的Hibernate版本

 <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.2.1.Final</version>
            <type>jar</type>
        </dependency>

2 个答案:

答案 0 :(得分:1)

对于NULL,它应该是

Restrictions.isNull("basket.active")

答案 1 :(得分:1)

我对Restrictions.eq("basket.active", null)持怀疑态度。据说4.2.0.CR1根据this修正了4.2.1.Final对我来说这意味着在where basketl1_.active=?中应该没问题,但是从查询中我可以看到它正在生成Restrictions.isNull("basket.active")错误。试试{{1}}