0

我有一个具有声明为可序列化的持久字段的实体。

我想使用 CriteriaBuilder 构建一个查询,该查询通过 Serializable 字段过滤结果。数据库是 Oracle,字段类型是 hbm2ddl 定义的 RAW(255)。

如果我使用普通的 JPQL TypedQuery 编写查询,一切正常(Serializable 字段是名称为“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();

但是,使用标准构建器的相同查询不起作用:

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

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

更改.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;

}

我试图手动序列化 Integer 但抛出了同样类型的异常,说需要一个 Serializable 实例而不是字节数组...... :)

任何评论将不胜感激。

4

0 回答 0