querydsl:比较数组

qxsslcnc  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(412)

我对querydsl数组比较有问题。问题是querydsl中缺少字节数组的比较,因为arraypath扩展了simpleexpression,simpleexpression只有eq、ne、in、isnull等方法。。。
使用kotlin、Hibernate5.2、QueryDSL4.1.4、MySQL5.7
具有hibernate ebmeddable对象ipaddress

@Embeddable
class IpAddress(
        var ipAddressBinary: ByteArray
)

数据库中的Map到varbinary(4)类型(对于ipv4地址,计划在将来支持ipv6)。我存储在那里的ip地址在二进制形式,我需要在范围内搜索。
我想创建kotlin扩展函数来支持嵌入对象上的goe和loe函数
到目前为止我想到了这个:

fun ArrayPath<ByteArray, Byte>.goe(ipBytes: ByteArray): BooleanExpression {
  return Expressions.booleanOperation(Ops.GOE, ConstantImpl.create(ipBytes))
}

fun QIpAddress.goe(ipAddressStr: String): BooleanExpression {
  val ipBytes = MapperUtils.IPv4.normalizedToBytes(ipAddressStr)
  return this.ipAddressBinary.goe(ipBytes)
}

但是打电话

query.where(QBlackListRecord.blackListRecord.startAddress.goe(someByteArrayRepresentingIpAddress))

导致异常:

[exec-10]java.lang.IndexOutOfBoundsException: index (1) must be less than size (1)
at com.google.common.base.Preconditions.checkElementIndex(Preconditions.java:310)
at com.google.common.base.Preconditions.checkElementIndex(Preconditions.java:292)
at com.google.common.collect.SingletonImmutableList.get(SingletonImmutableList.java:45)
at com.querydsl.core.types.Template$ByIndex.convert(Template.java:190)
at com.querydsl.core.support.SerializerBase.visitOperation(SerializerBase.java:256)
at com.querydsl.jpa.JPQLSerializer.visitOperation(JPQLSerializer.java:426)
at com.querydsl.core.support.SerializerBase.visit(SerializerBase.java:231)
at com.querydsl.core.support.SerializerBase.visit(SerializerBase.java:31)
at com.querydsl.core.types.OperationImpl.accept(OperationImpl.java:83)
at com.querydsl.core.support.SerializerBase.handle(SerializerBase.java:92)
at com.querydsl.core.support.SerializerBase.visitOperation(SerializerBase.java:267)
at com.querydsl.jpa.JPQLSerializer.visitOperation(JPQLSerializer.java:437)
at com.querydsl.core.support.SerializerBase.visit(SerializerBase.java:231)
at com.querydsl.core.support.SerializerBase.visit(SerializerBase.java:31)
at com.querydsl.core.types.OperationImpl.accept(OperationImpl.java:83)
at com.querydsl.core.support.SerializerBase.handle(SerializerBase.java:92)
at com.querydsl.jpa.JPQLSerializer.serialize(JPQLSerializer.java:220)
at com.querydsl.jpa.JPAQueryBase.serialize(JPAQueryBase.java:60)
at com.querydsl.jpa.JPAQueryBase.serialize(JPAQueryBase.java:50)
at com.querydsl.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:98)
at com.querydsl.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:94)
at com.querydsl.jpa.impl.AbstractJPAQuery.fetch(AbstractJPAQuery.java:201)
at org.springframework.data.jpa.repository.support.QuerydslJpaRepository.findAll(QuerydslJpaRepository.java:164)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:377)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:629)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:593)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.repository.core.support.MethodInvocationValidator.invoke(MethodInvocationValidator.java:99)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy144.findAll(Unknown Source)

我不确定我对这件事的态度是否正确。querydsl使用简单比较运算符生成简单sql,有没有其他方法来比较字节数组?
我只需要生成这样的sql

select *
from black_list_record
where start_ip_address_binary >= @binary_value

或者像这样

select *
from black_list_record
where start_ip_address_binary >= inet6_aton('10.20.0.1')
eiee3dmh

eiee3dmh1#

错误是由函数引起的

fun ArrayPath<ByteArray, Byte>.goe(ipBytes: ByteArray): BooleanExpression {
  return Expressions.booleanOperation(Ops.GOE, ConstantImpl.create(ipBytes))
}

应该是这样的

fun ArrayPath<ByteArray, Byte>.goe(ipBytes: ByteArray): BooleanExpression {
  return Expressions.booleanOperation(Ops.GOE, this, ConstantImpl.create(ipBytes))
}

我没有为goe操作传递“this”参数,因为它需要两个参数,因为它的模板是{0}>={1}。仅此而已。。。
如果您不使用kotlin而使用java,则可以使用querydsl内置方法委派-http://www.querydsl.com/static/querydsl/4.2.1/reference/html_single/#d0e2474

相关问题