2016-05-23 63 views
0

我在JPA 2.0和Hibernate 4.2.19.Final中使用Spring,我尝试构建一个动态查询,它具有从Restrictions类生成的简单谓词方法。 Restrictions.like("attributes.value" + value.getKey() , value.getValue());。我的实体存储在一个稀疏表中,其中列的编号与属性描述相关。当使用限制时,Hibernate的criteria.list()返回空列表。

实体:

@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()方法返回空列表时DB中有实体满足标准。

有什么建议吗?

回答

0

显然准则:

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

回报false如果表中的值是null

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

通过修改我的简单的标准是这样解决了这个问题