我正在尝试使用一些简单的 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)
...