I am using spring boot 1.3.3 with starter-data-jpa + I am using specification-arg-resolver for combine multiple filters in JPA Repo
I have a filed call status attribute in entity which is matching to the database status id, nevertheless, we want search by status name, I defined all the status name in hashMap of the entity it is like following code
However when I try to filter by status name I got
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Unable to locate Attribute with the the given name [statusName] on this ManagedType [api.domain.Inscription]; nested exception is java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [statusName] on this ManagedType [api.domain.Inscription]] with root cause
java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [statusName] on this ManagedType [api.domain.Inscription]
Entity class:
@Entity
@Table(name="INSCRIPTIONS")
public class Inscription {
private static final Map<Byte, String> STATUS = new HashMap<Byte, String>(){{
put((byte) 1, "sale");
put((byte) 3, "rent");
}};
private byte status;
@Transient
private String statusName;
public byte getStatus() {
return this.status;
}
public void setStatus(byte status) {
this.status = status;
}
public String getStatusName() {
return STATUS.get(this.status);
}
public void setStatusName(byte status) {
this.statusName = STATUS.get(status);
}
}
Controller Class:
public class InscriptionController
{
@Autowired
private InscriptionRepository inscriptionRepository;
@RequestMapping(method = RequestMethod.GET)
public Page<Inscription> findAll(
final @RequestParam(defaultValue = "0", required = false) int offset,
final @RequestParam(defaultValue = "20", required = false) int limit,
@Spec(params = "status", path = "statusName", spec = In.class) Specification<Inscription> spec
)
{
Page<Inscription> inscriptions = inscriptionRepository.findAll(spec, new PageRequest(offset, limit));
return inscriptions;
}
Repo Class:
public interface InscriptionRepository extends PagingAndSortingRepository<Inscription, String>, JpaSpecificationExecutor<Inscription> { }