Spring 数据 Elasticsearch:在 existsBy* 存储库方法中抛出 ClassCastException
Spring Data Elasticsearch: ClassCastException thrown in existsBy* repository method
我在 ES 中有这个简单的索引:
{
"dynamic": "strict",
"properties": {
"_class": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"id": {
"type": "keyword"
},
"reviewRequestDocumentId": {
"type": "keyword"
},
"productId": {
"type": "keyword"
}
}
}
为了使用它,我使用 Spring Data Elasticsearch,因此创建了一个实体和绑定存储库:
@Data
@Builder(toBuilder = true)
@Document(indexName = "order_line", createIndex = false)
public class OrderLineDocument {
@Id
private String id;
@NotNull
private String reviewRequestDocumentId;
@NotNull
private String productId;
}
public interface OrderLineRepository extends ElasticsearchRepository<OrderLineDocument, String> {
boolean existsByReviewRequestDocumentIdAndProductId(String reviewRequestDocumentId, String productId);
}
我面临的问题与从测试方法调用的存储库方法 existsByReviewRequestDocumentIdAndProductId()
有关:
@Test
void existsByReviewRequestDocumentIdAndProductId() {
String rrdId = UUID.randomUUID().toString();
String productId = UUID.randomUUID().toString();
OrderLineDocument orderLine = OrderLineDocument.builder()
.productId(productId)
.reviewRequestDocumentId(rrdId)
.build();
String id = repository.save(orderLine).getId();
assertThat(repository.findById(id)).isNotEmpty();
assertThat(repository.existsByReviewRequestDocumentIdAndProductId(rrdId, productId)).isTrue();
}
调用它会导致 ClassCastException 和此堆栈跟踪:
java.lang.ClassCastException: class com.yotpo.review.requests.dashboard.core.documents.orderline.OrderLineDocument cannot be cast to class java.lang.Boolean (com.yotpo.review.requests.dashboard.core.documents.orderline.OrderLineDocument is in unnamed module of loader 'app'; java.lang.Boolean is in module java.base of loader 'bootstrap')
at com.sun.proxy.$Proxy140.existsByReviewRequestDocumentIdAndProductId(Unknown Source)
at com.yotpo.review.requests.dashboard.componenttest.OrderLineRepositoryTest.existsByReviewRequestDocumentIdAndProductId(OrderLineRepositoryTest.java:67)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod[=14=](ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke[=14=](ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod(TestMethodTestDescriptor.java:210)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:143)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
看起来该方法从 ES 查询整个实体而不是获取简单的 boolean
。
有人知道这样的问题吗?是错误还是我配置有误?
P.S。我也试过了
public interface OrderLineRepository extends ElasticsearchRepository<OrderLineDocument, String> {
boolean existsOrderLineDocumentByReviewRequestDocumentIdAndProductId(String reviewRequestDocumentId, String productId);
}
抛出同样的异常。
这是一个错误,目前没有考虑到它是 exists 方法的事实,并且 return 类型不是 Collection
执行参数的正常查询,第一个结果 returned。这会导致您看到错误。
解决方法是更改您的方法以进行计数:
Long countOrderLineDocumentByReviewRequestDocumentIdAndProductId(String reviewRequestDocumentId, String productId);
并检查结果是否大于零。
感谢您的报告this issue
我在 ES 中有这个简单的索引:
{
"dynamic": "strict",
"properties": {
"_class": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"id": {
"type": "keyword"
},
"reviewRequestDocumentId": {
"type": "keyword"
},
"productId": {
"type": "keyword"
}
}
}
为了使用它,我使用 Spring Data Elasticsearch,因此创建了一个实体和绑定存储库:
@Data
@Builder(toBuilder = true)
@Document(indexName = "order_line", createIndex = false)
public class OrderLineDocument {
@Id
private String id;
@NotNull
private String reviewRequestDocumentId;
@NotNull
private String productId;
}
public interface OrderLineRepository extends ElasticsearchRepository<OrderLineDocument, String> {
boolean existsByReviewRequestDocumentIdAndProductId(String reviewRequestDocumentId, String productId);
}
我面临的问题与从测试方法调用的存储库方法 existsByReviewRequestDocumentIdAndProductId()
有关:
@Test
void existsByReviewRequestDocumentIdAndProductId() {
String rrdId = UUID.randomUUID().toString();
String productId = UUID.randomUUID().toString();
OrderLineDocument orderLine = OrderLineDocument.builder()
.productId(productId)
.reviewRequestDocumentId(rrdId)
.build();
String id = repository.save(orderLine).getId();
assertThat(repository.findById(id)).isNotEmpty();
assertThat(repository.existsByReviewRequestDocumentIdAndProductId(rrdId, productId)).isTrue();
}
调用它会导致 ClassCastException 和此堆栈跟踪:
java.lang.ClassCastException: class com.yotpo.review.requests.dashboard.core.documents.orderline.OrderLineDocument cannot be cast to class java.lang.Boolean (com.yotpo.review.requests.dashboard.core.documents.orderline.OrderLineDocument is in unnamed module of loader 'app'; java.lang.Boolean is in module java.base of loader 'bootstrap')
at com.sun.proxy.$Proxy140.existsByReviewRequestDocumentIdAndProductId(Unknown Source)
at com.yotpo.review.requests.dashboard.componenttest.OrderLineRepositoryTest.existsByReviewRequestDocumentIdAndProductId(OrderLineRepositoryTest.java:67)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:688)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod[=14=](ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke[=14=](ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod(TestMethodTestDescriptor.java:210)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:206)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:143)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
看起来该方法从 ES 查询整个实体而不是获取简单的 boolean
。
有人知道这样的问题吗?是错误还是我配置有误?
P.S。我也试过了
public interface OrderLineRepository extends ElasticsearchRepository<OrderLineDocument, String> {
boolean existsOrderLineDocumentByReviewRequestDocumentIdAndProductId(String reviewRequestDocumentId, String productId);
}
抛出同样的异常。
这是一个错误,目前没有考虑到它是 exists 方法的事实,并且 return 类型不是 Collection
执行参数的正常查询,第一个结果 returned。这会导致您看到错误。
解决方法是更改您的方法以进行计数:
Long countOrderLineDocumentByReviewRequestDocumentIdAndProductId(String reviewRequestDocumentId, String productId);
并检查结果是否大于零。
感谢您的报告this issue