JPA CriteriaBuilder:带有Seri​​alizable字段的谓词抛出异常

时间:2013-05-22 06:36:35

标签: oracle jpa serialization criteria-api predicate

我有一个实体,其持久字段声明为Serializable。

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

如果我使用普通的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)
...

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;

}

我尝试手动序列化Integer但是抛出了同样的异常,说是期望Serializable实例而不是字节数组......:)

任何评论都会非常感激。

0 个答案:

没有答案