2016-05-23 37 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())); 

通過修改我的簡單的標準是這樣解決了這個問題

相關問題