2014-02-19 73 views
1

沒有爲Product表分頁搜索。搜索查看Product對象以及ProductParam對象。正確分頁withCriteria而看多到一個表(Grails的)

的問題是有在搜索結果中重複的產品:具有25個項目每頁,三分之二被複制的產品。

如果在標準建造應用resultTransformer org.hibernate.Criteria.DISTINCT_ROOT_ENTITY,每頁導致項目超過25個少 - 這是關於6

在這兩種情況下,搜索被打破。這個問題能解決嗎? (如果沒有完全重寫代碼)

class Product { 
    String name 
    static hasMany = [ params: ProductParam ] 
} 

class ProductParam { 
    String key 
    String value 
    static belongsTo = [ product: Product ] 
} 

HibernateCriteriaBuilder criteriaBuilder = Product.createCriteria() 
PagedResultList results = criteriaBuilder.list(max: 25, offset: offset) { 
    or { 
    // searching in Product 
    ilike 'name', "%${query}%" 

    // searching in ProductParam 
    createAlias('params', 'pp') 
    ilike 'pp.value', "%${query}%" 
    } 
    //resultTransformer org.hibernate.Criteria.DISTINCT_ROOT_ENTITY 
} 

的Grails 2.2.0,Postgres的

+0

多少在總(忽略分頁),你希望如果查詢按預期工作? – dmahapatro

回答

1

您的代碼看起來有點過於複雜:) 我把它這樣:

def results = Product.withCriteria{ 
    projections{ distinct 'id' } 
    or{ 
    eq 'name', "%${query}%" // do you really mean *eq* here, not *ilike*? 
    params{ 
     ilike 'value', "%${query}%" 
    } 
    } 
    maxResults 25 
    firstResult offset 
}