1

I'm trying to do a select using a few simple where conditions but I keep getting the exception 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;
    }

Output:

    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)
    ...

1 Answers1

0

In your code you define three parameters, and then wrap them with literals. Sending these invalid literals fails since these expressions are not supported by ObjectDB.

Try:

    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)
    );

The parameters should be set by:

    query.setParameter(name2, name);
    query.setParameter(surname2, surname);
    query.setParameter(password2, password);
ObjectDB
  • 1,312
  • 8
  • 9
  • Applying these changes now results in a better output: `sout: SELECT $1 FROM Person $1 WHERE (($1.name=:p1) AND ($1.surname=:p2) AND ($1.password=:p3))`. However, I still get `[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)` when executing `System.out.println("sout2: " + query.getSingleResult());` –  Oct 19 '14 at 22:06
  • I'm using ObjectDB version 2.5.7_03. `Person` entity implements Serializable. I have no objectdb.conf because I don't want to use any xml/text configurations (server-side **only** setup). `Person` has more member variables than just `name`, `surname` and `password` - not sure if this makes any difference. –  Oct 19 '14 at 22:30
  • I changed the `query.setParameter("name", name2);` lines to use the parameters in the signature. i.e. `query.setParameter("name", name);`. This seems to have resolved the issue. However, now I get `Attempt to execute a query with no argument for parameter p3 (error 775)` –  Oct 19 '14 at 22:47
  • 1
    Yes, you are right, there is also a problem with setting the parameters. See revised answer above. – ObjectDB Oct 20 '14 at 01:31