Filter on join table attribute
See original GitHub issueFirstly, thanks for your open source contribution.
I would like to know if what I am trying is supported by the specification-arg-resolver. For example, I would like to retrieve all customers whose city of address matches ‘Montreal’ in the below entity model.
@Entity
public class Customer implements Serializable {
@Id @GeneratedValue
private Long id;
@OneToMany
@JoinColumn(name = "customerId")
private List<Address> addresses;
}
@Entity
public class Address implements Serializable {
@Id
private Long contactId;
private String city;
}
I tried below spec annotation, but it didn’t work.
@RequestMapping(value = "", params = { "city" })
@ResponseBody
public Iterable<Customer> filterCustomersByCity(
@JoinFetch(paths = {"addresses" })
@Spec(path="addresses.city", params="city", spec=In.class)
final Specification<Customer> customersByCitySpec) {
return customerRepo.findAll(customersByCitySpec);
}
I get below error
java.lang.IllegalStateException: Illegal attempt to dereference path source [null.addresses] of basic type at org.hibernate.jpa.criteria.path.AbstractPathImpl.illegalDereference(AbstractPathImpl.java:98) at org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:182) at net.kaczmarzyk.spring.data.jpa.domain.PathSpecification.path(PathSpecification.java:42) at net.kaczmarzyk.spring.data.jpa.domain.In.toPredicate(In.java:64) at org.springframework.data.jpa.domain.Specifications$1.toPredicate(Specifications.java:64) at org.springframework.data.jpa.domain.Specifications.toPredicate(Specifications.java:114) at net.kaczmarzyk.spring.data.jpa.domain.Conjunction.toPredicate(Conjunction.java:58)
On debugging, I see it is not able to fetch city field. What am I doing wrong? Could you please point me in right direction.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:10 (5 by maintainers)
v1.0.0 with join support has been released.
I was in assumption that ’ @JoinFetch(paths = {“addresses” }) ’ along with ’ @Spec(path=“addresses.city” ’ reference to OneToMany attribute ‘addresses’ + join-table attribute ‘city’ will produce the specification object with join + distinct you mentioned in option 2.
I would be happy to work with you on this. Let me know how I can help. Thanks for your prompt reply and effort.