我使用 JPA2 作为 MySQL 连接器,并使用查询生成器来构造查询。在尝试按实体参数对结果列表进行排序时,我发现了一些错误:
SEVERE: java.lang.IllegalArgumentException: The attribute [customer}] from the managed type [EntityTypeImpl@1572996478:Documentation [ javaType: class pl.ego.software.entity.Documentation descriptor: RelationalDescriptor(pl.ego.software.entity.Documentation --> [DatabaseTable(documentation)]), mappings: 20]] is not present.
我不知道为什么,但这是唯一一个不存在于整个实体中的实体参数。当尝试使用 Root 类的 get 方法时,会引发此异常。
Root<Documentation> u = select.from(Documentation.class);
CriteriaBuilder builder = em.getCriteriaBuilder();
if (sortField != null) {
Order o;
if (sortOrder == SortOrder.ASCENDING) {
o = builder.asc(u.get(sortField));
} else {
o = builder.desc(u.get(sortField));
}
select.orderBy(o);
}
当 sortField 值设置为“customer”时,u.get(sortField) 会引发上述异常。如果 sortField 设置为某个不同的值,一切都会很好。
这是实体代码的一部分,用于确保参数存在。
@Size(max = 150)
@Column(name = "customer")
private String customer;
最佳答案
不完全是,但你的帖子给了我一些线索。基本上是的,由于字段名称末尾有“}”符号而引发了异常,但直到现在才知道为什么:)我遇到了一些问题
sortBy="#{element.customer} "
末尾有空格。
sortBy="#{element.customer}"
删除它修复了整个问题;)
关于java - 实体参数不能被Root的get方法使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14550755/