gpt4 book ai didi

java - 尝试存储非持久类型 com.objectdb.jpa.criteria.Expressions$h 的实例(错误 303)

转载 作者:太空宇宙 更新时间:2023-11-04 06:28:35 26 4
gpt4 key购买 nike

我尝试使用一些简单的 where 条件进行选择,但我不断收到异常尝试存储非持久类型 com.objectdb.jpa.criteria.Expressions$h 的实例(错误 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)
...

最佳答案

在代码中定义三个参数,然后用文字包装它们。发送这些无效文字会失败,因为 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);

关于java - 尝试存储非持久类型 com.objectdb.jpa.criteria.Expressions$h 的实例(错误 303),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26454867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com