1

我正在尝试使用一些简单的 where 条件进行选择,但我不断收到异常Attempt to store an instance of a non persistable type com.objectdb.jpa.criteria.Expressions$h (error 303)

public Person read(String surname, String name, String password) throws NonUniqueResultException, NoResultException, PersistenceException, UserException {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        Person Person = null;
        try {
            entityManager.getTransaction().begin();
            CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
            CriteriaQuery<Person> criteriaQuery = criteriaBuilder.createQuery(Person.class);
            Root<Person> root = criteriaQuery.from(Person.class);
            criteriaQuery.select(root);
            ParameterExpression<String> name2 = criteriaBuilder.parameter(String.class);
            ParameterExpression<String> surname2 = criteriaBuilder.parameter(String.class);
            ParameterExpression<String> password2 = criteriaBuilder.parameter(String.class);
            criteriaQuery.where(
                    criteriaBuilder.equal(root.get("name"), criteriaBuilder.literal(name2)),
                    criteriaBuilder.equal(root.get("surname"), criteriaBuilder.literal(surname2)),
                    criteriaBuilder.equal(root.get("password"), criteriaBuilder.literal(password2))
            );
            TypedQuery<Person> query = entityManager.createQuery(criteriaQuery);
            query.setParameter("name", name2);
            query.setParameter("surname", surname2);
            query.setParameter("password", password2);
            System.out.println("sout: " + query.toString());
            System.out.println("sout2: " + query.getSingleResult());
            entityManager.getTransaction().commit();
        } finally {
            if (entityManager.getTransaction().isActive()) {
                entityManager.getTransaction().rollback();
            }
            entityManager.close();
        }
        return Person;
    }

输出:

    sout: SELECT $1 FROM Person $1 WHERE (($1.name=:p1) AND ($1.surname=:p1) AND ($1.password=:p1))
    [ObjectDB 2.5.7_03] javax.persistence.PersistenceException
Attempt to store an instance of a non persistable type com.objectdb.jpa.criteria.Expressions$h (error 303)
    at com.objectdb.jpa.JpaQuery.getSingleResult(JpaQuery.java:754)
    ...
4

1 回答 1

1

在您的代码中,您定义了三个参数,然后用文字包装它们。发送这些无效文字会失败,因为 ObjectDB 不支持这些表达式。

尝试:

    ParameterExpression<String> name2 = criteriaBuilder.parameter(String.class);
    ParameterExpression<String> surname2 = criteriaBuilder.parameter(String.class);
    ParameterExpression<String> password2 = criteriaBuilder.parameter(String.class);
    criteriaQuery.where(
        criteriaBuilder.equal(root.get("name"), name2),
        criteriaBuilder.equal(root.get("surname"), surname2),
        criteriaBuilder.equal(root.get("password"), password2)
    );

参数应通过以下方式设置:

    query.setParameter(name2, name);
    query.setParameter(surname2, surname);
    query.setParameter(password2, password);
于 2014-10-19T22:01:55.737 回答