Reputation: 1
I have an searchValue, inputted by user, and I want to find all the customers that have similar name, email, username, phone,... etc. like it. I'm using the Specifications to create a spec then inject it into repository file This is the Entity
public class DealRequestEntity {
private long id;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "customer_id", referencedColumnName = "id")
private CustomerEntity customer;
}
And the Specifications
public class DealRequestSpecifications {
public static Specification<DealRequestEntity> hasSearchValue(String searchValue) {
return new Specification<DealRequestEntity>() {
public Predicate toPredicate(Root<DealRequestEntity> root, CriteriaQuery<?> query,
CriteriaBuilder builder) {
return builder.like(builder.lower(root.get("customer").get("name")), searchValue.toLowerCase());
}
};
}
}
And the get("name") always warning error there. So what is the right keyword that I can research for this?
I tried to get("customer").get("name") but it didn't work
Upvotes: 0
Views: 62
Reputation: 1
I found the way, after 2 days of roaming around.
public static Specification<DealRequestEntity> hasSearchValue(String searchValue) {
return (root, query, builder) -> {
Join<DealRequestEntity, CustomerEntity> customerJoin = root.join("customer");
return builder.or(
builder.like(builder.lower(customerJoin.get("name")), "%" + searchValue.toLowerCase() + "%"),
builder.like(builder.lower(customerJoin.get("username")), "%" + searchValue.toLowerCase() + "%"),
builder.like(builder.lower(customerJoin.get("email")), "%" + searchValue.toLowerCase() + "%"),
builder.like(builder.lower(customerJoin.get("phone")), "%" + searchValue.toLowerCase() + "%"));
};
}
Upvotes: 0