【问题标题】:Hibernate criteria.list() returns empty list when using Restrictions.notHibernate criteria.list() 在使用 Restrictions.not 时返回空列表
【发布时间】:2016-05-23 18:05:37
【问题描述】:

我正在将 Spring 与 JPA 2.0 和 Hibernate 4.2.19.Final 一起使用,并且我正在尝试构建一个动态查询,该查询具有从 Restrictions 类类似方法生成的简单谓词。 Restrictions.like("attributes.value" + value.getKey() , value.getValue());。我的实体存储在一个稀疏表中,其中列的编号与属性描述相关。

实体:

@Entity
@Table(name = "MY_ENTITIES")
public class Entity {

  @Id
  @GeneratedValue
  Long id;

  @Embedded
  Attributes attributes;
}

@Embeddable
public class Attributes{

  /** Attribute 1. */
  @Embedded
  @Column(name = "value_1")
  private String attribute1;

         *

  /** Attribute N. */
  @Embedded
  @Column(name = "value_N")
  private String attributeN;
}

还有一些复杂的谓词,如 AND、OR、NOT 谓词是通过嵌套上述简单谓词获得的。

当我使用 AND 和 OR 谓词但使用涉及 NOT 的更复杂的表达式时,一切似乎都运行良好,例如:

((attribute1=% OR attribute2=%) AND attribute3=%) AND NOT attribute4=%

当数据库中存在满足条件的实体时,Criteria.list() 方法返回空列表。

有什么建议吗?

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

    显然是标准:

    Restrictions.not(Restrictions.like("attributes.value" + value.getKey(), value.getValue()));
    

    如果表中的值为null,则返回false

    通过像这样修改我的简单标准来解决这个问题:

    Restrictions.and(Restrictions.isNotNull("attributes.value" + value.getKey()),
         Restrictions.like("attributes.value" + value.getKey(), value.getValue()));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      相关资源
      最近更新 更多