Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JPA specification - filerting on joining table's embedded object #15

Closed
chao-huang opened this issue Nov 16, 2017 · 3 comments
Closed

Comments

@chao-huang
Copy link

你好,

我们有个场景需要对join表的embedded属性字段做过滤。假设我们有如下2个@entity类和一个@embeddable类:

`
@entity
@DaTa
public class Product {
@id
@column(columnDefinition = "bigint(10)", name = "id", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Column(length = 100)
String name;

@JsonManagedReference
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "product")
protected List<Variant> variants;

}

@entity
@DaTa
public class Variant {

@Id
@Column(columnDefinition = "bigint(10)", name = "id", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

@Column(name = "sku", length = 50)
protected String sku;

@Embedded
protected Money price;

@JsonBackReference
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "product_id")
protected Product product;

}

@embeddable
@DaTa
public class Money {

@Column(length = 10)
private String currency;

@Column(columnDefinition = "bigint(13)")
private Integer amount;

}
`

当我创建如下specification进行查询时候,会报错。specification如下:

`
public Page findAllProducts(Pageable pageable) {
Specification specification = Specifications.and()
.eq("name", "product-111")
.ge("variants.price.amount", 2000)
.build();

    return productRepository.findAll(specification, pageable);
}

`
Java报错信息如下:

java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [price.amount] on this ManagedType [com.example.sample.model.Variant] at org.hibernate.jpa.internal.metamodel.AbstractManagedType.checkNotNull(AbstractManagedType.java:128) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.jpa.internal.metamodel.AbstractManagedType.getAttribute(AbstractManagedType.java:113) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.jpa.criteria.path.AbstractFromImpl.locateAttributeInternal(AbstractFromImpl.java:116) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.jpa.criteria.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:204) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] at org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:177) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final] at com.github.wenhao.jpa.specification.GeSpecification.toPredicate(GeSpecification.java:22) ~[classes/:na] at com.github.wenhao.jpa.PredicateBuilder.lambda$build$0(PredicateBuilder.java:131) ~[classes/:na] at org.springframework.data.jpa.repository.support.SimpleJpaRepository.applySpecificationToCriteria(SimpleJpaRepository.java:718) ~[spring-data-jpa-1.11.8.RELEASE.jar:na] at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:649) ~[spring-data-jpa-1.11.8.RELEASE.jar:na] at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:608) ~[spring-data-jpa-1.11.8.RELEASE.jar:na] at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:407) ~[spring-data-jpa-1.11.8.RELEASE.jar:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:504) ~[spring-data-commons-1.13.8.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:489) ~[spring-data-commons-1.13.8.RELEASE.jar:na] at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461) ~[spring-data-commons-1.13.8.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56) ~[spring-data-commons-1.13.8.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133) ~[spring-data-jpa-1.11.8.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57) ~[spring-data-commons-1.13.8.RELEASE.jar:na] at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.12.RELEASE.jar:4.3.12.RELEASE] at com.sun.proxy.$Proxy104.findAll(Unknown Source) ~[na:na] at com.example.sample.controller.ProductController.findAllProducts(ProductController.java:52) ~[classes/:na] at com.example.sample.controller.ProductController.findProducts(ProductController.java:43) ~[classes/:na]
请问这个问题该如何处理?谢谢帮忙看一下。

@wenhao
Copy link
Owner

wenhao commented Dec 8, 2017

抽个时间我试一下

@baoqintian
Copy link

怎么引入到自己的项目中??maven项目

@wenhao
Copy link
Owner

wenhao commented Jan 3, 2019

怎么引入到自己的项目中??maven项目

文档里面有描述。

<dependency>
    <groupId>com.github.wenhao</groupId>
    <artifactId>jpa-spec</artifactId>
    <version>3.2.3</version>
</dependency>

@wenhao wenhao closed this as completed Jan 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants