0

I have an entity with a persistent field declared as Serializable.

I would like to build a query with the CriteriaBuilder, that filters the results by the Serializable field. The database is Oracle, and the field type is RAW(255) as hbm2ddl defined it.

If i write the query with a plain JPQL TypedQuery, everything works fine (the Serializable field is the one with the name "entityId"):

    TypedQuery<Change> query = em.createQuery("FROM Change c WHERE c.entityClass = :class AND c.entityId = :id", Change.class);
    query.setParameter("class", Person.class.getName());
    query.setParameter("id", new Integer(2287));
    query.getResultList();

However, the very same query with criteria builder does not work:

    final CriteriaBuilder builder = em.getCriteriaBuilder();
    final CriteriaQuery<Change> criteriaQuery = builder.createQuery(Change.class);
    final Root<Change> from = criteriaQuery.from(Change.class);
    final CriteriaQuery<Change> select = criteriaQuery.select(from);
    final List<Predicate> predicates = new ArrayList<>();
    predicates.add(builder.equal(from.get("entityClass"), Person.class.getName()));
    predicates.add(builder.equal(from.get("entityId"), new Integer(2287)));
    select.where(predicates.toArray(new Predicate[predicates.size()]));
    final TypedQuery<Change> query = em.createQuery(select);
    query.getResultList();

It throws the following exception after invoking getResultList():

[2013-05-21 16:12:45,960] [com.mycompany.myproduct.server.Main.startServer(Main.java:56)] [ERROR] [main] - Error starting Server: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
...
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
...
Caused by: java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes: expected BINARY got NUMBER    
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:445)
...

Change.java:

@Entity
@Table(name = "T_REVISION_CHANGE")
@SequenceGenerator(name = "seq_revision_change", sequenceName = "SEQ_REVISION_CHANGE", allocationSize = 1)
public class Change {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_revision_change")
    private Integer id;

    @Column(name = "ENTITY_CLASS")
    private String entityClass;

    @Column(name = "ENTITY_ID")
    private Serializable entityId;

}

I tried to manually serialize the Integer but the same kind of exception was thrown saying that a Serializable instance was expected instead of a byte array... :)

Any comment would be much appreciated.

perissf
  • 15,979
  • 14
  • 80
  • 117
Istvan
  • 17
  • 1
  • 5
  • Are you really sure that you need a RAW data type? Are you aware of its use? It's used for binary data or byte strings. Not integers – perissf May 22 '13 at 11:14
  • I needed to declare the property as Serializable because there are entities in the project with composite keys, strings, etc.. it's a legacy schema that cant be changed unfortunately. – Istvan May 22 '13 at 14:36

0 Answers0