【发布时间】:2016-07-26 02:40:50
【问题描述】:
我正在使用带有 starter-data-jpa 的 spring boot 1.3.3 + 我正在使用 specification-arg-resolver 在 JPA Repo 中组合多个过滤器
我在实体中有一个与数据库状态ID匹配的字段调用状态属性,但是,我们想按状态名称搜索,我在实体的hashMap中定义了所有状态名称,就像下面的代码
但是,当我尝试按状态名称过滤时,我得到了
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:无法在此 ManagedType [api.domain.Inscription] 上找到具有给定名称 [statusName] 的属性
实体类:
@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);
}
}
控制器类:
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;
}
回购类:
public interface InscriptionRepository extends PagingAndSortingRepository<Inscription, String>, JpaSpecificationExecutor<Inscription> { }
【问题讨论】:
-
不想要的时候为什么还要加上@Transient呢?
-
@mh-dev 因为statusName在表中不存在,status存在但它是status的id,我想通过status name过滤。
标签: java hibernate spring-boot spring-data spring-data-jpa