简体   繁体   English

如何解决JPA-CriteriaAPI的Java警告和编译错误?

[英]How to solve this Java Warning & Compilation error for JPA-CriteriaAPI?

Eclipse中的Java警告

If I declare like following it gives compilation error as follows : 如果我像下面这样声明,则会产生如下编译错误:

Eclipse中的Java编译错误

ParameterExpression<List<?>> vp=cb.parameter(List.class);

如果不起作用,请保留原始类型并添加@SuppressWarning('rawtypes');

Please make sure that your question should have all the information which will convey your problem statment very clearly. 请确保您的问题应具有所有信息,这些信息将非常清楚地传达您的问题陈述。 What I can guess from above image is that your are facing problem while creating ParameterExpression. 我可以从上面的图片中猜测,您在创建ParameterExpression时遇到了问题。 And for that you can take a look at following sample code. 为此,您可以看一下以下示例代码。

private List<Customer> findCustomerWithParam(String name) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Customer> criteriaQuery = cb.createQuery(Customer.class);
Root<Customer> customer = criteriaQuery.from(Customer.class);
ParameterExpression<String> nameParameter = cb.parameter(String.class, "name");
criteriaQuery.select(customer).where(cb.equal(customer.get("name"), nameParameter));
return em.createQuery(criteriaQuery).setParameter("name", name).getResultList();

} }

Hope this might help you. 希望这对您有帮助。

The problem stems using the following pattern 问题源于以下模式

public <T> T f(Class<T> type) {
    return type.newInstance();
}

This does not yet work satisfactory for container classes. 对于容器类,这还不能令人满意。 An inefficient work-around would be to copy the list. 一种低效的解决方法是复制列表。

List<?> list0 = f(List.class);
List<String> list = list0.stream()
        .map(Objects::toString)
        //Or .map(s -> (String)s)
        .collect(Collectors.toList());

So it is an API restriction. 因此,这是一个API限制。 I confess not being aware that the method parameter could deliver. 我承认没有意识到方法parameter可以实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM