2015-09-04 68 views
1

this question類似,我想使用我自己的用戶定義類型「AccountNumber」執行SQL「like」操作。QueryDSL like operation SimplePath

的QueryDSL實體類定義了列的字段是這樣的:

public final SimplePath<com.myorg.types.AccountNumber> accountNumber; 

我曾嘗試下面的代碼,實現了「喜歡」的SQL操作,但在類型前相比得到一個錯誤運行查詢:

final Path path=QBusinessEvent.businessEvent.accountNumber; 
final Expression<AccountNumber> constant = Expressions.constant(AccountNumber.valueOfWithWildcard(pRegion.toString())); 
final BooleanExpression booleanOperation = Expressions.booleanOperation(Ops.STARTS_WITH, path, constant); 
expressionBuilder.and(booleanOperation); 

的錯誤是:

org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [7!%%] did not match expected type [com.myorg.types.AccountNumber (n/a)] 

有沒有人能夠使用QueryDSL/JPA組合實現這一目標?

回答

0

您是否嘗試使用String常量?

Path<?> path = QBusinessEvent.businessEvent.accountNumber; 
Expression<String> constant = Expressions.constant(pRegion.toString()); 
Predicate predicate = Expressions.predicate(Ops.STARTS_WITH, path, constant); 
0

最終,我得到了一個提示我的同事做到以下幾點:

if (pRegion != null) { 
     expressionBuilder.and(Expressions.booleanTemplate("{0} like concat({1}, '%')", qBusinessEvent.accountNumber, pRegion)); 
} 

這似乎這樣的伎倆!

0

它似乎有錯誤/模糊。在我的情況下,我需要搜索不同類型的字段(String,Number),例如SQL是這樣的:

SELECT * FROM table AS t WHERE t.name = "%some%" OR t.id = "%some%"; 

我的代碼如下所示:

BooleanBuilder where = _getDefaultPredicateBuilder(); 
BooleanBuilder whereLike = new BooleanBuilder(); 
for(String likeField: _likeFields){ 
    whereLike = whereLike.or(_pathBuilder.getString(likeField).contains(likeValue)); 
} 
where.and(whereLike); 

如果第一_likeFieldsString類型 - 請求正常工作,否則拋出異常。