2014-02-25 58 views
2

我有以下問題。 我使用了spring-data,我想爲Long Property創建一個「like」語句。Spring Data JPA:具有「like」和long的規範

我試過以下,但沒有成功。

private Specification<T> createSpecification() 
{ 
    return new Specification<T>() 
    { 

     @Override 
     public Predicate toPredicate(final Root<T> root, final CriteriaQuery<?> query, final CriteriaBuilder cb) 
     { 

      final Predicate like = query.where(cb.like(root.<String> get("kundenNr"), "%4%")).getRestriction(); 

      return like; 
     } 
    }; 

} 

和領域類

public class Kunde implements Serializable 
{ 

@ColumnYB(lable = "KundenNr.", order = 1) 
@Id 
@javax.persistence.Column(nullable = false, name = "kundennr") 
protected Long kundenNr; 

@ColumnYB(lable = "Deaktiviert") 
@Column(nullable = false) 
private boolean deaktiviert = false; 

@Column(name = "kundenart") 
private String kundenart; 
... 

我碰到下面的錯誤。

org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [%4%] did not match expected type [java.lang.Long]; nested exception is java.lang.IllegalArgumentException: Parameter value [%4%] did not match expected type [java.lang.Long] 
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384) ~[spring-orm-4.0.1.RELEASE.jar:4.0.1.RELEASE] 
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:157) ~[spring-orm-4.0.1.RELEASE.jar:4.0.1.RELEASE] 
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417) ~[spring-orm-4.0.1.RELEASE.jar:4.0.1.RELEASE] 
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-4.0.1.RELEASE.jar:4.0.1.RELEASE] 
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213) ~[spring-tx-4.0.1.RELEASE.jar:4.0.1.RELEASE] 
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147) ~[spring-tx-4.0.1.RELEASE.jar:4.0.1.RELEASE] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.0.1.RELEASE.jar:4.0.1.RELEASE] 
at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeRepositoryPostProcessor.java:92) ~[spring-data-jpa-1.4.1.RELEASE.jar:na] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.0.1.RELEASE.jar:4.0.1.RELEASE] 
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.0.1.RELEASE.jar:4.0.1.RELEASE] 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.0.1.RELEASE.jar:4.0.1.RELEASE] 

我做錯了什麼?

回答

2

這是因爲您的實體類中的kundenNrLong,並且%4%不會解析這麼長時間。

在SQL中,查詢中的%符號僅適用於varchar(以及某些變體),因此無法在長字段中使用該字段,該字段可能會轉換爲數據庫中的數字字段。