我有以下问题。 我使用了spring-data,我想为Long Property创建一个“like”语句。
我尝试了以下内容,但没有成功。
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]
我做错了什么?
答案 0 :(得分:3)
这是因为您的实体类中的kundenNr
是Long
,而%4%
的解析时间不长。
在SQL中,查询中的%符号仅适用于varchar(以及某些变体),因此无法在长字段上使用它,这可能会转换为数据库中的数字字段。
答案 1 :(得分:0)
使用以下规范来查找其列(NUMBER)与某些数字匹配的记录:
public static org.springframework.data.jpa.domain.Specification<Model> numberLikeValue(Long value) {
return (root, query, builder) ->
org.springframework.util.ObjectUtils.isEmpty(value)
? builder.conjunction()
: builder.like(builder.function("TO_CHAR", String.class, root.get("dbColumn")), contains(String.valueOf(value)));
}
使用了Oracle数据库。