Root的get方法不能使用实体参数

时间:2013-01-27 18:37:25

标签: java jpa-2.0 root

我正在使用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)引发上面提到的Exception。如果sortField设置为某个不同的值,一切都很好。

以下是确保参数存在的实体代码的一部分。

@Size(max = 150)
@Column(name = "customer")
private String customer;

1 个答案:

答案 0 :(得分:0)

不完全,但你的帖子给了我一些线索。基本上是的,因为字段名称末尾的“}”符号引发异常,但直到现在才知道原因:)我遇到了一些问题

 sortBy="#{element.customer} "

最后有空白区域。

 sortBy="#{element.customer}"

删除它固定整件事;)

相关问题