Criteria query error: Unexpected query token

#1

Using JPA2, I can save entities now in JBoss, and also fetch them uniquely by id. However, if I try to fetch it by getting back all instances (e.g. findAll, I get the following error:

org.jboss.arquillian.spi.ArquillianProxyException: com.objectdb.o._PersistenceException : Unexpected query token 'FROM' (SELECT is expected) [Proxied because : Could not find suitable constructor]
at com.objectdb.o._PersistenceException.b(_PersistenceException.java:45)
at com.objectdb.o.JPE.g(JPE.java:140)
at com.objectdb.o.ERR.f(ERR.java:59)
at com.objectdb.o.OBC.onObjectDBError(OBC.java:1398)

 

Code:

    public <T extends BaseEntity> List<T> findAll(Class<T> aClass) {
         CriteriaBuilder qb = entityManager.getCriteriaBuilder();
         CriteriaQuery<T> c = qb.createQuery(aClass);
         c.from(aClass);
         return entityManager.createQuery(c).getResultList();
    }

 

Entities:

@Entity()
@Table(name = "test_entity")
public class BaseEntityTest extends BaseEntity{


    /**
  *
  */
private static final long serialVersionUID = 613965399994067819L;

public BaseEntityTest() {
  super();
}

@Column(length = 16)
    private String testColumn;

    public String getTestColumn() {
        return testColumn;
    }

    public void setTestColumn(String testColumn) {
        this.testColumn = testColumn;
    }
}


@MappedSuperclass
public abstract class BaseEntity implements Serializable{

    private static final long serialVersionUID = -7979088681300801112L;
    /**
     * Primary key for entity
     */
    @Id
//    @GeneratedValue(strategy = GenerationType.TABLE, generator = "sequences")
//    @TableGenerator(name = "sequences", initialValue = 1, allocationSize = 50)
//    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @GeneratedValue
    private Long id;

    /**
     * Version of object, supporting optimistic lock
     */
    @Column (name = "opt_lock")
    @Version()
    private Long optimisticLock;

    /**
     * Flag to disable entity
     */
    @Column
    private boolean deleted = false;

    /**
     * Audit
     */
    @Embedded
    private Audit audit;

    public Long getId() {
        return id;
    }

    public Long getOptimisticLock() {
        return optimisticLock;
    }
}

The same code, against a PostgreSQL jta datasource works fine however. Is there anything glaringly wrong with my code I'm failing to see? Or is Criteria a W.I.P?

 

Cheers

#2

It seems that the query is invalid because a SELECT is not specified.

Unexpected query token 'FROM' (SELECT is expected)

A query with no SELECT may be valid in Hibernate HQL but not in JPQL and JPA Criteria API.

ObjectDB Support
#3

My apologies, in my eagerness to test I think I need to do an audit on all criteria queries. Works well. Fetched 256,000 records in 9 seconds (laptop). Pretty good!!!!

If anyone is interested in my mistake, the fixed shared code is:

public <T extends BaseEntity> List<T> findAll(Class<T> aClass) {
     CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
     CriteriaQuery<T> q = criteriaBuilder.createQuery(aClass);
     Root<T> entityRoot = q.from(aClass);
    
q.select(entityRoot);
     return entityManager.createQuery(q).getResultList();
}

Thanks again, I'm enjoying the simplicity of objectdb. Now I'm going to setup the JDO annotations to build up indexes.

 

Reply