java.lang.IllegalArgumentException:您为此查找操作提供了不正确 PK class 的实例。

java.lang.IllegalArgumentException: You have provided an instance of an incorrect PK class for this find operation.

我正在尝试创建一个 URI 并且 return 是一个正在寻找 NIF 的对象。 (一个自定义过滤器) 我试图通过 id 复制搜索但不起作用,事实上,我不确定我在做什么。我有两个 类 具有这些功能

ClientesAbstractFacade.java

    public T findNif(Object nif) {
        return getEntityManager().find(entityClass, nif);
    }

lientesFacadeREST.java

    @GET
    @Path("nif/{nif}")
    @Produces({MediaType.APPLICATION_JSON})
    public Clientes findNif(@PathParam("nif") String nif) {
        return super.findNif(nif);
    }

这里是 POJO

@Entity
@Table(name = "clientes")
public class Clientes implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

如您所见,我正在尝试进行自定义搜索,一些简单的事情,然后实现登录。 但我什至无法通过 nif 进行过滤,这些 return 错误 500

java.lang.IllegalArgumentException: You have provided an instance of an incorrect PK class for this find operation. Class expected : class java.lang.Integer, Class received : class java.lang.String.

异常说明了一切:

IllegalArgumentException: You have provided an instance of an incorrect PK class for this find operation. Class expected : class java.lang.Integer, Class received : class java.lang.String

getEntityManager().find(entityClass, nif) 正在处理您的 Table 的 主键 列。正如异常所述,这是一个整数。 我猜您想使用您的 NamedQueries,因此必须使用 EntityManager.

createNamedQuery-方法

所以你的 find-method 应该看起来像这样:

public T findNif(String nif) {
    return (T) getEntityManager().createNamedQuery("Clientes.findByNif", Clientes.class)
              .setParameter("nif", nif).getSingleResult();
}